Believe in yourself.

Pytest学习笔记(五) xfail标记预期失败的测试用例

使用xfail标记希望的测试用例失败,会运行此测试用例,但是在报告中会将其列在“预期失败”(XFAIL)或“意外传递”(XPASS)部分,如下:

import pytest


@pytest.mark.xfail(reason="这个用例实际返回结果与预期不一致")
def test_001():
    assert 1 == 2


@pytest.mark.xfail(reason="这个用例实际返回结果与预期一致")
def test_002():
    assert 8 == 8


def test_003():
    assert 6 == 6

在命令行中执行pytest -vrs,查看结果:

 还可以在测试用例中用xfail强制标识测试预期失败:

def test_function():
    if not valid_config():
        pytest.xfail("failing configuration (but should work)")

 各参数含义:

1、strict 参数设置为True, 如果出现xpass,测试用例的结果将视为失败

如下:

import pytest


@pytest.mark.xfail(strict=True,reason="这个用例实际返回结果与预期不一致")
def test_001():
    assert 1 == 2


@pytest.mark.xfail(strict=True,reason="这个用例实际返回结果与预期一致")
def test_002():
    assert 8 == 8


def test_003():
    assert 6 == 6

在命令行中执行pytest -vrs,查看结果:

 

 strict参数也可以配置到pytest.ini文件中

[pytest]
xfail_strict=true

2、run参数,不运行xfail标记的用例,但是报告中会将其列在“预期失败”(XFAIL)部分

如下:

import pytest


@pytest.mark.xfail(reason="这个用例实际返回结果与预期不一致")
def test_001():
    assert 1 == 2


@pytest.mark.xfail(run=False,reason="这个用例实际返回结果与预期一致")
def test_002():
    print("此条用例不执行")
    assert 8 == 8


def test_003():
    assert 6 == 6

执行结果:

 3、--runxfail  忽略xfail

命令行执行时指定--runxfail,会导致所有的xfail标识不生效。

pytest  --runxfail

 

posted @ 2020-12-09 16:14  eastonliu  阅读(460)  评论(0编辑  收藏  举报