我有一个频繁使用的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
是一个可能的解决方案,但我仍然希望使用其他装置运行测试pytest.skip
会导致永远无法打印异常——我仍然希望第一次看到它编辑是因为问题出在一个有范围的fixture上:在最初的示例中,我展示了一个fixture,它在每个测试中都运行,在那里多次打印错误是有意义的。现在它显示错误只发生一次,但被打印多次。你知道吗
目前没有回答
相关问题 更多 >
编程相关推荐