BDD - Python Behave 入门

2023-12-22 18:28
文章标签 python 入门 bdd behave

本文主要是介绍BDD - Python Behave 入门,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

BDD - Python Behave 入门

  • Behave 是什么
  • Behave 的主要特点和组成部分
  • Behave 实践
    • 安装 Behave
    • Behave 项目目录结构
    • 创建项目
      • 创建 Feature 文件
      • 创建步骤定义文件
    • 执行用例
      • 执行全部用例
      • 执行部分用例
    • 生成报告
      • 生成 Json report
      • 生成 HTML 报告
      • 生成 Junit report
      • 生成 Cucumber report
      • 生成 Allure 报告

Behave 是什么

Behave 是一个用于 Python 的行为驱动开发(BDD)框架。BDD 是一种敏捷软件开发方法,它促使开发人员、QA(质量保障)团队和非技术利益相关者之间更好地合作,以确保软件开发的最终产物符合预期的行为。

Behave 的 BDD 侧重于从用户的角度出发,关注软件的行为和功能,以一种易于理解的自然语言来描述。Behave 使用 Gherkin 语言来描述软件的功能,该语言是一种用于描述软件行为的业务可读的语言。Gherkin 语言的特点是简洁易懂,非技术人员也能够理解。

Behave 的主要特点和组成部分

Gherkin 语言: Behave 使用 Gherkin 语言描述软件的功能和行为。Gherkin 是一种简单的自然语言,它使用关键字(如 Given、When、Then)来描述场景和行为,这使得非技术人员能够轻松理解和参与。

Feature 文件: Behave 的测试用例以 .feature 文件的形式存在,其中包含了用 Gherkin 语言编写的场景描述。这些文件包含了对软件功能的期望行为和测试用例。

步骤定义: Behave 允许用户使用 Python 编写步骤定义,将 Gherkin 语言中描述的场景映射到实际的测试代码。步骤定义包括 Given、When、Then 等关键字,并通过正则表达式与实际的测试逻辑关联起来。

运行测试: Behave 提供了一个命令行界面,通过该界面可以运行定义的测试用例。测试执行过程中,Behave 将解释 Gherkin 语言的描述,并调用相应的步骤定义执行实际的测试代码。

报告生成: Behave 生成详细的测试报告,其中包含测试用例的执行结果、失败的步骤、日志信息等。这有助于团队追踪测试进度和了解软件的行为是否符合预期。

Behave 实践

官网 Github 和 文档

安装 Behave

pip install behave

或升级 Behave

pip install -U behave

Behave 项目目录结构

最简单的目录结构:
在这里插入图片描述
复杂的目录结构:
注意 Feature 文件 可以按子目录分类,但是步骤定义文件不能按子目录分类,必须统统都在 steps 文件夹中,steps 文件夹的位置跟 Features 目录推荐是兄弟关系比较清晰,也可以是父子关系。

在这里插入图片描述

创建项目

按下面结构来创建 feature 文件和步骤实现,测试计算器的加法。

在这里插入图片描述

创建 Feature 文件

创建一个名为 calculator.feature 的.feature 文件,其中描述了加法功能的场景
这里 calculator2.feature 只是 calculator.feature 的 copy,mock 两个功能吧。

# calculator.featureFeature: Calculator AdditionIn order to verify that the calculator can perform additionAs a userI want to ensure that the addition operation is correctScenario: Add two numbersGiven the calculator is turned onWhen I add 5 and 7Then the result should be 12

创建步骤定义文件

创建一个 Python 文件,用于定义 Gherkin 语言中描述的步骤的实际执行代码。保存为 calculator_steps.py

# calculator_steps.pyfrom behave import given, when, then@given('the calculator is turned on')
def step_calculator_turned_on(context):context.calculator_on = True@when('I add {num1:d} and {num2:d}')
def step_add_numbers(context, num1, num2):context.result = num1 + num2@then('the result should be {expected_result:d}')
def step_check_result(context, expected_result):assert context.result == expected_result, f"Actual result: {context.result}, Expected result: {expected_result}"

