環(huán)境前提
以下先決條件才能使用pytest-rerunfailures
Python 3.5, 最高 3.8, or PyPy3
pytest 5.0或更高版本
安裝插件
pip3 install pytest-rerunfailures -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
命令行參數(shù):--reruns n(重新運(yùn)行次數(shù))饮潦,--reruns-delay m(等待運(yùn)行秒數(shù))
裝飾器參數(shù):reruns=n(重新運(yùn)行次數(shù))艇肴,reruns_delay=m(等待運(yùn)行秒數(shù))
要重新運(yùn)行所有測(cè)試失敗,使用 --reruns 命令行選項(xiàng)子檀,并指定要運(yùn)行測(cè)試的最大次數(shù):
pytest --reruns 5 -s
知識(shí)點(diǎn)
運(yùn)行失敗的fixture或setup_class也將重新執(zhí)行
命令行添加重新運(yùn)行的延時(shí)
要在兩次重試之間增加延遲時(shí)間祷膳,使用 --reruns-delay 命令行選項(xiàng)陶衅,指定下次測(cè)試重新開始之前等待的秒數(shù)
pytest --reruns 5 --reruns-delay 10 -s
裝飾器里用重新運(yùn)行用例
要將單個(gè)測(cè)試用例添加flaky裝飾器 @pytest.mark.flaky(reruns=5) ,并在測(cè)試失敗時(shí)自動(dòng)重新運(yùn)行直晨,需要指定最大重新運(yùn)行的次數(shù)
代碼如下:reruns_delay表示等待秒數(shù)可不填
@pytest.mark.flaky(reruns=5, reruns_delay=2)
def test_example():
import random
assert random.choice([True, False, False])
注意:
如果指定了用例的重新運(yùn)行次數(shù)搀军,則在命令行添加--reruns對(duì)這些用例是不會(huì)生效的
兼容性測(cè)試
- 不可以和fixture裝飾器一起使用: @pytest.fixture()
- 該插件與pytest-xdist的 --looponfail 標(biāo)志不兼容
- 該插件與核心--pdb標(biāo)志不兼容