Python 如何使用 py.test 禁用测试?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38442897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I disable a test using py.test?
提问by ericmjl
Say I have a bunch of tests:
假设我有一堆测试:
def test_func_one():
...
def test_func_two():
...
def test_func_three():
...
Is there a decorator or something similar that I could add to the functions to prevent from py.test from running just that test? The result might look something like...
是否有装饰器或类似的东西可以添加到函数中以防止 py.test 仅运行该测试?结果可能看起来像......
@pytest.disable()
def test_func_one():
...
def test_func_two():
...
def test_func_three():
...
I have searched for something like this in the py.test docs, but I think I might be missing something here.
我在 py.test 文档中搜索过类似的东西,但我想我可能在这里遗漏了一些东西。
回答by Alexander Huszagh
Pytest has the skip and skipif decorators, similar to the Python unittest module (which uses skip
and skipIf
), which can be found in the documentation here.
Pytest 具有 skip 和 skipif 装饰器,类似于 Python unittest 模块(使用skip
和skipIf
),可在此处的文档中找到。
Examples from the link can be found here:
可以在此处找到链接中的示例:
@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():
...
The first example always skips the test, the second example allows you to conditionally skip tests (great when tests depend on the platform, executable version, or optional libraries.
第一个示例始终跳过测试,第二个示例允许您有条件地跳过测试(当测试依赖于平台、可执行版本或可选库时非常有用。
For example, if I want to check if someone has the library pandas installed for a test.
例如,如果我想检查是否有人安装了用于测试的库 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():
...
回答by alecxe
The skip
decoratorwould do the job:
该skip
装饰将做的工作:
@pytest.mark.skip(reason="no way of currently testing this")
def test_func_one():
# ...
(reason
argument is optional, but it is always a good idea to specify why a test is skipped).
(reason
参数是可选的,但指定为什么跳过测试总是一个好主意)。
There is also skipif()
that allows to disable a test if some specific condition is met.
skipif()
如果满足某些特定条件,还允许禁用测试。
These decorators can be applied to methods, functions or classes.
这些装饰器可以应用于方法、函数或类。
To skip all tests in a module, define a global pytestmark
variable:
要跳过模块中的所有测试,请定义一个全局pytestmark
变量:
# test_module.py
pytestmark = pytest.mark.skipif(...)
回答by Wayne Werner
I'm not sure if it's deprecated, but you can also use the pytest.skip
function inside of a test:
我不确定它是否已弃用,但您也可以在pytest.skip
测试中使用该函数:
def test_valid_counting_number():
number = random.randint(1,5)
if number == 5:
pytest.skip('Five is right out')
assert number <= 3
回答by Ruben Gasparian
You may also want to run the test even if you suspect that test will fail. For such scenario https://docs.pytest.org/en/latest/skipping.htmlsuggests to use decorator @pytest.mark.xfail
即使您怀疑测试会失败,您也可能希望运行该测试。对于这种情况https://docs.pytest.org/en/latest/skipping.html建议使用装饰器@pytest.mark.xfail
@pytest.mark.xfail
def test_function():
...
In this case, Pytest will still run your test and let you know if it passes or not, but won't complain and break the build.
在这种情况下,Pytest 仍会运行您的测试并让您知道它是否通过,但不会抱怨并破坏构建。
回答by Arindam Roychowdhury
If you want to skip the test but not hard code a marker, better use keyword expression to escape it.
如果您想跳过测试但不想对标记进行硬编码,最好使用关键字表达式来转义它。
pytest test/test_script.py -k 'not test_func_one'