在sessionscoped pytest fixture中,对于每个使用该fixture的测试都会报告异常,我只想在上面看到它

2024-05-15 15:25:50 发布

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

我有一个频繁使用的fixture,它有时会出错,使使用它的测试无法运行(这是由于硬件错误,所以它是不可修复的)。当它失败时,pytest为使用该fixture的每个测试打印相同的错误,这意味着我突然从输出中得到数千行,真正的错误消息在顶部,其他错误隐藏在flood中。我正在寻找一些选项,这样我只得到错误一次,而不是数百次。你知道吗

举个简单的例子:

我的测试文件:

import pytest

@pytest.fixture(scope='session')
def buggy_fixture():
    print("Making buggy fixture")
    raise RuntimeError("Oh no, a bug")

def test_1(buggy_fixture):
    print("test 1")

def test_2(buggy_fixture):
    print("test 2")

输出:

% pytest test_stuff.py
============================= test session starts ==============================
platform linux -- Python 3.5.2, pytest-3.9.3, py-1.7.0, pluggy-0.8.0
benchmark: 3.2.2 (defaults: timer=time.perf_counter disable_gc=False min_rounds=5 min_time=0.000005 max_time=1.0 calibration_precision=10 warmup=False warmup_iterations=100000)
rootdir: /home/test/a, inifile:
plugins: timeout-1.3.3, benchmark-3.2.2, repeat-0.8.0
collected 2 items                                                              

test_stuff.py EE                                                         [100%]

==================================== ERRORS ====================================
___________________________ ERROR at setup of test_1 ___________________________

    @pytest.fixture(scope='session')
    def buggy_fixture():
        print("Making buggy fixture")
>       raise RuntimeError("Oh no, a bug")
E       RuntimeError: Oh no, a bug

test_stuff.py:6: RuntimeError
---------------------------- Captured stdout setup -----------------------------
Making buggy fixture
___________________________ ERROR at setup of test_2 ___________________________

    @pytest.fixture(scope='session')
    def buggy_fixture():
        print("Making buggy fixture")
>       raise RuntimeError("Oh no, a bug")
E       RuntimeError: Oh no, a bug

test_stuff.py:6: RuntimeError
=========================== 2 error in 0.04 seconds ============================

这更接近我想看到的:

% pytest test_stuff.py   
============================= test session starts ==============================
platform linux -- Python 3.5.2, pytest-3.9.3, py-1.7.0, pluggy-0.8.0
benchmark: 3.2.2 (defaults: timer=time.perf_counter disable_gc=False min_rounds=5 min_time=0.000005 max_time=1.0 calibration_precision=10 warmup=False warmup_iterations=100000)
rootdir: /home/test/a, inifile:
plugins: timeout-1.3.3, benchmark-3.2.2, repeat-0.8.0
collected 2 items                                                              

test_stuff.py EE                                                         [100%]

==================================== ERRORS ====================================
_________________________ ERROR at setup of buggy_fixture ______________________

    @pytest.fixture(scope='session')
    def buggy_fixture():
        print("Making buggy fixture")
>       raise RuntimeError("Oh no, a bug")
E       RuntimeError: Oh no, a bug

test_stuff.py:6: RuntimeError
---------------------------- Captured stdout setup -----------------------------
Making buggy fixture
=========================== 2 error in 0.04 seconds ============================

注意事项:

  • pytest.exit是一个可能的解决方案,但我仍然希望使用其他装置运行测试
  • 对fixture中的错误调用pytest.skip会导致永远无法打印异常——我仍然希望第一次看到它
  • 在我们的实际测试中,每个测试都使用相同的夹具,但它是参数化的。这个参数作为测试名称的一部分结束,因此很明显,如果fixture失败,这些测试将无法运行。你知道吗

编辑是因为问题出在一个有范围的fixture上:在最初的示例中,我展示了一个fixture,它在每个测试中都运行,在那里多次打印错误是有意义的。现在它显示错误只发生一次,但被打印多次。你知道吗


Tags: nopytestpytestsessiondef错误fixture