我有一个经常使用的固定装置,有时会出错,并使其无法运行使用它的测试(这是由于硬件错误,因此无法修复)。当失败时,pytest会为使用该固定装置的每个测试打印相同的错误,这意味着我突然从输出中删除了数千行,而实际错误消息位于顶部,而其他错误则隐藏在洪水中。我正在寻找某种选择,以便使我只收到一次错误而不是数百次错误。
一个简单的例子:
我的测试文件:
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
导致异常永远不会被打印-我仍然想第一次看到它之所以进行编辑是因为问题出在一定范围的夹具上:在原始示例中,我展示了一个可以在每次测试中运行的夹具,在这里可以多次打印错误。现在,它表明该错误仅发生一次,却被多次打印。