繁体   English   中英

如何使用 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():
    ...

Pytest 具有skip 和skipif 装饰器,类似于Python unittest 模块(使用skipskipIf ),可在此处的文档中找到

可以在此处找到链接中的示例:

@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():
    ...

skip装饰器将完成这项工作:

@pytest.mark.skip(reason="no way of currently testing this")
def test_func_one():
    # ...

reason参数是可选的,但指定为什么跳过测试总是一个好主意)。

如果满足某些特定条件,还有skipif()允许禁用测试。


这些装饰器可以应用于方法、函数或类。

跳过模块中的所有测试,请定义一个全局pytestmark变量:

# test_module.py
pytestmark = pytest.mark.skipif(...)

我不确定它是否已弃用,但您也可以在测试中使用pytest.skip函数:

def test_valid_counting_number():
     number = random.randint(1,5)
     if number == 5:
         pytest.skip('Five is right out')
     assert number <= 3

您可以标记与测试skipskipif当你想跳过一个测试装饰pytest

跳过测试

@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标记来指示您希望测试失败。

即使您怀疑测试会失败,您也可能希望运行该测试。 对于这种情况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'是一个关键字。

有关更多信息,请参阅此链接

更多关键字表达式的例子:参考这个答案

您可以通过自定义 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"

如何组合多个标记

官方文档中的更多示例

如果您对测试用例进行良好的逻辑分离,它看起来会更方便。

如果你想放置条件,那么使用skipIf else skip

例子 -

@pytest.mark.skipif(sys.platform.startswith("mac"), reason = str("If platform is mac ignore this test"))
def test_abc() -> None:
    pass

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM