背景:在app自動化測試的時候裂允,有時候會因為網絡問題或者其他未知的問題導致case執(zhí)行失敗(實際程序無bug)
pytest 提供rerunfailures插件解決此問題凿蒜,對失敗的用例進行自動重跑:
安裝:pip install pytest-rerunfailures
import pytest,os
cur_path = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__)))))
result_path = cur_path + '/web/autotest/ui/result/'
result_report = cur_path + '/web/autotest/ui/result_report/'
if not os.path.exists(result_path):os.makedirs(result_path)
if not os.path.exists(result_report):os.makedirs(result_report)
if __name__ == '__main__':
pytest.main([
'-m','smoke', #篩選帶有smoke標記的所有測試用例
"--reruns", "1", #將錯誤的用例重跑1次
"--reruns-delay","1", #重跑case的間隔時間
'--clean-alluredir',
'--alluredir=' + result_path,
'test_suites/',
])
os.system("allure generate --clean "+result_path+" --report-dir "+result_report) #轉換為html
運行結果:失敗的用例重新執(zhí)行了一次
image.png