我的设置是这样的; pytest test.py
pytest --first-test test.py
时不执行任何操作执行目标函数 test_skip
.
为了确定是否应该进行某个测试,这是我一直在使用的:
skip_first = pytest.mark.skipif(
not (
pytest.config.getoption("--first-test")
or os.environ.get('FULL_AUTH_TEST')
), reason="Skipping live"
)
@skip_first
def test_skip():
assert_something
现在,
pytest.config.getoption
正在被弃用,我正在尝试更新我的代码。这是我写的:@pytest.fixture
def skip_first(request):
def _skip_first():
return pytest.mark.skipif(
not (
request.config.getoption("--first-test")
or os.environ.get('FULL_AUTH_TEST')),
reason="Skipping"
)
return _skip_first()
# And, to call:
def test_skip(skip_first):
assert 1==2
然而,我是否做
pytest test.py
或 pytest --first-test test.py
, test_skip
将始终执行。但是,skip_first 似乎工作正常 - 插入打印语句显示 skip_first = MarkDecorator(mark=Mark(name='skipif', args=(False,), kwargs={'reason': 'Skipping first'}))
, 当 --first-test
给出,并且 args=(True) 给出时。 (使用第一个设置时观察到同样的事情)。我错过了什么吗??我什至试图返回函数
_skip_first
而不是它在 def skip_first
中的输出但没有区别。使用测试类时,手册上提示我们需要使用
@pytest.mark.usefixtures("fixturename")
但事实证明这也没有用(对于类)。想法?这是我的系统:
platform linux -- Python 3.6.7, pytest-4.0.2, py-1.7.0, pluggy-0.8.0
最佳答案
为了引起SKIP
从 fixture 中,您必须提高 pytest.skip
.这是使用上面代码的示例:
import os
import pytest
@pytest.fixture
def skip_first(request):
if (
request.config.getoption("--first-test")
or os.environ.get('FULL_AUTH_TEST')
):
raise pytest.skip('Skipping!')
# And, to call:
def test_skip(skip_first):
assert 1==2
如果需要,您几乎可以通过执行以下操作来替换原始代码:
@pytest.fixture
def skip_first_fixture(request): ...
skip_first = pytest.mark.usefixtures('skip_first_fixture')
@skip_first
def test_skip(): ...
这是显示此工作的执行:
$ pytest t.py -q
F [100%]
=================================== FAILURES ===================================
__________________________________ test_skip ___________________________________
skip_first = None
def test_skip(skip_first):
> assert 1==2
E assert 1 == 2
E -1
E +2
t.py:16: AssertionError
1 failed in 0.03 seconds
$ pytest t.py --first-test -q
s [100%]
1 skipped in 0.01 seconds
关于python - pytest.config.getoption 替代方案失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55058237/