这玩意我也写过一个,看起来差不多,写完就撂那儿了......
https://github.com/zhaixuwen/simple-mock
ChatGPT 给出了个方案,我整合起来代码如下。这样的确可以捕获到 Timeout 异常,只是结合到业务场景中略显复杂,如果有简洁的方案就更好了
import time
import pytest
from concurrent.futures import ThreadPoolExecutor, TimeoutError as FuturesTimeoutError
@ pytest.mark.parametrize("s", [1, 3, 1])
def test_timeout(s):
def do_test(t):
time.sleep(t)
assert True
timeout_limit = 2
result = run_with_timeout(do_test, timeout_limit, s)
if result == "Timeout occurred":
pytest.fail("Test time out")
def run_with_timeout(func, timeout, *args, **kwargs):
with ThreadPoolExecutor(max_workers=1) as executor:
try:
future = executor.submit(func, *args, **kwargs)
return future.result(timeout=timeout)
except FuturesTimeoutError:
return "Timeout occurred"
感谢你的回复,我尝试修改代码,将 parametrize 放在 timeout 上面,仍无法捕获到 Timeout 异常
@ pytest.mark.parametrize("s", [1, 3, 1])
@ pytest.mark.timeout(2)
def test_timeout(s):
try:
do_test(s)
except Exception as e:
print(e)
pytest.fail('Test timeout!')
def do_test(t):
time.sleep(t)
assert True
根据我目前的工作情况回答下:
1、大多数是按照日构建,少部分会按小时或分钟构建,相当于承担了一部分监控职能
2、接口自动化发现的问题其实很少,要么是服务挂了,要么是响应的内容发生了变动
3、如果已知接口有变动,会提交做维护,大多是构建失败的时候人工确认再调整
终于来了