Pytest框架 — 15、Pytest的失败重试

1、前言

有时候我们测试执行由于某些原因失败了,想要多执行几次,Pytest可以使用pytest-rerunfailures插件来实现。
安装方式:pip install pytest-rerunfailures

2、使用

(一)命令行或main函数中使用

pytest -v -s ./xxx.py --reruns 2
pytest.main(["-v","-s","xxx.py","--reruns=2"])

(二)全局配置中使用(推荐用法)

pytest.ini配置文件中addopts添加reruns参数

[pytest]
addopts = -s -v --reruns 2 --reruns-delay 2
testpaths = scripts
python_files = test_*.py
python_classes = Test*
python_functions = test*

说明:
--reruns 2 代表重跑次数
--reruns-delay 2 代表重跑间隔,单位秒

示例:

def test_1():
    print("测试1")
    assert True

def test_2():
    print("测试2")
    assert False

def test_3():
    print("测试3")
    assert True


"""
执行结果
mark/reruns/reruns.py::test_1 测试1
PASSED
mark/reruns/reruns.py::test_2 测试2
RERUN
mark/reruns/reruns.py::test_2 测试2
RERUN
mark/reruns/reruns.py::test_2 测试2
FAILED
mark/reruns/reruns.py::test_3 测试3
PASSED
"""
posted @ 2022-08-20 17:44  睡觉大王Risen  阅读(408)  评论(0编辑  收藏  举报