Обратите внимание, что у меня более 3000 тестов, и я не могу редактировать каждый тест для добавления маркеров.
Вы можете добавлять маркеры динамически. Пример: добавьте следующий код в ваш conftest.py
:
# conftest.py
import pytest
def pytest_collection_modifyitems(items):
xfail_exceptions = (IndexError, KeyError)
for item in items:
item.add_marker(pytest.mark.xfail(raises=xfail_exceptions))
Теперь все тесты будут автоматически помечены, вызывая только те исключения, которые не перечислены в xfail_exceptions
.
Вы можете расширить это и еще больше параметризовать исключения из командной строки:
import importlib
import pytest
def pytest_addoption(parser):
parser.addoption('--ex', action='append', default=[], help='exception classes to xfail')
def class_from(name):
modname, clsname = name.rsplit('.', 1)
mod = importlib.import_module(modname)
return getattr(mod, clsname)
def pytest_collection_modifyitems(items):
xfail_exceptions = tuple((class_from(name) for name in pytest.config.getoption('--ex')))
if xfail_exceptions:
for item in items:
item.add_marker(pytest.mark.xfail(raises=xfail_exceptions))
Пример использования:
$ pytest --ex builtins.KeyError --ex builtins.IndexError -sv
================================== test session starts ====================================
platform darwin -- Python 3.6.4, pytest-3.7.3, py-1.5.4, pluggy-0.7.1
cachedir: .pytest_cache
rootdir: /Users/hoefling/projects/private/stackoverflow, inifile:
plugins: django-3.4.2
collected 3 items
test_spam.py::test_a xfail
test_spam.py::test_b FAILED
test_spam.py::test_c xfail
======================================== FAILURES =========================================
_________________________________________ test_b __________________________________________
def test_b():
> 1/0 # this test would be ignored
E ZeroDivisionError: division by zero
test_spam.py:5: ZeroDivisionError
========================== 1 failed, 2 xfailed in 0.08 seconds ============================