【问题标题】:Why pytest quit once an unit test(@pytest.mark.timeout(3)) timeout?为什么 pytest 一旦单元测试(@pytest.mark.timeout(3))超时就退出?
【发布时间】:2019-03-16 03:19:51
【问题描述】:

我安装了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"

现在的问题是,我在 Windows 7 中运行这段代码,一旦第二次超时,测试将停止,第三次单元测试没有运行。

我有如下日志:

D:\dev\pytestlog>pytest

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

平台 win32 -- Python 3.6.4、pytest-3.8.2、py-1.5.3、pluggy-0.7.1 根目录:D:\dev\pytestlog,inifile: 插件:timeout-1.3.2,instafail-0.4.0 收集了 3 件物品

test_log.py .

++++++++++++++++++++++++++超时++++++++++++++++++++++ ++

~~~~~~~~~~~~~~ MainThread 堆栈 (17636) ~~~~~~~~~~~~~~~~~

文件“c:\python36-32\lib\runpy.py”,第 193 行,在 _run_module_as_main "ma​​in", mod_spec) _run_code 中的文件“c:\python36-32\lib\runpy.py”,第 85 行 执行(代码,run_globals)

...(这里的日志太多了)

文件“D:\dev\pytestlog\test_log.py”,第 15 行,在 test_02 time.sleep(4)

++++++++++++++++++++++++++超时++++++++++++++++++++++ ++

D:\dev\pytestlog>

【问题讨论】:

  • 我想知道单元测试如何超时?你是否正确地模拟了你的依赖关系?
  • 您好 Klaus,您可以看到 pypi.org/project/pytest-timeout,使用 @pytest.mark.timeout 可以将单个测试标记为超时
  • 我的意思是:一个单元测试不应该花费很长时间,如果它等待某些东西那么它不是正确的单元测试,因为它有依赖关系。
  • 是的克劳斯,在某些情况下你是对的,pytest 是一个单元测试框架,它还可以做更多的事情,在我的项目中,一些测试需要访问某些设备,默认情况下测试会一次又一次地尝试(最多 5 分钟)如果设备不可访问,我想在合理的时间内(如 30 秒)使这些测试失败,以缩短整个测试执行时间。
  • @KlausD。 Pytest 最初是一个单元测试框架,但它可以用于“更高级别的测试”,如功能测试。例如,请参阅这篇博文:patricksoftwareblog.com/…(所以超时是有意义的)

标签: python unit-testing pytest


【解决方案1】:

作为替代方案,您可以使用超时装饰器,如以下链接所示:

timeout decorator in windows

测试代码应如下所示:

# content of test_log.py
import time
from timeout import timeout

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

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

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

【讨论】:

    【解决方案2】:

    第三个单元测试没有运行。第一个错误退出

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

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

    带鼻子2

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

    nose2 -F
    

    用鼻子测试

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

    运行所有测试,不考虑错误

    pytest -v --continue-on-collection-errors test_log.py 
    
    --continue-on-collection-errors
                        Force test execution even if collection errors occur.
    

    检查你的 pytest 配置,默认是在第一个错误时退出

    【讨论】:

      【解决方案3】:

      答案是使用documentation中描述的signal方法

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

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-14
      • 1970-01-01
      • 1970-01-01
      • 2015-11-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多