【发布时间】: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