测试框架pytest教程(9)跳过测试skip和xfail

skip无条件跳过

使用装饰器

@pytest.mark.skip(reason="no way of currently testing this")
def test_example(faker):
    print("nihao")
    print(faker.words())

测试框架pytest教程(9)跳过测试skip和xfail_第1张图片

 方法内部调用

满足条件时跳过


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

测试框架pytest教程(9)跳过测试skip和xfail_第2张图片

 内部调用

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

测试框架pytest教程(9)跳过测试skip和xfail_第3张图片

 

你可能感兴趣的:(pytest,pytest,python,开发语言)