pytest-ordering
提供了使测试用例按顺序执行的方法
安装
pip install pytest-ordering
使用方法
在测试用例前加装饰器@pytest.mark.run(order=1)
例子:
import pytest
@pytest.mark.run(order=2)
def test_foo():
assert True
@pytest.mark.run(order=1)
def test_bar():
assert True
注意
order后参数也可用负数表示
pytest-dependency
这个pytest插件管理测试的依赖项。它允许将某些测试标记为与其他测试相关。如果任何依赖项确实失败或已被跳过,则将跳过这些测试。
安装
pip install pytest-dependency
使用方法
在测试用例前加装饰器@pytest.mark.dependency(depends=["test_01"])
例子:
import pytest
@pytest.mark.dependency()
def test_01(test):
assert False
@pytest.mark.dependency(depends=["test_01"])
def test_02(test):
print("执行测试2")
执行结果:
test_01失败,test_02跳过
pytest-xdist
pytset-xdist
是一款分布式执行插件,可以多个CPU或主机执行,这款插件允许用户将测试并发执行(进程级并发),插件是动态决定测试用例执行顺序的,为了保证测试能在各个独立的线程里正确的执行,应该保证测试用例的独立性。
安装
pip instal pytest-xdist
使用方法
多个CPU并行执行用例,需要在pytest
后面添加-n
参数,如果参数为auto
,会自动检测系统的CPU数目。如果参数为数字,则指定运行测试的处理器进程数。
pytest -n auto
pytest -n [num]
pytest-html
在执行完pytest测试用例后,可以用pytest-html生成HTML格式的测试报告。
安装
pip install pytest-html
使用方法
pytest --html=path/to/html/report.html
//结合 pytest-xdist使用
pytest -v -s -n 3 --html=report.html --self-contained-html
//如果不添加--self-contained-html参数 生成的报告的CSS文件使独立的,不易分享