执行用例

执行全部用例

打开终端,进入包含 Features 和 Steps 目录的父目录 BDD,并运行以下命令:behave
Behave 将解释 BDD 目录下所有 .feature 文件中的场景,并调用 calculator_steps.py 中定义的步骤执行相应的测试逻辑

PS C:\Automation\Test\BDD> behave
Feature: Calculator Addition # Features/Calculator/calculator.feature:3In order to verify that the calculator can perform additionAs a userI want to ensure that the addition operation is correctScenario: Add two numbers           # Features/Calculator/calculator.feature:8Given the calculator is turned on # steps/calculator_steps.py:5When I add 5 and 7                # steps/calculator_steps.py:9Then the result should be 12      # steps/calculator_steps.py:13Feature: Calculator Addition test # Features/Calculator2/calculator2.feature:3In order to verify that the calculator can perform additionAs a userI want to ensure that the addition operation is correctScenario: Add two numbers test      # Features/Calculator2/calculator2.feature:8Given the calculator is turned on # steps/calculator_steps.py:5When I add 5 and 7                # steps/calculator_steps.py:9Then the result should be 12      # steps/calculator_steps.py:132 features passed, 0 failed, 0 skipped
2 scenarios passed, 0 failed, 0 skipped
6 steps passed, 0 failed, 0 skipped, 0 undefined
Took 0m0.000s

执行部分用例

也可以只运行部分 feature 场景,执行 behave Features/Calculator2
只运行 Features/Calculator2 目录下的用例,Features/Calculator 的用例不会执行。

PS C:\Automation\Test\BDD> behave Features/Calculator2
Feature: Calculator Addition test # Features/Calculator2/calculator2.feature:3In order to verify that the calculator can perform additionAs a userI want to ensure that the addition operation is correctScenario: Add two numbers test      # Features/Calculator2/calculator2.feature:8Given the calculator is turned on # steps/calculator_steps.py:5When I add 5 and 7                # steps/calculator_steps.py:9Then the result should be 12      # steps/calculator_steps.py:131 feature passed, 0 failed, 0 skipped
1 scenario passed, 0 failed, 0 skipped
3 steps passed, 0 failed, 0 skipped, 0 undefined
Took 0m0.000s

生成报告

生成 Json report

执行:behave -f json -o report.json

PS C:\Automation\Test\BDD> behave -f json -o report.json
2 features passed, 0 failed, 0 skipped
2 scenarios passed, 0 failed, 0 skipped
6 steps passed, 0 failed, 0 skipped, 0 undefined
Took 0m0.000s

上述命令将运行 Behave 测试,并将结果输出到当前目录下 report.json 文件中。你可以根据需要更改输出文件的名称。

生成 HTML 报告

如果你希望使用 HTML 报告,你可以考虑使用 behave-html-formatter 插件。
首先,你需要安装该插件:

pip install behave-html-formatter

然后,使用以下命令运行 Behave:behave -f behave_html_formatter:HTMLFormatter -o report.html

PS C:\Automation\Test\BDD> behave -f behave_html_formatter:HTMLFormatter -o report.html
2 features passed, 0 failed, 0 skipped
2 scenarios passed, 0 failed, 0 skipped
6 steps passed, 0 failed, 0 skipped, 0 undefined
Took 0m0.016s

这样就会在当前目录下生成一个名为 report.html 的 HTML 报告文件

在这里插入图片描述

生成 Junit report

执行命令:behave --junit

