【问题标题】:Pytest not skipping tests when mixing parametrize and skipif混合参数化和skipif时Pytest不跳过测试
【发布时间】:2016-03-24 15:58:58
【问题描述】:

我有这个测试代码:

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。

【问题讨论】:

  • 这可能是一个错误...?
  • 您的真实代码是否有更复杂的 skipif 表达式,或者它们实际上只是 True 和 False?
  • @pfctdayelise 我的真实代码有更复杂的表达式,可以解析为布尔值,而且它也不起作用。我提出了一个错误报告:github.com/pytest-dev/pytest/issues/1296

标签: python pytest python-3.5


【解决方案1】:

您使用的 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 ============================================================================

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-16
    • 2018-01-22
    • 1970-01-01
    • 2013-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多