背景:
平時在做接口測試的時候,經常會遇到網絡抖動或者環境問題導致測試用例運行失敗,而這個並不是我們想要的結果,我們想要重新運行失敗的測試用例,這個就需要通過插件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次。