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实现快速精准抓取网页数据,文中的示例代码简洁易懂,具有一定的借鉴价值,有需要的小伙伴可以了解下... 目录1. 准备工作2. 基础爬虫实现3. 高级功能扩展3.1 抓取文章详情3.2 保存数据到文件4. 完整示例

使用Python实现IP地址和端口状态检测与监控

《使用Python实现IP地址和端口状态检测与监控》在网络运维和服务器管理中,IP地址和端口的可用性监控是保障业务连续性的基础需求,本文将带你用Python从零打造一个高可用IP监控系统,感兴趣的小伙... 目录概述:为什么需要IP监控系统使用步骤说明1. 环境准备2. 系统部署3. 核心功能配置系统效果展

基于Python打造一个智能单词管理神器

《基于Python打造一个智能单词管理神器》这篇文章主要为大家详细介绍了如何使用Python打造一个智能单词管理神器,从查询到导出的一站式解决,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 项目概述:为什么需要这个工具2. 环境搭建与快速入门2.1 环境要求2.2 首次运行配置3. 核心功能使用指

Python实现微信自动锁定工具

《Python实现微信自动锁定工具》在数字化办公时代,微信已成为职场沟通的重要工具,但临时离开时忘记锁屏可能导致敏感信息泄露,下面我们就来看看如何使用Python打造一个微信自动锁定工具吧... 目录引言:当微信隐私遇到自动化守护效果展示核心功能全景图技术亮点深度解析1. 无操作检测引擎2. 微信路径智能获

Python中pywin32 常用窗口操作的实现

《Python中pywin32常用窗口操作的实现》本文主要介绍了Python中pywin32常用窗口操作的实现,pywin32主要的作用是供Python开发者快速调用WindowsAPI的一个... 目录获取窗口句柄获取最前端窗口句柄获取指定坐标处的窗口根据窗口的完整标题匹配获取句柄根据窗口的类别匹配获取句

利用Python打造一个Excel记账模板

《利用Python打造一个Excel记账模板》这篇文章主要为大家详细介绍了如何使用Python打造一个超实用的Excel记账模板,可以帮助大家高效管理财务,迈向财富自由之路,感兴趣的小伙伴快跟随小编一... 目录设置预算百分比超支标红预警记账模板功能介绍基础记账预算管理可视化分析摸鱼时间理财法碎片时间利用财

Python中的Walrus运算符分析示例详解

《Python中的Walrus运算符分析示例详解》Python中的Walrus运算符(:=)是Python3.8引入的一个新特性,允许在表达式中同时赋值和返回值,它的核心作用是减少重复计算,提升代码简... 目录1. 在循环中避免重复计算2. 在条件判断中同时赋值变量3. 在列表推导式或字典推导式中简化逻辑

python处理带有时区的日期和时间数据

《python处理带有时区的日期和时间数据》这篇文章主要为大家详细介绍了如何在Python中使用pytz库处理时区信息,包括获取当前UTC时间,转换为特定时区等,有需要的小伙伴可以参考一下... 目录时区基本信息python datetime使用timezonepandas处理时区数据知识延展时区基本信息

Python位移操作和位运算的实现示例

《Python位移操作和位运算的实现示例》本文主要介绍了Python位移操作和位运算的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1. 位移操作1.1 左移操作 (<<)1.2 右移操作 (>>)注意事项:2. 位运算2.1

使用Python和Pyecharts创建交互式地图

《使用Python和Pyecharts创建交互式地图》在数据可视化领域,创建交互式地图是一种强大的方式,可以使受众能够以引人入胜且信息丰富的方式探索地理数据,下面我们看看如何使用Python和Pyec... 目录简介Pyecharts 简介创建上海地图代码说明运行结果总结简介在数据可视化领域,创建交互式地