BDD - Python Behave Runner Script
BDD - Python Behave Runner Script
引言
通过终端命令执行 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 subprocess
def run_behave(feature, tags, no_capture, html_report):
behave_command = ["behave"]
# Include feature if provided
if feature:
behave_command.append(feature)
# Include tags if provided
if tags:
behave_command.extend(["--tags", tags])
# Include --no-capture option
if no_capture:
behave_command.append("--no-capture")
# Include html report
if 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 needed
if __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: smoke
Before 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: regression
After 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.py
from behave import __main__ as behave_main
def 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 和 tags
default_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_behave
behave_main.run_behave(behave_args)
File "C:\Users\xxx\AppData\Local\Programs\Python\Python39\lib\site-packages\behave\__main__.py", line 67, in run_behave
if config.version:
AttributeError: 'list' object has no attribute 'version'
PS C:\Automation\Test> Behave --version
behave 1.2.6
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!