Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用pytest禁用测试?_Python_Testing_Pytest - Fatal编程技术网

Python 如何使用pytest禁用测试?

Python 如何使用pytest禁用测试?,python,testing,pytest,Python,Testing,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()

假设我有一系列测试:

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.mark.skip(reason="no way of currently testing this")
def test_func_one():
    # ...
reason
参数是可选的,但最好指定跳过测试的原因)

如果满足某些特定条件,还可以禁用测试


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

要执行此操作,请定义全局
pytestmark
变量:

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

Pytest具有skip和skipif修饰符,类似于Python单元测试模块(它使用
skip
skipif
),可以在文档中找到

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

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

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

def test_valid_counting_number():
     number = random.randint(1,5)
     if number == 5:
         pytest.skip('Five is right out')
     assert number <= 3
def test\u valid\u counting\u number():
number=random.randint(1,5)
如果数字==5:
pytest.skip('5是正确的')

assert number即使怀疑测试将失败,您也可能希望运行测试。对于这种情况,建议使用decorator@pytest.mark.xfail

@pytest.mark.xfail
def test_function():
    ...

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

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

pytest test/test_script.py -k 'not test_func_one'
注意:这里的“关键字表达式””基本上是使用pytest(或python)提供的关键字来表达一些东西并完成一些事情。在上面的例子中,“not”是一个关键字

有关更多信息,请参阅此


关键字表达式的更多示例:参考答案当您想在中跳过测试时,您可以使用
跳过
skipif
装饰符标记测试

跳过测试
@pytest.mark.skip(reason=“当前无法测试此项”)
def test_func_one():
...
跳过测试的最简单方法是用
skip
修饰符标记它,该修饰符可以通过可选的
reason

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

def test_func_one():
如果无效,\u config():
跳过(“不支持的配置”)
基于条件跳过测试
@pytest.mark.skipif(sys.version_info<(3,6),reason=“需要python3.6或更高版本”)
def test_func_one():
...
如果要基于条件跳过,则可以使用
skipif
。在前面的示例中,当在早于Python3.6的解释器上运行时,将跳过测试函数


最后,如果您想跳过一个测试,因为您确信它是失败的,那么您也可以考虑使用标记来指示您期望测试失败。

是否有类似于优雅<代码>的跳过 MOCHA(No.js)有?code>it('tests something'…)
-->
it.skip('tests something'…)
,这将禁用特定的测试。它还有一个方便的反面:
.only
只运行该测试,其他什么都不运行。我的意思是,对于
only
部分,您可以通过命令行或您用来初始化测试运行程序的任何东西来执行该操作:对于
It
It.skip
,我相信这里的功能完全涵盖了这一点。来获取pytest帮助,留下来获取引用它引用了什么。我必须know@XChikuX恐怕这比我的时代早得多。但是,我很高兴我现在知道了。什么是“关键词表达”?你是说“关键字抑制”吗?在任何情况下,您可以添加对它的引用吗?但是没有“编辑”,“更新:”或类似的内容-答案应该看起来像是今天写的一样。@PeterMortensen我补充了一点。告诉我这是否有用。
pytest test/test_script.py -k 'not test_func_one'