为什么pytest在一次单元测试中退出(@pytest.mark.timeout(3) )超时?

2024-10-01 09:28:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我安装pytestpytest-timeout,我按照https://pypi.org/project/pytest-timeout/中的pytest-timeout的说明为每个单元测试设置超时。在

我只希望单元测试在时间用完时失败,然后继续运行其他单元测试。在

请参阅我的单元测试代码:

# content of test_log.py
import time
import pytest

class TestDemo(object):
    @pytest.mark.timeout(60)
    def test_01(self):
        time.sleep(2)
        assert "1"

    @pytest.mark.timeout(3)
    def test_02(self):
        time.sleep(4)
        assert "1"

    @pytest.mark.timeout(3)
    def test_03(self):
        time.sleep(1)
        assert "1"

现在的问题是,我在Windows7中运行这段代码,一旦第二次测试时间用完,测试就会停止,第三次单元测试就不运行了。在

我有如下日志:

D:\dev\pytestlog>;pytest

==============测试会话开始===========

平台win32--Python3.6.4、pytest-3.8.2、py-1.5.3、pluggy-0.7.1 根目录:D:\dev\pytestlog,ini文件: 插件:timeout-1.3.2,instafail-0.4.0 收集3项

试验_日志.py. 在

+++++

~~~~~~~~~~~~~主线程堆栈(17636)~~~~~~~~~~~~~~~

文件“c:\python36-32\lib\runpy.py“,第193行,以主模块形式运行 “main”,mod\u规范) 文件“c:\python36-32\lib\runpy.py“,第85行,运行代码 执行(代码,运行全局)

。。。(这里的日志太多)

文件“D:\dev\pytestlog\test_日志.py“,第15行,在测试?2中 时间。睡觉(四)

+++++

D:\dev\pytestlog>


Tags: 文件代码pydevtestselftimepytest
2条回答

答案是使用signal方法,如documentation中所述

@pytest.mark.timeout(5, method='signal')
def test_foo():
    pass

the 3rd unit test is not run. exit on first error

在pytest出现第一个错误时退出

-x,  exitfirst       exit instantly on first error or failed test.
pytest -v  exitfirst test_log.py

带鼻子2

-F,fail fast在第一个错误或失败后停止测试运行

^{pr2}$

用鼻子测试

 -x,  stop            Stop running tests after the first error or failure

To run all tests, regardless of errors

pytest -v  continue-on-collection-errors test_log.py 

 continue-on-collection-errors
                    Force test execution even if collection errors occur.

检查您的pytest配置,默认值是exit on first error

相关问题 更多 >