BDD - Python Behave Runner Script

2023-12-26 05:28
文章标签 python script bdd runner behave

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

BDD - Python Behave Runner Script

  • 引言
  • Runner Script
    • subprocess.run 调用 Behave 命令行
    • 调用 Behave 提供的 API behave_main

引言

通过终端命令执行 Behave 测试用例,有时 IDE 重启了,还得重新敲一遍命令,很是麻烦,说实话我都懒得记那些命令选项,于是我就想是否找个偷懒办法,当然是可以的,我们可以创建一个 Runner 脚本文件,每次运行这个脚本文件就可以了。

想了解更多 Behave 相关的文章,欢迎阅读《Python BDD Behave 系列》,持续更新中。

Runner Script

首先在 features 文件夹同级目录创建一个 runner.py 文件。代码实现有两种,一种利用 subprocess.run 直接执行 Behave 的命令行参数,另外一种就是使用 Behave 提供的API behave_main ,而不是直接调用其命令行工具。

在这里插入图片描述

subprocess.run 调用 Behave 命令行

定义一个 run_behave 方法,用来构建 Behave 命令选项,通过 subprocess.run 执行 Behave 命令。调用 run_behave 方法只需传需要的参数就可以了。

例如:运行 BDD/Features/tag_example 下,标签为 @cart 的测试用例,允许终端 Print 输出,同时生成 html 测试报告,就可以这样调用了
run_behave(feature = “BDD/Features/tag_example”, tags = “cart”, no_capture= True, html_report= True)

import subprocessdef run_behave(feature, tags, no_capture, html_report):behave_command = ["behave"]# Include feature if providedif feature:behave_command.append(feature)# Include tags if providedif tags:behave_command.extend(["--tags", tags])# Include --no-capture optionif no_capture:behave_command.append("--no-capture")  # Include html reportif html_report:behave_command.extend(["-f", "behave_html_formatter:HTMLFormatter","-o", "BDD/output/report.html"])     try:subprocess.run(behave_command, check=True)except subprocess.CalledProcessError as e:print(f"Behave execution failed. Return code: {e.returncode}")# Handle the error or raise an exception if neededif __name__ == "__main__":run_behave(feature = "BDD/Features/tag_example",tags = "cart",no_capture= True,html_report= True)

终端输出,这里有一些 hooks 输出:

PS C:\Automation\Test> & C:/Users/xxx/AppData/Local/Programs/Python/Python39/python.exe c:/Automation/Test/BDD/runner.py
Before all tests
Before feature: Shopping Cart and Order Process
Before tag: cart
Before tag: smoke
Before scenario: Guest user adds items to the cart
Before step: the user is on the home page
After step: the user is on the home page
Before step: the user adds an item to the cart
After step: the user adds an item to the cart
Before step: the user should see the item in the cart
After step: the user should see the item in the cart
After scenario: Guest user adds items to the cart
After tag: cart
After tag: smokeBefore tag: cart
Before tag: regression
Before scenario: Registered user removes items from the cart
Before step: the user is logged in
After step: the user is logged in
Before step: the user has items in the cart
After step: the user has items in the cart
Before step: the user removes an item from the cart
After step: the user removes an item from the cart
Before step: the user should see the updated cart
After step: the user should see the updated cart
After scenario: Registered user removes items from the cart
After tag: cart
After tag: regressionAfter feature: Shopping Cart and Order Process
After all tests
1 feature passed, 0 failed, 0 skipped
2 scenarios passed, 0 failed, 2 skipped
7 steps passed, 0 failed, 9 skipped, 0 undefined
Took 0m0.005s

Html 报告:
在这里插入图片描述

调用 Behave 提供的 API behave_main

定义一个 run_behave 方法,构建 Behave 参数,通过 behave_main.run_behave 传参来执行测试用例。
这里需要注意一下 behave 1.2.6 版本会有异常:AttributeError: 'list' object has no attribute 'version'
据说 behave 1.2.5 没有问题,小伙伴们可以试一下。

# runner.pyfrom behave import __main__ as behave_maindef run_behave(features_path='features', tags=None):# 构建 Behave 参数behave_args = [features_path]# 添加标签过滤if tags:behave_args.extend(['--tags', tags])# 使用 Behave API 运行测试behave_main.run_behave(behave_args)if __name__ == "__main__":# 你可以在这里设置默认的 features_path 和 tagsdefault_features_path = 'BDD/Features/tag_example'default_tags = None# 运行 Behave 测试run_behave(features_path=default_features_path, tags=default_tags)

终端输出:

