测试框架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"])
相关推荐
努力搬砖的咸鱼1 天前
从零开始搭建 Pytest 测试框架(Python 3.8 + PyCharm 版)
python·pycharm·pytest
FINE!(正在努力!)4 天前
PyTest框架学习
学习·pytest
程序员杰哥4 天前
接口自动化测试之pytest 运行方式及前置后置封装
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·pytest
测试老哥4 天前
Pytest+Selenium UI自动化测试实战实例
自动化测试·软件测试·python·selenium·测试工具·ui·pytest
水银嘻嘻4 天前
07 APP 自动化- appium+pytest+allure框架封装
python·appium·自动化·pytest
天才测试猿5 天前
接口自动化测试之pytest接口关联框架封装
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·pytest
not coder6 天前
Pytest Fixture 详解
数据库·pytest
not coder6 天前
pytest 常见问题解答 (FAQ)
开发语言·python·pytest
程序员的世界你不懂6 天前
(1)pytest简介和环境准备
pytest
not coder6 天前
Pytest Fixture 是什么?
数据库·oracle·pytest