用例执行失败,重试

  1. # test_demo.py
  2. import pytest
  3. import random
  4. class Test1:
  5. def test_01(self):
  6. assert True
  7. def test_02(self):
  8. # 生成0-1 之间 随机数
  9. num = random.random()
  10. print("随机值",num)
  11. assert num > 0.5
  12. def test_03(self):
  13. assert True

安装插件 pytest-rerunfailures
https://pypi.org/project/pytest-rerunfailures/

  1. pip install pytest-rerunfailures

运行

  1. pytest --reruns 5 -s -v testdemo.py
  • —reruns 5 最大执行次数5次

image.png

用例数量很多,如何提高执行效率

xdist 插件

https://pypi.org/project/pytest-xdist/

可以使用并行的方式来运行。

  1. pip install pytest-xdist

代码

  1. import pytest
  2. @pytest.mark.parametrize('i',range(1000))
  3. def test_i(i):
  4. import random
  5. import time
  6. time.sleep(1)
  7. assert i > random.randint(0,1000)

上面的代码 执行,如果单进程执行 至少 1000秒 使用多进程的方式执行。

  1. pytest --tb=no -n auto testdemo.py
  • —tb=no 不打印日志
  • -n auto 自动选择执行进程数

image.png

如果有上下游传参,参数化接口. 运行的时候分配

上下游传参,使用单进程的方式运行.
参数化的接口,使用多进程的方式执行.