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 @   睡觉大王Risen  阅读(422)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
点击右上角即可分享
微信分享提示