Pytest - 记录一个 pytest-reruns 插件和 pytest-timeout 插件的 兼容性问题

记录一个 pytest-reruns 插件和 pytest-timeout 插件的 兼容性问题

先说结论:
当两个插件一起使用时,需要设置 timeout_func_only=true

问题

接口自动化项目在容器内执行时 reruns 插件闪退,配置的 timeout 时间不生效,导致项目一直在阻塞状态,本文记录下该问题。

看看这个项目构建时长,受得了吗!?

复现

编写测试用例

import pytest
import time

def test_rerun():
    for i in range(5):
        time.sleep(1)
		    print(f"sleep {i + 1}")
    assert 0

验证插件各自都正常工作

$ pytest test.py -raR --reruns 2

Results (15.14s):
       1 failed
         - test.py:14 test_rerun
       2 rerun
       
$ pytest test.py -raR --timeout 3

E     Failed: Timeout >3.0s

两个插件结合使用

$ pytest test.py -sraR \
  --reruns 2 --reruns-delay=2 \
  --timeout==3
  
timeout: 3.0s
timeout method: signal
timeout func_only: False
sleep 1
sleep 2

sleep 1
sleep 2
sleep 3
sleep 4
sleep 5

sleep 1
sleep 2
sleep 3
sleep 4
sleep 5

可以看到重试用例后,超时并没有生效,依然等待 5 秒。

解决

官方仓库内找到解决办法,需要增加一个配置项。

# -o 代表覆盖 pytest.ini 内定义的配置项	
$ pytest test.py -sraR \
  --reruns 2 --reruns-delay=2 \
  --timeout=3 \
  -o timeout_func_only=true  # 需增加配置项

timeout=3 
timeout: 3.0s
timeout method: signal
timeout func_only: True
sleep 1
sleep 2

sleep 1
sleep 2

sleep 1
sleep 2

或者在配置文件内增加

[pytest]
timeout = 5
timeout_func_only=true

作者:ABEELAN

出处:https://www.cnblogs.com/abeelan/p/17203393.html

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

posted @   ABEELAN  阅读(51)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
more_horiz
keyboard_arrow_up light_mode palette
选择主题
点击右上角即可分享
微信分享提示