skip无条件跳过
使用装饰器
python
@pytest.mark.skip(reason="no way of currently testing this")
def test_example(faker):
print("nihao")
print(faker.words())
方法内部调用
满足条件时跳过
python
def test_example():
a=1
if a>0:
pytest.skip("unsupported configuration")
skipif满足条件跳过
python
import sys
@pytest.mark.skipif(sys.version_info < (3, 10), reason="requires python3.10 or higher")
def test_function():
...
xfail预期失败
装饰器用法
python
@pytest.mark.xfail
def test_function():
...
内部调用
python
def test_function():
if not valid_config():
pytest.xfail("failing configuration (but should work)")
参数化时使用skip和xfail
python
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