在测试执行之前无法获取 pytest 命令行参数?
Can't get pytest command line arguments prior to test execution?
我尝试根据命令行参数的值跳过特定测试。我尝试使用 pytest.config.getoption("--some-custom-argument")
获取参数值,如 related question suggestion in the test files and check the arguments values via skipif
中所述。但是pyest
没有config
。通过 request.config.getoption("--some-custom-argument")
获取参数值似乎只适用于 fixture 函数。我可以在测试执行之前获取命令行参数吗?我可以在 skipif
文件范围级别检查它们?
你可以试试看
@pytest.mark.skipif(pytest.config.option.some-custom-argument=='foo',
reason='i do not want to run this test')
但为什么不使用标记表达式呢?
由于测试是在配置阶段之后和测试收集之前(即也在测试执行之前)收集的,因此 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/Whosebug/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 =============================
如果我没看错问题,你可能想看看这个answer。
它建议使用带有 request
对象的夹具,并从那里读取输入参数值 request.config.getoption("--option_name")
或 request.config.option.name
。
代码片段(归功于ipetrik):
# test.py
def test_name(name):
assert name == 'almond'
# conftest.py
def pytest_addoption(parser):
parser.addoption("--name", action="store")
@pytest.fixture(scope='session')
def name(request):
name_value = request.config.option.name
if name_value is None:
pytest.skip()
return name_value
我尝试根据命令行参数的值跳过特定测试。我尝试使用 pytest.config.getoption("--some-custom-argument")
获取参数值,如 related question suggestion in the test files and check the arguments values via skipif
中所述。但是pyest
没有config
。通过 request.config.getoption("--some-custom-argument")
获取参数值似乎只适用于 fixture 函数。我可以在测试执行之前获取命令行参数吗?我可以在 skipif
文件范围级别检查它们?
你可以试试看
@pytest.mark.skipif(pytest.config.option.some-custom-argument=='foo',
reason='i do not want to run this test')
但为什么不使用标记表达式呢?
由于测试是在配置阶段之后和测试收集之前(即也在测试执行之前)收集的,因此 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/Whosebug/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 =============================
如果我没看错问题,你可能想看看这个answer。
它建议使用带有 request
对象的夹具,并从那里读取输入参数值 request.config.getoption("--option_name")
或 request.config.option.name
。
代码片段(归功于ipetrik):
# test.py
def test_name(name):
assert name == 'almond'
# conftest.py
def pytest_addoption(parser):
parser.addoption("--name", action="store")
@pytest.fixture(scope='session')
def name(request):
name_value = request.config.option.name
if name_value is None:
pytest.skip()
return name_value