测试框架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"])
相关推荐
xing25166 小时前
pytest-html
前端·html·pytest
唐古乌梁海1 天前
【pytest】编写自动化测试用例命名规范README
自动化·pytest
xing25161 天前
pytest下allure
开发语言·python·pytest
IT求学人2 天前
pytest运行用例的常见方式及参数
pytest
开源优测3 天前
什么是pytest.ini及如何在Pytest中应用以提升配置效率
pytest
摸鱼仙人~4 天前
ImportError: cannot import name ‘FixtureDef‘ from ‘pytest‘
conda·pytest·fastapi
唐古乌梁海4 天前
【pytest-jira】自动化用例结合jira初版集成思路
pytest·jira
小码哥说测试6 天前
高效执行自动化用例:分布式执行工具pytest-xdist实战!
自动化测试·软件测试·功能测试·jmeter·pytest·postman·测试工程师
予早6 天前
pytest asyncio 支持插件 pytest-asyncio
pytest