簡體   English   中英

pytest-timeout 超時后是否可以繼續執行其他測試?

[英]Is it possible to continue executing other tests after pytest-timeout timeouts?

如果我的其中一個測試超時,我會收到一條超時消息,並且不會執行所有其他測試。 有沒有辦法讓pytest在那之后繼續運行其他測試?

編輯

例子:

添加.py:

import time

def add(a, b):
    if a == 2:
        time.sleep(2)
    elif a == 3:
        time.sleep(4)
    return a + b

test_add.py:

import pytest
from add import add


@pytest.mark.parametrize(
    'a, b, expected_result', [
        (1, 1, 2),
        (2, 2, 4),
        (2, 3, 5),
        (3, 2, 5),
        (4, 1, 5)

    ]
)
def test_add(a, b, expected_result):
    result = add(a, b)
    expected_result = a + b
    assert result == expected_result

通過跑步

(venv) >pytest test_add.py --timeout=3

我明白了

============= test session starts ==============
platform win32 -- Python 3.7.7, pytest-6.0.1, py-1.9.0, pluggy-0.13.1
rootdir: C:\Users\Kosh\PycharmProjects\calcTest
plugins: timeout-1.4.2
timeout: 3.0s
timeout method: thread
timeout func_only: False
collected 5 items                                                                                                                                                                                           

test_add.py ...
++++++++++++++++++++ Timeout ++++++++++++++++++++


~~~~~~~~~~ Stack of MainThread (10588) ~~~~~~~~~~

  File "C:\Users\Kosh\AppData\Local\Programs\Python\Python37\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)

.....

執行了第 1、第 2 和第 3 次測試,第 4 次測試超時,第 5 次未執行。 我正在尋找一種方法來運行它。

pytest-timeout提供了兩種超時方法:信號和線程。

信號方法提供了所需的功能,如果一個測試失敗,它不會停止整個測試套件。 但是Windows不支持信號方法,因此只能使用線程方法。

正如pytest-timeout自述文件所說:

對於每個測試項, pytest-timeout插件會啟動一個計時器線程,該線程將在指定的超時后終止整個過程

所以這是Windows 的預期行為。 而且,據我所知,除了使用 Linux 之外,還沒有找到令人滿意的解決方法 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM