如何使用 pytest 禁用测试?
How do I disable a test using pytest?
假设我有一堆测试:
def test_func_one():
...
def test_func_two():
...
def test_func_three():
...
是否有装饰器或类似的东西可以添加到函数中以防止 pytest
来自 运行 只是那个测试?结果可能类似于...
@pytest.disable()
def test_func_one():
...
def test_func_two():
...
def test_func_three():
...
skip
decorator 可以胜任:
@pytest.mark.skip(reason="no way of currently testing this")
def test_func_one():
# ...
(reason
参数是可选的,但最好指明跳过测试的原因)。
还有 skipif()
允许在满足某些特定条件时禁用测试。
这些装饰器可以应用于方法、函数或 类。
到skip all tests in a module,定义一个全局pytestmark
变量:
# test_module.py
pytestmark = pytest.mark.skipif(...)
Pytest 有 skip 和 skipif 装饰器,类似于 Python unittest 模块(使用 skip
和 skipIf
),可以在文档中找到 here.
可在此处找到 link 中的示例:
@pytest.mark.skip(reason="no way of currently testing this")
def test_the_unknown():
...
import sys
@pytest.mark.skipif(sys.version_info < (3,3),
reason="requires python3.3")
def test_function():
...
第一个示例总是跳过测试,第二个示例允许您有条件地跳过测试(当测试依赖于平台、可执行版本或可选库时非常好。
例如,如果我想检查是否有人安装了用于测试的库pandas。
import sys
try:
import pandas as pd
except ImportError:
pass
@pytest.mark.skipif('pandas' not in sys.modules,
reason="requires the Pandas library")
def test_pandas_function():
...
我不确定它是否已弃用,但您也可以在测试中使用 pytest.skip
函数:
def test_valid_counting_number():
number = random.randint(1,5)
if number == 5:
pytest.skip('Five is right out')
assert number <= 3
即使您怀疑测试会失败,您也可能想要 运行 测试。对于这种情况 https://docs.pytest.org/en/latest/skipping.html 建议使用装饰器 @pytest.mark.xfail
@pytest.mark.xfail
def test_function():
...
在这种情况下,Pytest 仍会 运行 您的测试并让您知道它是否通过,但不会抱怨和破坏构建。
如果您想跳过测试但又不想对标记进行硬编码,最好使用关键字表达式对其进行转义。
pytest test/test_script.py -k 'not test_func_one'
注意:这里'关键字表达式'基本上是,使用pytest(或python)提供的关键字表达一些东西并完成一些事情。我上面的例子,'not'是一个关键字。
有关详细信息,请参阅此 link。
关键词更多示例expression:Referthis答案
当您想跳过 pytest
中的测试时,您可以使用 skip
和 skipif
装饰器标记测试。
跳过测试
@pytest.mark.skip(reason="no way of currently testing this")
def test_func_one():
...
跳过测试的最简单方法是使用 skip
装饰器对其进行标记,装饰器可能会传递可选的 reason
.
也可以通过调用 pytest.skip(reason)
函数在测试执行或设置期间强制跳过。当无法在导入期间评估跳过条件时,这很有用。
def test_func_one():
if not valid_config():
pytest.skip("unsupported configuration")
根据条件跳过测试
@pytest.mark.skipif(sys.version_info < (3, 6), reason="requires python3.6 or higher")
def test_func_one():
...
如果您想根据条件跳过,则可以改用 skipif
。在前面的例子中,当 运行 在早于 Python3.6.
的解释器上时,测试函数被跳过
最后,如果因为确定测试失败而想跳过测试,您还可以考虑使用 xfail
标记来表明您预计测试会失败。
您可以通过自定义 pytest 标记在一组测试用例上划分测试,并仅执行您想要的那些测试用例。或者相反,运行除了另一组之外的所有测试:
@pytest.mark.my_unit_test
def test_that_unit():
...
@pytest.mark.my_functional_test
def test_that_function():
...
然后,以运行只有一组单元测试为例:
pytest -m my_unit_test
相反,如果你想 运行 所有测试,除了一组:
pytest -m "not my_unit_test"
More examples in official documentation
如果你对测试用例进行良好的逻辑分离,看起来会更方便。
假设我有一堆测试:
def test_func_one():
...
def test_func_two():
...
def test_func_three():
...
是否有装饰器或类似的东西可以添加到函数中以防止 pytest
来自 运行 只是那个测试?结果可能类似于...
@pytest.disable()
def test_func_one():
...
def test_func_two():
...
def test_func_three():
...
skip
decorator 可以胜任:
@pytest.mark.skip(reason="no way of currently testing this")
def test_func_one():
# ...
(reason
参数是可选的,但最好指明跳过测试的原因)。
还有 skipif()
允许在满足某些特定条件时禁用测试。
这些装饰器可以应用于方法、函数或 类。
到skip all tests in a module,定义一个全局pytestmark
变量:
# test_module.py
pytestmark = pytest.mark.skipif(...)
Pytest 有 skip 和 skipif 装饰器,类似于 Python unittest 模块(使用 skip
和 skipIf
),可以在文档中找到 here.
可在此处找到 link 中的示例:
@pytest.mark.skip(reason="no way of currently testing this")
def test_the_unknown():
...
import sys
@pytest.mark.skipif(sys.version_info < (3,3),
reason="requires python3.3")
def test_function():
...
第一个示例总是跳过测试,第二个示例允许您有条件地跳过测试(当测试依赖于平台、可执行版本或可选库时非常好。
例如,如果我想检查是否有人安装了用于测试的库pandas。
import sys
try:
import pandas as pd
except ImportError:
pass
@pytest.mark.skipif('pandas' not in sys.modules,
reason="requires the Pandas library")
def test_pandas_function():
...
我不确定它是否已弃用,但您也可以在测试中使用 pytest.skip
函数:
def test_valid_counting_number():
number = random.randint(1,5)
if number == 5:
pytest.skip('Five is right out')
assert number <= 3
即使您怀疑测试会失败,您也可能想要 运行 测试。对于这种情况 https://docs.pytest.org/en/latest/skipping.html 建议使用装饰器 @pytest.mark.xfail
@pytest.mark.xfail
def test_function():
...
在这种情况下,Pytest 仍会 运行 您的测试并让您知道它是否通过,但不会抱怨和破坏构建。
如果您想跳过测试但又不想对标记进行硬编码,最好使用关键字表达式对其进行转义。
pytest test/test_script.py -k 'not test_func_one'
注意:这里'关键字表达式'基本上是,使用pytest(或python)提供的关键字表达一些东西并完成一些事情。我上面的例子,'not'是一个关键字。
有关详细信息,请参阅此 link。
关键词更多示例expression:Referthis答案
当您想跳过 pytest
中的测试时,您可以使用 skip
和 skipif
装饰器标记测试。
跳过测试
@pytest.mark.skip(reason="no way of currently testing this")
def test_func_one():
...
跳过测试的最简单方法是使用 skip
装饰器对其进行标记,装饰器可能会传递可选的 reason
.
也可以通过调用 pytest.skip(reason)
函数在测试执行或设置期间强制跳过。当无法在导入期间评估跳过条件时,这很有用。
def test_func_one():
if not valid_config():
pytest.skip("unsupported configuration")
根据条件跳过测试
@pytest.mark.skipif(sys.version_info < (3, 6), reason="requires python3.6 or higher")
def test_func_one():
...
如果您想根据条件跳过,则可以改用 skipif
。在前面的例子中,当 运行 在早于 Python3.6.
最后,如果因为确定测试失败而想跳过测试,您还可以考虑使用 xfail
标记来表明您预计测试会失败。
您可以通过自定义 pytest 标记在一组测试用例上划分测试,并仅执行您想要的那些测试用例。或者相反,运行除了另一组之外的所有测试:
@pytest.mark.my_unit_test
def test_that_unit():
...
@pytest.mark.my_functional_test
def test_that_function():
...
然后,以运行只有一组单元测试为例:
pytest -m my_unit_test
相反,如果你想 运行 所有测试,除了一组:
pytest -m "not my_unit_test"
More examples in official documentation
如果你对测试用例进行良好的逻辑分离,看起来会更方便。