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

时间:2015-12-18 01:30:36

标签: python pytest python-3.5

我有这个测试代码:

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

即使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 ===========================

如果我更改此行

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

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

然后它会跳过测试用例:

============================= 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 ===========================

如何在pytest.mark.skipif使用可跳过的参数时让pytest.mark.parametrize工作?我使用Python 3.5.0和Pytest 2.8.5。

1 个答案:

答案 0 :(得分:0)

您使用的pytest版本太旧了。我在环境中使用了您的代码,并且在第一个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 ============================================================================