PS C:\Automation\Test\bdd> behave --junit 
Feature: Calculator Addition # Features/Calculator/calculator.feature:3In order to verify that the calculator can perform additionAs a userI want to ensure that the addition operation is correctScenario: Add two numbers           # Features/Calculator/calculator.feature:8Given the calculator is turned on # steps/calculator_steps.py:5When I add 5 and 7                # steps/calculator_steps.py:9Then the result should be 12      # steps/calculator_steps.py:13
Feature: Calculator Addition 2 # Features/Calculator2/calculator2.feature:3In order to verify that the calculator can perform additionAs a userI want to ensure that the addition operation is correctScenario: Add two numbers 2         # Features/Calculator2/calculator2.feature:8Given the calculator is turned on # steps/calculator_steps.py:5When I add 5 and 7                # steps/calculator_steps.py:9Then the result should be 12      # steps/calculator_steps.py:132 features passed, 0 failed, 0 skipped
2 scenarios passed, 0 failed, 0 skipped
6 steps passed, 0 failed, 0 skipped, 0 undefined
Took 0m0.007s

默认会创建 reports 子目录,生成测试报告。

在这里插入图片描述
当然也可以指定目录,执行命令:behave --junit --junit-directory my_reports

生成 Cucumber report

Behave 本身直接生成 Cucumber 报告的功能是有限的,因为 Cucumber 报告通常与 Gherkin 语法密切相关,而 Behave 使用的是 Gherkin 语法的 Python 版本。

如果你想生成类似 Cucumber 风格的报告,你可以考虑使用 behave2cucumber 工具。该工具能够将 Behave 的 JSON 报告转换为 Cucumber JSON 格式。

以下是使用该工具的步骤:

  1. 安装 behave2cucumber:
pip install behave2cucumber
  1. 运行 Behave 以生成 JSON 报告:
behave -f json -o report.json
  1. 使用 behave2cucumber 工具将 Behave JSON 报告转换为 Cucumber JSON 格式:
python -m behave2cucumber -i report.json -o cucumber_json.json

-i 表示由 behave 生成的测试 json 文件中的输入文件
-o 表示兼容 cucumber json文件的输出文件 cucumber_json

在这里插入图片描述

  1. 利用 jenkins 上的 cucumber reports 插件配置 cucumber json 文件生成 cucumber 报告
    在这里插入图片描述

生成 Allure 报告

对于更漂亮和交互式的报告,你可能需要考虑使用专门的报告生成工具,例如 Allure 或其他定制的报告工具。 Allure 支持 Behave 并提供了漂亮的图形化报告和交互式功能。

以下是使用 Allure 生成漂亮的报告的示例:

  1. 安装 Allure 及 Behave 插件:
pip install allure-behave
  1. 运行 Behave 测试并生成 Allure 报告:
PS C:\Automation\Test\bdd> behave -f allure_behave.formatter:AllureFormatter -o allure-results
2 features passed, 0 failed, 0 skipped
2 scenarios passed, 0 failed, 0 skipped
6 steps passed, 0 failed, 0 skipped, 0 undefined
Took 0m0.000s

当前目录下生成
在这里插入图片描述
3. 生成和查看 Allure 报告:

根据官方 Allure Report installation ,本地安装 Allure

  • 确保安装了Java版本8或更高版本,并且在JAVA_HOME环境变量中指定了它的目录。.
  • 从 latest Allure Report release on GitHub 下载 allure-.zip 或 allure-.tgz.
  • 将下载的压缩文件解压缩到任意目录,Allure 报告现在可以使用 bin/allure 或 bin/allure.bat 脚本运行,具体取决于操作系统。

生成和查看 report 执行命令:allure serve C:\Automation\Test\BDD\allure-results

在这里插入图片描述
在这里插入图片描述

这篇关于BDD - Python Behave 入门的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/524998

相关文章

Spring Security 从入门到进阶系列教程

Spring Security 入门系列 《保护 Web 应用的安全》 《Spring-Security-入门(一):登录与退出》 《Spring-Security-入门(二):基于数据库验证》 《Spring-Security-入门(三):密码加密》 《Spring-Security-入门(四):自定义-Filter》 《Spring-Security-入门(五):在 Sprin

python: 多模块(.py)中全局变量的导入

