pip install pytest-rerunfailures
参考文档: https://pypi.org/project/pytest-rerunfailures/
使用–reruns num,num是要重跑的次数
pytest --reruns 5
比如如下在test_simpleexample.py中的测试用例:
import pytest
import logging
LOG=logging.getLogger(__name__)
def inc(x):
return x+1
a=0
@pytest.mark.skipif(a>1,reason="a>1")
def test_inc():
assert inc(5)==6
def test_inc2():
assert inc(3)==7
@pytest.mark.mock
def test_mock_01():
assert inc(1)==2
@pytest.mark.mock
def test_mock_02():
assert inc(3)==4
@pytest.mark.regression
def test_regression_01():
assert inc(3)==4
想要失败用例重跑,就可以使用如下命令:
pytest -v -s --reruns 5 test_simpleexample.py
当测试用例test_inc2测试结果是Failed,就会直接再跑5次
运行结果:
test_simpleexample.py::test_inc PASSED
test_simpleexample.py::test_inc2 RERUN
test_simpleexample.py::test_inc2 RERUN
test_simpleexample.py::test_inc2 RERUN
test_simpleexample.py::test_inc2 RERUN
test_simpleexample.py::test_inc2 RERUN
test_simpleexample.py::test_inc2 FAILED
test_simpleexample.py::test_mock_01 PASSED
test_simpleexample.py::test_mock_02 PASSED
test_simpleexample.py::test_regression_01 PASSED
使用–only-rerun参数
比如:只重跑失败原因是AssertionError的测试用例
pytest -v -s --reruns 5 --only-rerun AssertionError test_simpleexample.py
如果需要满足多个表达式
pytest --reruns 5 --only-rerun AssertionError --only-rerun ValueError
使用–rerun-except
pytest --reruns 5 --rerun-except AssertionError
@pytest.mark.flaky(reruns=5)
import pytest
import logging
LOG=logging.getLogger(__name__)
def inc(x):
return x+1
a=0
@pytest.mark.skipif(a>1,reason="a>1")
def test_inc():
assert inc(5)==6
@pytest.mark.flaky(reruns=5)
def test_inc2():
assert inc(3)==7
@pytest.mark.mock
def test_mock_01():
assert inc(1)==2
@pytest.mark.mock
def test_mock_02():
assert inc(3)==4
@pytest.mark.regression
def test_regression_01():
assert inc(3)==4
运行结果:
test_simpleexample.py::test_inc PASSED
test_simpleexample.py::test_inc2 RERUN
test_simpleexample.py::test_inc2 RERUN
test_simpleexample.py::test_inc2 RERUN
test_simpleexample.py::test_inc2 RERUN
test_simpleexample.py::test_inc2 RERUN
test_simpleexample.py::test_inc2 FAILED
test_simpleexample.py::test_mock_01 PASSED
test_simpleexample.py::test_mock_02 PASSED
test_simpleexample.py::test_regression_01 PASSED
这个mark还有reruns_delay和condition参数
@pytest.mark.flaky(reruns=5, reruns_delay=2)
@pytest.mark.flaky(reruns=2,condition=“sys.platform.startswith(‘win32’)”)