用例执行失败,重试
# test_demo.pyimport pytestimport randomclass Test1:def test_01(self):assert Truedef test_02(self):# 生成0-1 之间 随机数num = random.random()print("随机值",num)assert num > 0.5def test_03(self):assert True
安装插件 pytest-rerunfailures
https://pypi.org/project/pytest-rerunfailures/
pip install pytest-rerunfailures
运行
pytest --reruns 5 -s -v testdemo.py
- —reruns 5 最大执行次数5次

用例数量很多,如何提高执行效率
xdist 插件
https://pypi.org/project/pytest-xdist/
可以使用并行的方式来运行。
pip install pytest-xdist
代码
import pytest@pytest.mark.parametrize('i',range(1000))def test_i(i):import randomimport timetime.sleep(1)assert i > random.randint(0,1000)
上面的代码 执行,如果单进程执行 至少 1000秒 使用多进程的方式执行。
pytest --tb=no -n auto testdemo.py
- —tb=no 不打印日志
- -n auto 自动选择执行进程数

如果有上下游传参,参数化接口. 运行的时候分配
上下游传参,使用单进程的方式运行.
参数化的接口,使用多进程的方式执行.
