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和OpenCV库实现实时颜色识别系统

《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解

一文深入详解Python的secrets模块

《一文深入详解Python的secrets模块》在构建涉及用户身份认证、权限管理、加密通信等系统时,开发者最不能忽视的一个问题就是“安全性”,Python在3.6版本中引入了专门面向安全用途的secr... 目录引言一、背景与动机:为什么需要 secrets 模块?二、secrets 模块的核心功能1. 基

python常见环境管理工具超全解析

《python常见环境管理工具超全解析》在Python开发中,管理多个项目及其依赖项通常是一个挑战,下面:本文主要介绍python常见环境管理工具的相关资料,文中通过代码介绍的非常详细,需要的朋友... 目录1. conda2. pip3. uvuv 工具自动创建和管理环境的特点4. setup.py5.

Python常用命令提示符使用方法详解

《Python常用命令提示符使用方法详解》在学习python的过程中,我们需要用到命令提示符(CMD)进行环境的配置,:本文主要介绍Python常用命令提示符使用方法的相关资料,文中通过代码介绍的... 目录一、python环境基础命令【Windows】1、检查Python是否安装2、 查看Python的安

Python UV安装、升级、卸载详细步骤记录

《PythonUV安装、升级、卸载详细步骤记录》:本文主要介绍PythonUV安装、升级、卸载的详细步骤,uv是Astral推出的下一代Python包与项目管理器,主打单一可执行文件、极致性能... 目录安装检查升级设置自动补全卸载UV 命令总结 官方文档详见:https://docs.astral.sh/

Python并行处理实战之如何使用ProcessPoolExecutor加速计算

《Python并行处理实战之如何使用ProcessPoolExecutor加速计算》Python提供了多种并行处理的方式,其中concurrent.futures模块的ProcessPoolExecu... 目录简介完整代码示例代码解释1. 导入必要的模块2. 定义处理函数3. 主函数4. 生成数字列表5.

Python中help()和dir()函数的使用

《Python中help()和dir()函数的使用》我们经常需要查看某个对象(如模块、类、函数等)的属性和方法,Python提供了两个内置函数help()和dir(),它们可以帮助我们快速了解代... 目录1. 引言2. help() 函数2.1 作用2.2 使用方法2.3 示例(1) 查看内置函数的帮助(

Python虚拟环境与Conda使用指南分享

《Python虚拟环境与Conda使用指南分享》:本文主要介绍Python虚拟环境与Conda使用指南,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、python 虚拟环境概述1.1 什么是虚拟环境1.2 为什么需要虚拟环境二、Python 内置的虚拟环境工具

Python实例题之pygame开发打飞机游戏实例代码

《Python实例题之pygame开发打飞机游戏实例代码》对于python的学习者,能够写出一个飞机大战的程序代码,是不是感觉到非常的开心,:本文主要介绍Python实例题之pygame开发打飞机... 目录题目pygame-aircraft-game使用 Pygame 开发的打飞机游戏脚本代码解释初始化部

Python pip下载包及所有依赖到指定文件夹的步骤说明

《Pythonpip下载包及所有依赖到指定文件夹的步骤说明》为了方便开发和部署,我们常常需要将Python项目所依赖的第三方包导出到本地文件夹中,:本文主要介绍Pythonpip下载包及所有依... 目录步骤说明命令格式示例参数说明离线安装方法注意事项总结要使用pip下载包及其所有依赖到指定文件夹,请按照以