我试图跳过依赖于命令行参数值的特定测试。我尝试使用pytest.config.getoption("--some-custom-argument")
获取参数值,如测试文件中此related question suggestion中所述,并通过skipif
检查参数值。但pyest
没有config
。通过request.config.getoption("--some-custom-argument")
获取参数值似乎只适用于夹具函数。我可以在测试执行之前获得命令行参数,但是我可以在skipif
的文件范围级别检查它们吗?
答案 0 :(得分:1)
由于在配置阶段之后和测试集合之前(即在测试执行之前)收集测试,因此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 =============================
答案 1 :(得分:0)
You can try to do like that
@pytest.mark.skipif(pytest.config.option.some-custom-argument=='foo',
reason='i do not want to run this test')
But why not to use mark expressions?
答案 2 :(得分:0)
如果我理解正确的问题,那么您可能想看看这个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