关键字:--reruns
pytest运行参数--reruns主要用于用例运行失败后,重新运行失败的用例,运行规则为遇到失败的用例,就根据重试次数运行失败的用例,然后在执行剩下的用例。举例如下:
import pytest
class TestPytestFeatures(object):
def test_case_login(self):
num = 1
assert num == 2
def test_case_logout(self):
tmp = ["a", "b"]
assert isinstance(tmp, list)
def test_case_shopping(self):
tmp = ["a", "b"]
assert isinstance(tmp, list)
def test_case_walk(self):
tmp = ["a", "b"]
assert isinstance(tmp, dict)
if __name__ == "__main__":
pytest.main(["--reruns=2", "test_case_pytest_features.py"])
"""
运行结果
============================= test session starts =============================
platform win32 -- Python 3.8.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: E:icp_capphelp
plugins: allure-pytest-2.9.43, forked-1.3.0, rerunfailures-10.2, xdist-2.4.0
collected 4 items
test_case_pytest_features.py RRF..R [100%]R [100%]F [100%]
================================== FAILURES ===================================
_____________________ TestPytestFeatures.test_case_login ______________________
self =
def test_case_login(self):
num = 1
> assert num == 2
E assert 1 == 2
test_case_pytest_features.py:14: AssertionError
______________________ TestPytestFeatures.test_case_walk ______________________
self =
def test_case_walk(self):
tmp = ["a", "b"]
> assert isinstance(tmp, dict)
E AssertionError: assert False
E + where False = isinstance(['a', 'b'], dict)
test_case_pytest_features.py:26: AssertionError
============================== warnings summary ===============================
test_case_pytest_features.py:32
E:icp_capphelptest_case_pytest_features.py:32: DeprecationWarning: invalid escape sequence i
-- Docs: https://docs.pytest.org/en/stable/warnings.html
=========================== short test summary info ===========================
FAILED test_case_pytest_features.py::TestPytestFeatures::test_case_login - as...
FAILED test_case_pytest_features.py::TestPytestFeatures::test_case_walk - Ass...
=============== 2 failed, 2 passed, 1 warning, 4 rerun in 0.14s ===============
"""
若已知某些用例不稳定,则可以使用@pytest.mark.flaky(n)来指定失败用例重试次数。
import pytest
class TestPytestFeatures(object):
@pytest.mark.flaky(2)
def test_case_login(self):
num = 1
assert num == 2
def test_case_logout(self):
tmp = ["a", "b"]
assert isinstance(tmp, list)
def test_case_shopping(self):
tmp = ["a", "b"]
assert isinstance(tmp, list)
def test_case_walk(self):
tmp = ["a", "b"]
assert isinstance(tmp, dict)
if __name__ == "__main__":
pytest.main([ "test_case_pytest_features.py"])
"""
运行结果
============================= test session starts =============================
platform win32 -- Python 3.8.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: E:icp_capphelp
plugins: allure-pytest-2.9.43, forked-1.3.0, rerunfailures-10.2, xdist-2.4.0
collected 4 items
test_case_pytest_features.py RRF..F [100%]
================================== FAILURES ===================================
_____________________ TestPytestFeatures.test_case_login ______________________
self =
@pytest.mark.flaky(2)
def test_case_login(self):
num = 1
> assert num == 2
E assert 1 == 2
test_case_pytest_features.py:15: AssertionError
______________________ TestPytestFeatures.test_case_walk ______________________
self =
def test_case_walk(self):
tmp = ["a", "b"]
> assert isinstance(tmp, dict)
E AssertionError: assert False
E + where False = isinstance(['a', 'b'], dict)
test_case_pytest_features.py:27: AssertionError
============================== warnings summary ===============================
test_case_pytest_features.py:33
E:icp_capphelptest_case_pytest_features.py:33: DeprecationWarning: invalid escape sequence i
-- Docs: https://docs.pytest.org/en/stable/warnings.html
=========================== short test summary info ===========================
FAILED test_case_pytest_features.py::TestPytestFeatures::test_case_login - as...
FAILED test_case_pytest_features.py::TestPytestFeatures::test_case_walk - Ass...
=============== 2 failed, 2 passed, 1 warning, 2 rerun in 0.10s ===============
"""



