背景:
平时在做接口测试的时候,经常会遇到网络抖动或者环境问题导致测试用例运行失败,而这个并不是我们想要的结果,我们想要重新运行失败的测试用例,这个就需要通过插件pytest-rerunfailures来实现了。
安装插件pytest-rerunfailures
pip install pytest-rerunfailures
执行命令重试失败测试用例
pytest test_add.py --reruns NUM # NUM表示重试的次数
举例:
代码参考如下:
# file_name: test_add.py import pytest def test_add01(): print("----------------->>> test_add01") assert 1 def test_add02(): print("----------------->>> test_add02") assert 0 def test_add03(): print("----------------->>> test_add03") assert 1 def test_add04(): print("----------------->>> test_add04") assert 1 if __name__ == '__main__': pytest.main(["-s", "test_add.py"])
执行命令:pytest ./pytest_study/test_add.py --reruns 2 -s (NUM=2表示失败测试用例重试2次,上述代码中只有test_add02()方法会失败)
通过上面的执行结果可以看到,test_add02()方法总共执行了3次,第1次执行失败后,重试了2次。