如何使用py.test禁用测试?

eri*_*mjl 60 python testing pytest

说我有一堆测试:

def test_func_one():
    ...

def test_func_two():
    ...

def test_func_three():
    ...
Run Code Online (Sandbox Code Playgroud)

是否有一个装饰器或类似的东西,我可以添加到函数,以防止py.test只运行该测试?结果可能看起来像......

@pytest.disable()
def test_func_one():
    ...

def test_func_two():
    ...

def test_func_three():
    ...
Run Code Online (Sandbox Code Playgroud)

我在py.test文档中搜索过类似的内容,但我想我可能会在这里遗漏一些东西.

Ale*_*agh 92

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():
    ...
Run Code Online (Sandbox Code Playgroud)

第一个示例总是跳过测试,第二个示例允许您有条件地跳过测试(当测试依赖于平台,可执行版本或可选库时,这很好.

例如,如果我想检查是否有人为测试安装了库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():
    ...
Run Code Online (Sandbox Code Playgroud)

  • 另一个有用的事情是使用函数调用来跳过。您可以让函数返回包含“condition”和“reason”属性的字典,然后使用“@pytest.mark.skipif(**myFunction())”自动解包函数参数。 (2认同)

ale*_*cxe 17

skip装饰将做的工作:

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

(reason参数是可选的,但指定跳过测试的原因总是一个好主意).

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


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

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

# test_module.py
pytestmark = pytest.mark.skipif(...)
Run Code Online (Sandbox Code Playgroud)


lmi*_*asf 12

您可以标记与测试skip,并skipif当你想跳过一个测试装饰pytest

跳过测试

@pytest.mark.skip(reason="no way of currently testing this")
def test_func_one():
    ...
Run Code Online (Sandbox Code Playgroud)

跳过测试的最简单方法是用skip装饰器标记它,装饰器可能会传递一个可选的reason.

也可以通过调用该pytest.skip(reason)函数在测试执行或设置期间强制跳过。当无法在导入期间评估跳过条件时,这很有用。

def test_func_one():
    if not valid_config():
        pytest.skip("unsupported configuration")
Run Code Online (Sandbox Code Playgroud)

根据条件跳过测试

@pytest.mark.skipif(sys.version_info < (3, 6), reason="requires python3.6 or higher")
def test_func_one():
    ...
Run Code Online (Sandbox Code Playgroud)

如果您想根据条件跳过,则可以skipif改用。在前面的示例中,在 Python3.6 之前的解释器上运行时会跳过测试函数。

最后,如果您因为确定测试失败而想跳过测试,您还可以考虑使用xfail标记来指示您预计测试会失败。


小智 7

即使您怀疑测试会失败,您也可能希望运行该测试。对于这种情况https://docs.pytest.org/en/latest/skipping.html建议使用装饰器@pytest.mark.xfail

@pytest.mark.xfail
def test_function():
    ...
Run Code Online (Sandbox Code Playgroud)

在这种情况下,Pytest 仍会运行您的测试并让您知道它是否通过,但不会抱怨并破坏构建。


Way*_*ner 6

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

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

  • 来寻求 pytest 帮助,留下来作为参考 (4认同)

Sys*_*nin 6

您可以通过自定义 pytest 标记将测试划分为一组测试用例,并仅执行您想要的测试用例。或者相反,运行除另一组之外的所有测试:

@pytest.mark.my_unit_test
def test_that_unit():
...

@pytest.mark.my_functional_test
def test_that_function():
...
Run Code Online (Sandbox Code Playgroud)

然后,仅运行一组单元测试,例如: pytest -m my_unit_test

相反,如果您想运行除一组之外的所有测试: pytest -m "not my_unit_test"

如何组合多个标记

更多示例参见官方文档

如果测试用例有良好的逻辑分离,看起来会更方便。


Ari*_*ury 5

如果您想跳过测试但不想对标记进行硬编码,最好使用关键字表达式来转义它。

pytest test/test_script.py -k 'not test_func_one'
Run Code Online (Sandbox Code Playgroud)

注意:这里的'关键字表达式'基本上是使用pytest(或python)提供的关键字表达一些东西并完成一些事情。我上面的例子,'not'是一个关键字。

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

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