可以通过运行命令行参数来运行 Behave 测试,或者可以创建一个运行脚本。该脚本提供了运行测试和生成相应报告的功能。可以重试并执行失败的测试。此外,在执行整个套件之前,运行脚本能够进行应用程序编程接口 (API) 调用并确保 API 没有问题。
运行器脚本的步骤
按照下面给出的步骤在 Behave 中成功创建和执行运行器脚本。
第 1 步 - 运行测试的运行脚本实现
可以使用下面提到的代码来实现运行器脚本以运行测试 - - 在 features
文件夹中创建一个运行脚本 (runner.py
)。
以下图所示 -
第 2 步 - 运行测试的运行脚本实现
可以使用下面提到的代码来实现运行器脚本以运行测试 -
import subprocess
if __name__ == '__main__':
#command line args along with error capture on failure with check true
s = subprocess.run('behave --no-capture',shell=True, check=True)
第 3 步 - 执行运行脚本
使用命令 python3 runner.py
执行 runner.py
文件(如果 Python 版本为 3)。如下图所示:
第 4 步 - 通过传递命令行参数来参数化运行脚本。
运行测试的运行脚本实现可以如下完成 -
import argparse
import subprocess
if __name__ == '__main__':
p = argparse.ArgumentParser()
#--testdir command line argument added
p.add_argument('--testdir', required=False, help="File path")
a = p.parse_args()
testdir = a.testdir
#complete command
c= f'behave --no-capture {testdir}'
s = subprocess.run(c, shell=True, check=True)
第 5 步 - 执行运行器脚本
使用命令 python3 runner.py --testdir=features
执行 runner.py 文件。