简体   繁体   English

Pytest不会在混合参数化和skipif时跳过测试

[英]Pytest not skipping tests when mixing parametrize and skipif

I have this testing code: 我有这个测试代码:

import pytest


def params():
    dont_skip = pytest.mark.skipif(False, reason="don't skip")
    return [dont_skip("foo"), dont_skip("bar")]


@pytest.mark.skipif(True, reason="always skip")
@pytest.mark.parametrize("param", params())
@pytest.mark.skipif(True, reason="really always skip please")
def test_foo(param):
    assert False

Yet test_foo is not skipped, even though there are skipif decorators attached to test_foo (I tried in both orders, as you can see above): 然而test_foo没有跳过,即使有skipif连接到装饰test_foo (我想在这两个订单,因为你可以看到上面的):

============================= test session starts ==============================
platform darwin -- Python 3.5.0, pytest-2.8.5, py-1.4.31, pluggy-0.3.1
rootdir: /Volumes/Home/Users/Waleed/tmp/python/explainerr/test, inifile: 
collected 2 items

test/test_example.py FF

=================================== FAILURES ===================================
________________________________ test_foo[foo] _________________________________

param = 'foo'

    @pytest.mark.skipif(True, reason="always skip")
    @pytest.mark.parametrize("param", params())
    @pytest.mark.skipif(True, reason="really always skip")
    def test_foo(param):
>       assert False
E       assert False

test/test_example.py:13: AssertionError
________________________________ test_foo[bar] _________________________________

param = 'bar'

    @pytest.mark.skipif(True, reason="always skip")
    @pytest.mark.parametrize("param", params())
    @pytest.mark.skipif(True, reason="really always skip")
    def test_foo(param):
>       assert False
E       assert False

test/test_example.py:13: AssertionError
=========================== 2 failed in 0.01 seconds ===========================

If I change this line 如果我改变这一行

dont_skip = pytest.mark.skipif(False, reason="don't skip")

to

dont_skip = pytest.mark.skipif(True, reason="don't skip")

then it skips the test cases: 然后它跳过测试用例:

============================= test session starts ==============================
platform darwin -- Python 3.5.0, pytest-2.8.5, py-1.4.31, pluggy-0.3.1
rootdir: /Volumes/Home/Users/Waleed/tmp/python/explainerr/test, inifile: 
collected 2 items

test/test_example.py ss

========================== 2 skipped in 0.01 seconds ===========================

How do I get pytest.mark.skipif to work when also using skippable parameters with pytest.mark.parametrize ? 我如何获得pytest.mark.skipif时也使用跳过的参数一起工作pytest.mark.parametrize I'm using Python 3.5.0 with Pytest 2.8.5. 我正在使用Python 3.5.0和Pytest 2.8.5。

The pytest version that you are using is very old. 你使用的pytest版本很老。 I used your code in my env and two tests are skipped at first skipif marks. 我在我的env中使用了你的代码,并且在第一个skipif标记处跳过了两个测试。

python3 -m pytest test_b.py
==================================================================================== test session starts =====================================================================================
platform darwin -- Python 3.6.5, pytest-3.9.1, py-1.7.0, pluggy-0.8.0
rootdir:<redacted>, inifile:
collected 2 items

test_b.py ss                                                                                                                                                                           [100%]

====================================================================================== warnings summary ======================================================================================
test_b.py:9: RemovedInPytest4Warning: Applying marks directly to parameters is deprecated, please use pytest.param(..., marks=...) instead.
For more details, see: https://docs.pytest.org/en/latest/parametrize.html
  @pytest.mark.skipif(True, reason="always skip")
test_b.py:9: RemovedInPytest4Warning: Applying marks directly to parameters is deprecated, please use pytest.param(..., marks=...) instead.
For more details, see: https://docs.pytest.org/en/latest/parametrize.html
  @pytest.mark.skipif(True, reason="always skip")

-- Docs: https://docs.pytest.org/en/latest/warnings.html
=========================================================================== 2 skipped, 2 warnings in 0.03 seconds ============================================================================

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM