Поскольку тесты собираются после этапа настройки и перед сбором тестов (т. Е. Также перед выполнением теста), pytest.config
доступен на уровне модулей в тестовых модулях.Пример:
# conftest.py
def pytest_addoption(parser):
parser.addoption('--spam', action='store')
# test_spam.py
import pytest
print(pytest.config.getoption('--spam'))
@pytest.mark.skipif(pytest.config.getoption('--spam') == 'eggs',
reason='spam == eggs')
def test_spam():
assert True
Запуск с --spam=eggs
выходами:
$ pytest -vs -rs --spam=eggs
============================== test session starts ================================
platform linux -- Python 3.6.5, pytest-3.4.1, py-1.5.3, pluggy-0.6.0 -- /data/gentoo64/usr/bin/python3.6
cachedir: .pytest_cache
rootdir: /data/gentoo64/home/u0_a82/projects/stackoverflow/so-50681407, inifile:
plugins: mock-1.6.3, cov-2.5.1, flaky-3.4.0
collecting 0 items
eggs
collected 1 item
test_spam.py::test_spam SKIPPED
============================ short test summary info ==============================
SKIP [1] test_spam.py:7: spam == eggs
=========================== 1 skipped in 0.03 seconds =============================