【文章末尾给大家留下了大量的福利】
有些项目的测试用例较多,测试用例时需要分布式执行,缩短运行时间。
pytest框架中提供可用于分布式执行测试用例的插件:pytest-parallel、pytest-xdist,接下来我们来学习这两个插件的使用方法。
pytest-parallel
pytest-parallel 同时支持多线程、多进程两种方式执行测试用例。
安装
安装命令:pip install pytest-parallel==0.0.10
注意,虽然最新的版本为 0.1.1,但在windows系统中需要指定安装 0.0.10 版本,否则使用 pytest-parallel 参数执行用例时会报如下错误,其他系统暂未尝试。
AttributeError: Can't pickle local object 'pytest_addoption.<locals>.label_type.<locals>.a_label_type'
参数说明
pytest-parallel 提供参数执行测试用例,示例如下:
if __name__ == '__main__':
pytest.main(['-s', 'testcase/test_case.py', '--workers=1', '--tests-per-worker=3'])
参数说明:
--workers=n
指定运行的进程数为 n,默认为1,windows系统中只能为1--tests-per-worker=m
指定运行的线程数为 m- 若两个参数都指定,则表示启动n个进程,每个进程最多启动m线程执行,总线程数=进程数*线程数
- windows系统中不支持 --workers 取其他值,即只能为1,mac或linux系统中可取其他值