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.feature

Feature: Calculator Addition
  In order to verify that the calculator can perform addition
  As a user
  I want to ensure that the addition operation is correct

  Scenario: Add two numbers
    Given the calculator is turned on
    When I add 5 and 7
    Then the result should be 12

创建步骤定义文件

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

# calculator_steps.py

from 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:3
  In order to verify that the calculator can perform addition
  As a user
  I want to ensure that the addition operation is correct
  Scenario: Add two numbers           # Features/Calculator/calculator.feature:8
    Given the calculator is turned on # steps/calculator_steps.py:5
    When I add 5 and 7                # steps/calculator_steps.py:9
    Then the result should be 12      # steps/calculator_steps.py:13

Feature: Calculator Addition test # Features/Calculator2/calculator2.feature:3
  In order to verify that the calculator can perform addition
  As a user
  I want to ensure that the addition operation is correct
  Scenario: Add two numbers test      # Features/Calculator2/calculator2.feature:8
    Given the calculator is turned on # steps/calculator_steps.py:5
    When I add 5 and 7                # steps/calculator_steps.py:9
    Then the result should be 12      # steps/calculator_steps.py:13

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

执行部分用例

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

PS C:\Automation\Test\BDD> behave Features/Calculator2
Feature: Calculator Addition test # Features/Calculator2/calculator2.feature:3
  In order to verify that the calculator can perform addition
  As a user
  I want to ensure that the addition operation is correct
  Scenario: Add two numbers test      # Features/Calculator2/calculator2.feature:8
    Given the calculator is turned on # steps/calculator_steps.py:5
    When I add 5 and 7                # steps/calculator_steps.py:9
    Then the result should be 12      # steps/calculator_steps.py:13

1 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:3
  In order to verify that the calculator can perform addition
  As a user
  I want to ensure that the addition operation is correct
  Scenario: Add two numbers           # Features/Calculator/calculator.feature:8
    Given the calculator is turned on # steps/calculator_steps.py:5
    When I add 5 and 7                # steps/calculator_steps.py:9
    Then the result should be 12      # steps/calculator_steps.py:13
Feature: Calculator Addition 2 # Features/Calculator2/calculator2.feature:3
  In order to verify that the calculator can perform addition
  As a user
  I want to ensure that the addition operation is correct
  Scenario: Add two numbers 2         # Features/Calculator2/calculator2.feature:8
    Given the calculator is turned on # steps/calculator_steps.py:5
    When I add 5 and 7                # steps/calculator_steps.py:9
    Then the result should be 12      # steps/calculator_steps.py:13

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.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

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/263789.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

Modbus-TCP数据帧

Modbus-TCP基于4种报文类型 MODBUS 请求是客户机在网络上发送用来启动事务处理的报文MODBUS 指示是服务端接收的请求报文MODBUS 响应是服务器发送的响应信息MODBUS 证实是在客户端接收的响应信息 Modbus-TCP报文: 报文头MBAP MBAP为报文头,长度为7字节&#xff0c…

postman和Jmeter的区别

📢专注于分享软件测试干货内容,欢迎点赞 👍 收藏 ⭐留言 📝 如有错误敬请指正!📢交流讨论:欢迎加入我们一起学习!📢资源分享:耗时200小时精选的「软件测试」资…

非隔离恒压ACDC稳压智能电源模块芯片推荐:SM7015

非隔离恒压ACDC稳压智能电源模块芯片是一种用于将交流(AC)电源转换为直流(DC)电源的集成电路。这种芯片具有恒压输出功能,能够保持输出电压的稳定,适用于各种需要直流电源的应用场景。 非隔离电源模块通常…

【华为数据之道学习笔记】6-5数据地图的核心价值

数据供应者与消费者之间往往存在一种矛盾:供应者做了大量的数据治理工作、提供了大量的数据,但数据消费者却仍然不满意,他们始终认为在使用数据之前存在两个重大困难。 1)找数难 企业的数据分散存储在上千个数据库、上百万张物理表…

2024年,消费品零售企业如何规划大模型和数据技术落地?

导读:品牌商和零售商目前都在做2024年的规划,本次分享基于爱分析过往的研究,带来消费品零售行业2024年宏观趋势和方向,以及如何落地大模型和数据技术。 分享嘉宾|张扬 爱分析联合创始人兼首席分析师 内容来源于爱分析…

【算法刷题】Day22

文章目录 1. 按摩师题干:算法原理:(dp)1. 状态表示:2. 状态转移方程3. 初始化4. 填表顺序5. 返回值 代码: 2. 寻找数组的中心下标题干:算法原理:(前缀和)代码…

大数据处理与分析

掌握分布式并行编程框架MapReduce掌握基于内存的分布式计算框架Spark理解MapReduce的工作流程、Spark运行原理熟悉机器学习概念 一.MapReduce Hadoop MapReduce是一个软件框架,基于该框架能够容易地编写应用程序,这些应用程序能够运行在由上千个商用机器…

亚马逊品牌分析ABA功能有哪些?亚马逊选品的量化标准有哪些?——站斧浏览器

