测试框架pytest教程(8)失败重试-pytest-rerunfailures

`pytest-rerunfailures`是一个pytest插件,用于重新运行失败的测试用例。当测试用例在第一次运行时失败,该插件会自动重新运行指定次数的失败用例,以提高稳定性和减少偶发性错误的影响。

要使用`pytest-rerunfailures`插件,需要按照以下步骤进行安装和配置:

安装插件

在终端中运行以下命令来安装插件:

```

pip install pytest-rerunfailures

```

配置插件

在pytest配置文件中或通过命令行参数来配置插件。以下是一个示例配置:

```

pytest --reruns 2 --reruns-delay 1

```

  • `--reruns n`:指定重试失败用例的次数。在此示例中,失败用例将会重新运行2次。

  • `--reruns-delay se`:指定每次重试之间的延迟时间(秒)。在此示例中,每次重试将会有1秒的延迟。

你也可以将这些配置添加到pytest配置文件中,例如pytest.ini或setup.cfg文件。在配置文件中,可以按照如下方式添加:

pytest

reruns = 2

reruns_delay = 1

运行测试

运行你的pytest测试用例。当有测试用例失败时,插件将会自动重新运行失败用例指定次数。

使用`pytest-rerunfailures`插件可以提高测试用例的稳定性,特别是在遇到偶发性错误或不稳定的环境时。然而,过多的重试可能会掩盖真正的问题,因此需要根据具体情况选择合适的重试次数和延迟时间。

3次重试,一共执行4次

重试包含特定error的用例

$ pytest --reruns 5 --only-rerun AssertionError

重试不包含特定error的用例

$ pytest --reruns 5 --rerun-except AssertionError

单个用例重试

把用例标记为flasky,并配置重试次数

python 复制代码
@pytest.mark.flaky(reruns=5)
def test_example():

    assert False

延迟时间:

复制代码
@pytest.mark.flaky(reruns=5, reruns_delay=2)

添加条件

复制代码
@pytest.mark.flaky(reruns=5, condition=sys.platform.startswith("win32"))

包含或者不包含特定error

复制代码
@pytest.mark.flaky(rerun_except="AssertionError")

包含error类型为list

复制代码
@pytest.mark.flaky(only_rerun=["AssertionError", "ValueError"])
相关推荐
我的xiaodoujiao2 小时前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 24--数据驱动--参数化处理 Excel 文件 1
python·学习·测试工具·pytest
西游音月3 天前
(2)pytest+Selenium自动化测试-环境准备
selenium·测试工具·pytest
我的xiaodoujiao3 天前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 23--数据驱动--参数化处理 Yaml 文件
python·学习·测试工具·pytest
我的xiaodoujiao6 天前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 22--数据驱动--参数化处理 Json 文件
python·学习·测试工具·pytest
胜天半月子7 天前
Python自动化测试 | 快速认识并了解pytest的基本使用
服务器·python·pytest
北珣.11 天前
自动化框架pytest基础
自动化·pytest
程序员杰哥12 天前
Pytest之收集用例规则与运行指定用例
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·pytest
学习3人组12 天前
Python + requests + pytest + allure + Jenkins 构建完整的接口自动化测试框架
python·jenkins·pytest
shao91851615 天前
Gradio全解14——使用Gradio构建MCP的服务器与客户端(4)——Python包命令:uv与uvx实战
pytest·uv·1024程序员节·npx·uvx·uv pip·ruff
我的xiaodoujiao16 天前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 20--PO(POM) 设计模式和用例撰写
python·学习·测试工具·设计模式·pytest