python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > pytest 重试机制

浅谈pytest中重试机制的多种方法

作者:追逐此刻

这篇文章主要介绍了浅谈pytest中重试机制,包括常用插件pytest-rerunfailures、灵活插件pytest-retry、自定义钩子及装饰器,具有一定的参考价值,感兴趣的可以了解一下

pytest 提供了多种重试机制来处理测试失败的情况,以下是主要的实现方式及示例:

1. pytest-rerunfailures 插件(最常用)

这是 pytest 最流行的重试机制实现方式。

安装

pip install pytest-rerunfailures

使用方式

命令行参数

pytest --reruns 3  # 对所有失败测试重试3次
pytest --reruns 3 --reruns-delay 2  # 重试3次,每次间隔2秒

标记特定测试

@pytest.mark.flaky(reruns=3)
def test_example():
    assert 1 + 1 == 2

@pytest.mark.flaky(reruns=3, reruns_delay=1)
def test_example_with_delay():
    assert 2 * 2 == 4

混合使用

pytest --reruns 1 --reruns-delay 1 -m flaky

2. pytest-retry 插件(更灵活)

安装

pip install pytest-retry

使用方式

@pytest.mark.retry(tries=3, delay=1)
def test_retry_specific():
    import random
    assert random.choice([True, False])

3. 自定义重试机制

使用 pytest 钩子

def pytest_runtest_makereport(item, call):
    if call.excinfo is not None:
        # 获取重试次数配置
        reruns = getattr(item, "execution_count", 1)
        if reruns > 1:
            # 实现重试逻辑
            pass

使用装饰器

def retry(times=3, delay=1):
    def decorator(func):
        def wrapper(*args, ​**kwargs):
            for i in range(times):
                try:
                    return func(*args, ​**kwargs)
                except AssertionError as e:
                    if i == times - 1:
                        raise
                    time.sleep(delay)
        return wrapper
    return decorator

@retry(times=3, delay=0.5)
def test_custom_retry():
    assert False

4. 条件重试

结合 pytest-rerunfailures 的条件重试:

@pytest.mark.flaky(reruns=3, condition=os.getenv("CI") == "true")
def test_conditional_retry():
    assert some_flaky_operation()

最佳实践建议

# 示例CI配置
pytest --reruns 3 --reruns-delay 1 --junitxml=report.xml

到此这篇关于浅谈pytest中重试机制的多种方法的文章就介绍到这了,更多相关pytest 重试机制内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家! 

您可能感兴趣的文章:
阅读全文