当前位置: 首页 > 工具软件 > pytest > 使用案例 >

pytest-pytest插件之失败用例重跑pytest-rerunfailures

戚建华
2023-12-01

安装插件

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

使用mark来标记重跑的次数

@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’)”)

 类似资料: