Pytest的失败用例重试

前言:

  用例执行一次失败后,可能是由于网络、设备、等等其他因素造成,并不一定就是程序的bug。因此我们需要多执行几次,再好定性它为bug。第三方插件,pytest-rerunfailures就提供了失败重跑的能力。

安装:

pip install pytest-rerunfailures

失败重试用法:

  py文件为:

class TestUser(object):

    def test_user_case1(self):
        print("test_user_case1")
        assert 1

    def test_user_case2(self):
        print("test_user_case2")
        assert 1

    def test_user_case3(self):
        print("test_user_case3")
        assert 0
  • 可以在命令行运行时加上--reruns=2,表示失败后重试两次;--reruns-delay 3,表示重试时间隔3秒
    pytest -s --reruns=2 --reruns-delay 3 /scripts/test_user.py

    在执行py文件时,test_user_case3第一次执行失败后为“RERUN”,会在重新执行两次,如果失败才会报“FAILED”

  • 也可以在pytest.ini文件中进行配置,如下:
    [pytest]
    addopts = -s -v --reruns 2 --reruns-delay 3

     

posted @ 2023-04-15 18:24  A熙  阅读(66)  评论(0)    收藏  举报