亚马逊品牌分析ABA功能有哪些? 1、品牌市场份额(Share of Voice) ABA提供了品牌在特定类别中市场份额的详细数据。这一模块帮助品牌所有者准确评估其品牌在整个市场中的竞争地位和表现。通过了解市场份额,品牌方可以制定更具针对…

2024年金三银四必备面试题之自动化测试面试题及答案大全

1.你如何用Selenium测试? SeleniumMavenTestNGJekins 2.如何解决问题? 先思考,然后百度,考虑网速、电脑配置等原因,这题主要看重解决问题的能力和思维。 3.你是怎么开发测试框架的? SeleniumMavenTestNGJ…

【接口测试】如何定位BUG的产生原因

我们从在日常功能测试过程中对UI的每一次操作说白了就是对一个或者多个接口的一次调用,接口的返回的内容(移动端一般为json)经过前端代码的处理最终展示在页面上。http接口是离我们最近的一层接口,web端和移动端所展示的数据就来自于这层,那么…

ARM作业1

汇编实现三个灯闪烁 汇编代码&#xff1a; .text .global _start _start: 设置GPIOE,GPIOF时钟使能LDR R0,0X50000A28 LDR R1,[R0] ORR R1,R1,#(0x3<<4) STR R1,[R0] 设置PE10,PF10,PE8为输出 LED1LDR R0,0X50006000LDR R1,[R0]ORR R1,R1,#(0X1<<20)BIC R1…

二值图像的游程编码

二值图像的游程编码是一种用于图像压缩和数据传输的有效方法&#xff0c;它能够显著减小图像文件的大小&#xff0c;同时保留图像的重要信息。本文将介绍二值图像的游程编码的原理、优势以及在实际应用中的作用。 一、什么是二值图像的游程编码&#xff1f; 二值图像是由黑白…

位运算:Leetcode137.只出现一次的数字(2)

题目描述&#xff1a; 给你一个整数数组 nums &#xff0c;除某个元素仅出现 一次 外&#xff0c;其余每个元素都恰出现 三次 。请你找出并返回那个只出现了一次的元素。 示例 1&#xff1a; 输入&#xff1a;nums [2,2,3,2] 输出&#xff1a;3示例 2&#xff1a; 输入&…

STM32的以太网外设+PHY(LAN8720)使用详解(2):硬件设计

0 工具准备 1.野火 stm32f407霸天虎开发板 2.LAN8720数据手册 3.STM32F4xx中文参考手册1 PHY&#xff08;LAN8720&#xff09;硬件配置 1.1 硬件配置引脚说明 在LAN8720上电或复位时会读取一些特定引脚的电平&#xff0c;根据电平来进行硬件配置。LAN8720的引脚分布如下&…

电子科大软件测试~第三次作业

第三次作业 第一题 采用JUnit软件测试框架进行测试程序编程&#xff0c;实现对下面java程序进行单元测试&#xff0c;找出其中缺陷。然后修改缺陷&#xff0c;直到通过单元测试&#xff0c;给出测试程序脚本和运行结果界面。 public class getMax {public int get_max(int x…

【CMake保姆级教程】制作动静态链接库、指定动静态库输出路径

文章目录 前言一、动静态链接库的介绍1.1 动态链接库 (DLL)1.2 静态链接库 (LIB) 二、制作静态库三、制作动态库四、指定动静态库输出路径4.1 方式1 - 适用于动态库4.2 方式2 - 都适用 总结 前言 在软件开发中&#xff0c;我们经常听到动态链接库&#xff08;Dynamic Link Lib…

劈窗算法反演地表温度

目录 摘要操作步骤提取热红外单波段提取NDVI同步像元分辨率与个数劈窗算法地表温度反演制图 摘要 主要使用HJ-2&#xff08;环境减灾二号卫星&#xff09;的IRS传感器的两个热红外波段&#xff0c;以及红波段与近红波段计算得到的NDVI&#xff0c;使用劈窗算法&#xff0c;得到…

如何在Linux下搭建接口自动化测试平台

我们今天来学习一下在Linux下如何搭建基于HttpRunner开发的接口自动化测试平台吧&#xff01; 需要在Linux上提前准备的环境&#xff08;下面是本人搭建时的环境&#xff09;&#xff1a; 1&#xff0c;Python 3.6.8 2&#xff0c;MySQL 5.7 一&#xff1a;下载HttpRunner…

用数码管慢速动态扫描显示数字“1234“

#include<reg51.h> // 包含51单片机寄存器定义的头文件 void delay(void) //延时函数&#xff0c;延时一段时间 { unsigned char i,j; for(i0;i<250;i) for(j0;j<250;j) ; } void main(void) { while(1) //无限循…

AI绘画中CLIP文本-图像预训练模型

介绍 OpenAI 在 2021 年提出了 CLIP&#xff08;Contrastive Language–Image Pretraining&#xff09;算法&#xff0c;这是一个先进的机器学习模型&#xff0c;旨在理解和解释图像和文本之间的关系。CLIP 的核心思想是通过大规模的图像和文本对进行训练&#xff0c;学习图像…