PS C:\Automation\Test> & C:/Users/xxx/AppData/Local/Programs/Python/Python39/python.exe c:/Automation/Test/BDD/runner.py
Traceback (most recent call last):File "c:\Automation\Test\BDD\runner.py", line 22, in <module>run_behave(features_path=default_features_path, tags=default_tags)File "c:\Automation\Test\BDD\runner.py", line 14, in run_behavebehave_main.run_behave(behave_args)File "C:\Users\xxx\AppData\Local\Programs\Python\Python39\lib\site-packages\behave\__main__.py", line 67, in run_behaveif config.version:
AttributeError: 'list' object has no attribute 'version'
PS C:\Automation\Test> Behave --version
behave 1.2.6

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



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

相关文章

基于Python实现一个图片拆分工具

《基于Python实现一个图片拆分工具》这篇文章主要为大家详细介绍了如何基于Python实现一个图片拆分工具,可以根据需要的行数和列数进行拆分,感兴趣的小伙伴可以跟随小编一起学习一下... 简单介绍先自己选择输入的图片,默认是输出到项目文件夹中,可以自己选择其他的文件夹,选择需要拆分的行数和列数,可以通过

Python中反转字符串的常见方法小结

《Python中反转字符串的常见方法小结》在Python中,字符串对象没有内置的反转方法,然而,在实际开发中,我们经常会遇到需要反转字符串的场景,比如处理回文字符串、文本加密等,因此,掌握如何在Pyt... 目录python中反转字符串的方法技术背景实现步骤1. 使用切片2. 使用 reversed() 函

Python中将嵌套列表扁平化的多种实现方法

《Python中将嵌套列表扁平化的多种实现方法》在Python编程中,我们常常会遇到需要将嵌套列表(即列表中包含列表)转换为一个一维的扁平列表的需求,本文将给大家介绍了多种实现这一目标的方法,需要的朋... 目录python中将嵌套列表扁平化的方法技术背景实现步骤1. 使用嵌套列表推导式2. 使用itert

使用Docker构建Python Flask程序的详细教程

《使用Docker构建PythonFlask程序的详细教程》在当今的软件开发领域,容器化技术正变得越来越流行,而Docker无疑是其中的佼佼者,本文我们就来聊聊如何使用Docker构建一个简单的Py... 目录引言一、准备工作二、创建 Flask 应用程序三、创建 dockerfile四、构建 Docker

Python使用vllm处理多模态数据的预处理技巧

《Python使用vllm处理多模态数据的预处理技巧》本文深入探讨了在Python环境下使用vLLM处理多模态数据的预处理技巧,我们将从基础概念出发,详细讲解文本、图像、音频等多模态数据的预处理方法,... 目录1. 背景介绍1.1 目的和范围1.2 预期读者1.3 文档结构概述1.4 术语表1.4.1 核

Python使用pip工具实现包自动更新的多种方法

《Python使用pip工具实现包自动更新的多种方法》本文深入探讨了使用Python的pip工具实现包自动更新的各种方法和技术,我们将从基础概念开始,逐步介绍手动更新方法、自动化脚本编写、结合CI/C... 目录1. 背景介绍1.1 目的和范围1.2 预期读者1.3 文档结构概述1.4 术语表1.4.1 核

Conda与Python venv虚拟环境的区别与使用方法详解

《Conda与Pythonvenv虚拟环境的区别与使用方法详解》随着Python社区的成长,虚拟环境的概念和技术也在不断发展,:本文主要介绍Conda与Pythonvenv虚拟环境的区别与使用... 目录前言一、Conda 与 python venv 的核心区别1. Conda 的特点2. Python v

Python使用python-can实现合并BLF文件

《Python使用python-can实现合并BLF文件》python-can库是Python生态中专注于CAN总线通信与数据处理的强大工具,本文将使用python-can为BLF文件合并提供高效灵活... 目录一、python-can 库:CAN 数据处理的利器二、BLF 文件合并核心代码解析1. 基础合

Python使用OpenCV实现获取视频时长的小工具

《Python使用OpenCV实现获取视频时长的小工具》在处理视频数据时,获取视频的时长是一项常见且基础的需求,本文将详细介绍如何使用Python和OpenCV获取视频时长,并对每一行代码进行深入解析... 目录一、代码实现二、代码解析1. 导入 OpenCV 库2. 定义获取视频时长的函数3. 打开视频文

Python中你不知道的gzip高级用法分享

《Python中你不知道的gzip高级用法分享》在当今大数据时代,数据存储和传输成本已成为每个开发者必须考虑的问题,Python内置的gzip模块提供了一种简单高效的解决方案,下面小编就来和大家详细讲... 目录前言:为什么数据压缩如此重要1. gzip 模块基础介绍2. 基本压缩与解压缩操作2.1 压缩文