1. 程式人生 > 其它 >pytest之失敗測試用例重試執行(pytest-rerunfailures)

pytest之失敗測試用例重試執行(pytest-rerunfailures)

背景:

平時在做介面測試的時候,經常會遇到網路抖動或者環境問題導致測試用例執行失敗,而這個並不是我們想要的結果,我們想要重新執行失敗的測試用例,這個就需要通過外掛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()方法會失敗)

注意 :

pytest多種執行模式支援疊加執行:

例如同時執行四個程序,且失敗後重跑2次,pytest命令列執行:pytest -n 4 -reruns 2

去期待陌生,去擁抱驚喜。