skip无条件跳过
使用装饰器
@pytest.mark.skip(reason="no way of currently testing this")
def test_example(faker):
print("nihao")
print(faker.words())
方法内部调用
满足条件时跳过
def test_example():
a=1
if a>0:
pytest.skip("unsupported configuration")
skipif满足条件跳过
import sys
@pytest.mark.skipif(sys.version_info < (3, 10), reason="requires python3.10 or higher")
def test_function():
...
xfail预期失败
装饰器用法
@pytest.mark.xfail
def test_function():
...
内部调用
def test_function():
if not valid_config():
pytest.xfail("failing configuration (but should work)")
参数化时使用skip和xfail
import pytest
@pytest.mark.parametrize(
("n", "expected"),
[
(1, 2),
pytest.param(1, 0, marks=pytest.mark.xfail),
pytest.param(1, 3, marks=pytest.mark.xfail(reason="some bug")),
(2, 3),
(3, 4),
(4, 5),
pytest.param(
10, 11, marks=pytest.mark.skipif(sys.version_info >= (3, 0), reason="py2k")
),
],
)
def test_increment(n, expected):
assert n + 1 == expected
文章来源:https://www.toymoban.com/news/detail-665194.html
文章来源地址https://www.toymoban.com/news/detail-665194.html
到了这里,关于测试框架pytest教程(9)跳过测试skip和xfail的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!