- 先安装插件pip install pytest-rerunfailures
- test_01.py
import pytest from webTest.Common.logger import logger my_skip = pytest.mark.skipif(1 == 1, reason='自定义的跳过标签') class Test01: def test_1(self): try: assert 1 == 1 except AssertionError as e: logger.error(e) raise e else: logger.info("测试通过") finally: print("测试执行完成") @pytest.mark.skip(reason="使用skip跳过") def test_2(self): assert 1 == 1 @pytest.mark.skipif(1 == 1, reason="使用skipif跳过") def test_3(self): assert 1 == 1 @my_skip def test_4(self): assert 1 == 1 def test_5(self): if(1 == 1): pytest.skip(reason='方法内跳过') else: assert 1 == 0 def test_6(self): assert False
以上代码中的test_6会执行失败
- main.py,--reruns=2表示失败的测试用例会重试2次
import pytest if __name__ == '__main__': pytest.main(['-rs', '--reruns=2'])
执行结果如下