pytest 中的重试机制

发布于:2025-06-30 ⋅ 阅读:(15) ⋅ 点赞:(0)

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()

最佳实践建议

  1. 合理设置重试次数​:通常2-3次足够,过多会掩盖真正问题
  2. 添加延迟​:特别是对于网络请求或资源竞争的情况
  3. 记录重试信息​:使用pytest -v查看哪些测试被重试了
  4. 避免滥用​:重试机制不应替代稳定的测试代码
  5. CI环境特殊处理​:在CI中可增加重试次数
# 示例CI配置
pytest --reruns 3 --reruns-delay 1 --junitxml=report.xml