混合参数化和skipif时Pytest不跳过测试
Posted
技术标签:
【中文标题】混合参数化和skipif时Pytest不跳过测试【英文标题】:Pytest not skipping tests when mixing parametrize and skipif 【发布时间】: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 【参考方案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 ============================================================================
【讨论】:
以上是关于混合参数化和skipif时Pytest不跳过测试的主要内容,如果未能解决你的问题,请参考以下文章
pytest学习和使用10-Pytest中的测试用例如何跳过执行?