python - pytest.config.getoption 替代方案失败

标签 python python-3.x pytest

我的设置是这样的; 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.pypytest --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/

相关文章:

Python Pandas 添加文件名列 CSV

python - 根据特定索引处的嵌套列表中的元素过滤列表

python - 使用pytest的parametrize,如果一个测试用例失败,我如何跳过剩余的测试?

python - 加速矩阵计算(循环子数组)[numpy]

python - [nn.nn] 或 [nn] 的正则表达式,具有更正的分组

python - 如何打破 Python 中的嵌套 for 循环?

emacs 中的 python 3

Python 3.x - 使用代表整数的符号创建垂直列表

python - 使用 pyspark dataframe : how to mock up . repartition() 链式函数测试代码时?

automated-tests - 剧作家派森. - 如何检查元素是否隐藏