文章目录 global关键字可变类型和不可变类型数据的内存地址单模块(单个py文件)的全局变量示例总结 多模块(多个py文件)的全局变量from x import x导入全局变量示例 import x导入全局变量示例 总结 global关键字 global 的作用范围是模块(.py)级别: 当你在一个模块(文件)中使用 global 声明变量时,这个变量只在该模块的全局命名空

【Python编程】Linux创建虚拟环境并配置与notebook相连接

1.创建 使用 venv 创建虚拟环境。例如,在当前目录下创建一个名为 myenv 的虚拟环境: python3 -m venv myenv 2.激活 激活虚拟环境使其成为当前终端会话的活动环境。运行: source myenv/bin/activate 3.与notebook连接 在虚拟环境中,使用 pip 安装 Jupyter 和 ipykernel: pip instal

【机器学习】高斯过程的基本概念和应用领域以及在python中的实例

引言 高斯过程(Gaussian Process,简称GP)是一种概率模型,用于描述一组随机变量的联合概率分布,其中任何一个有限维度的子集都具有高斯分布 文章目录 引言一、高斯过程1.1 基本定义1.1.1 随机过程1.1.2 高斯分布 1.2 高斯过程的特性1.2.1 联合高斯性1.2.2 均值函数1.2.3 协方差函数(或核函数) 1.3 核函数1.4 高斯过程回归(Gauss

数论入门整理(updating)

一、gcd lcm 基础中的基础,一般用来处理计算第一步什么的,分数化简之类。 LL gcd(LL a, LL b) { return b ? gcd(b, a % b) : a; } <pre name="code" class="cpp">LL lcm(LL a, LL b){LL c = gcd(a, b);return a / c * b;} 例题:

【学习笔记】 陈强-机器学习-Python-Ch15 人工神经网络(1)sklearn

系列文章目录 监督学习:参数方法 【学习笔记】 陈强-机器学习-Python-Ch4 线性回归 【学习笔记】 陈强-机器学习-Python-Ch5 逻辑回归 【课后题练习】 陈强-机器学习-Python-Ch5 逻辑回归(SAheart.csv) 【学习笔记】 陈强-机器学习-Python-Ch6 多项逻辑回归 【学习笔记 及 课后题练习】 陈强-机器学习-Python-Ch7 判别分析 【学

Java 创建图形用户界面(GUI)入门指南(Swing库 JFrame 类)概述

概述 基本概念 Java Swing 的架构 Java Swing 是一个为 Java 设计的 GUI 工具包,是 JAVA 基础类的一部分,基于 Java AWT 构建,提供了一系列轻量级、可定制的图形用户界面(GUI)组件。 与 AWT 相比,Swing 提供了许多比 AWT 更好的屏幕显示元素,更加灵活和可定制,具有更好的跨平台性能。 组件和容器 Java Swing 提供了许多

【IPV6从入门到起飞】5-1 IPV6+Home Assistant(搭建基本环境)

【IPV6从入门到起飞】5-1 IPV6+Home Assistant #搭建基本环境 1 背景2 docker下载 hass3 创建容器4 浏览器访问 hass5 手机APP远程访问hass6 更多玩法 1 背景 既然电脑可以IPV6入站,手机流量可以访问IPV6网络的服务,为什么不在电脑搭建Home Assistant(hass),来控制你的设备呢?@智能家居 @万物互联

poj 2104 and hdu 2665 划分树模板入门题

题意: 给一个数组n(1e5)个数,给一个范围(fr, to, k),求这个范围中第k大的数。 解析: 划分树入门。 bing神的模板。 坑爹的地方是把-l 看成了-1........ 一直re。 代码: poj 2104: #include <iostream>#include <cstdio>#include <cstdlib>#include <al

nudepy,一个有趣的 Python 库!

更多资料获取 📚 个人网站:ipengtao.com 大家好,今天为大家分享一个有趣的 Python 库 - nudepy。 Github地址:https://github.com/hhatto/nude.py 在图像处理和计算机视觉应用中,检测图像中的不适当内容(例如裸露图像)是一个重要的任务。nudepy 是一个基于 Python 的库,专门用于检测图像中的不适当内容。该