Behave runner 脚本


我们可以通过运行命令行参数来运行 Behave 测试,或者我们可以创建一个运行脚本。该脚本提供了运行测试和生成相应报告的功能。

我们可以重试并执行失败的测试。此外,在执行整个套件之前,运行脚本能够进行应用程序编程接口 (API) 调用并确保 API 没有问题。

Runner 脚本的步骤


按照下面给出的步骤在 Behave 中成功创建和执行运行器脚本。

步骤 1: 在 features 文件夹中创建一个运行器脚本(runner.py)。

你的电脑上会出现以下画面:

步骤s for Runner Script

第 2 步:运行测试的 Runner Script 实现

运行器脚本可以使用下面提到的代码来运行测试:

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)

第三步:执行运行器脚本

使用命令执行 runner.py 文件 python3 runner.py (如果 Python 版本是 3)。你的计算机上将出现以下屏幕:

Execute the Runner Script

步骤 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)

第五步:执行运行器脚本

使用命令 python3 runner.py --testdir=features 执行 runner.py 文件。

Python3 Runner