pytest简易教程(21):标记为预期失败 - xfail

本文介绍了pytest中的预期失败功能,包括xfail标记装饰器的用法,如何设置条件、原因、运行状态以及异常处理,通过实例展示了在函数、方法、类和模块级别使用xfail进行测试的场景。
摘要由CSDN通过智能技术生成

pytest简易教程汇总,详见https://www.cnblogs.com/uncleyong/p/17982846

应用场景

功能未开发完成,但是用例写了;

环境限制,已经知道会失败,也可以预期失败。

源码

    class _XfailMarkDecorator(MarkDecorator):
        @overload  # type: ignore[override,misc,no-overload-impl]
        def __call__(self, arg: Markable) -> Markable:
            ...

        @overload
        def __call__(
            self,
            condition: Union[str, bool] = ...,
            *conditions: Union[str, bool],
            reason: str = ...,
            run: bool = ...,
            raises: Union[Type[BaseException], Tuple[Type[BaseException], ...]] = ...,
            strict: bool = ...,
        ) -> MarkDecorator:
            ...

方法:xfail(condition=None, reason=None, raises=None, run=True, strict=False)

常用参数:

  • condition:预期失败的条件
  • reason:失败的原因
  • run:布尔值,是否运行
  • raises:抛出某类型异常,和用例中raise的异常类型一样,结果就是FAILED,否则结果是XFAIL
  • strict,默认是False,strict=False,断言成功结果是XPASS,断言失败结果是XFAIL;strict=True,断言成功结果是FAILED,断言失败结果是XFAIL 

使用方法:
  @pytest.mark.xfail(condition, reason="xxx" )

函数/方法级预期失败

示例一:assert成功

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : 韧
# @wx :ren168632201
# @Blog :https://www.cnblogs.com/uncleyong/

import pytest

@pytest.mark.xfail
def test_case1():
    print("代码开发中")
    assert 1==1

结果:X表示预期失败

示例二:assert失败

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : 韧
# @wx :ren168632201
# @Blog :https://www.cnblogs.com/uncleyong/

import pytest

@pytest.mark.xfail
def test_case2():
    print("代码开发中")
    assert 1==2

结果:

示例三:condition为true

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : 韧
# @wx :ren168632201
# @Blog :https://www.cnblogs.com/uncleyong/

import pytest

@pytest.mark.xfail(1==1, reason="代码开发中")
def test_case3():
    print("---xfail")
    assert 1==1

结果:

示例四:condition为false

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : 韧
# @wx :ren168632201
# @Blog :https://www.cnblogs.com/uncleyong/

import pytest

@pytest.mark.xfail(1==2, reason="代码开发中")
def test_case3():
    print("---xfail")
    assert 1==1

结果:

函数/方法执行过程中预期失败

示例:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : 韧
# @wx :ren168632201
# @Blog :https://www.cnblogs.com/uncleyong/

import pytest

def test_case4():
    pytest.xfail("代码开发中")
    print("---xfail")
    assert 1 == 1

结果:pytest.xfail后面代码没执行

类级预期失败

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : 韧
# @wx :ren168632201
# @Blog :https://www.cnblogs.com/uncleyong/
import pytest


@pytest.mark.xfail(reason="当前环境没法测试")
class Test01:
    def test_b(self):
        print("---test_b")
        assert 1==1

    def test_a(self):
        print("---test_a")
        assert 1==2

结果:

模块级预期失败

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : 韧
# @wx :ren168632201
# @Blog :https://www.cnblogs.com/uncleyong/
import pytest

pytestmark = pytest.mark.xfail(reason="当前环境没法测试")
class Test01:
    def test_b(self):
        print("---test_b")
        assert 1==1

    def test_a(self):
        print("---test_a")
        assert 1==2

结果:

xfail方法run参数

默认是True

run=False不会执行方法

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : 韧
# @wx :ren168632201
# @Blog :https://www.cnblogs.com/uncleyong/
import pytest


@pytest.mark.xfail()
def test_c():
    print("---test_c")
    assert 1==1
@pytest.mark.xfail(run=True)
def test_b():
    print("---test_b")
    assert 1==1

@pytest.mark.xfail(run=False)
def test_a():
    print("---test_a")
    raise Exception("异常")

结果:

xfail方法raises参数

raises:抛出某类型异常,和用例中raise的异常类型一样,结果就是FAILED,否则结果是XFAIL

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : 韧
# @wx :ren168632201
# @Blog :https://www.cnblogs.com/uncleyong/
import pytest


@pytest.mark.xfail
def test_d():
    print("---test_d")
    raise Exception("异常")

@pytest.mark.xfail(reason="异常了")
def test_c():
    print("---test_c")
    raise Exception("异常")
@pytest.mark.xfail(raises=RuntimeError) def test_b(): print("---test_b") raise RuntimeError("运行时异常") @pytest.mark.xfail(raises=RuntimeError) def test_a(): print("---test_a") raise Exception("异常")

结果:

xfail方法strict参数

strict默认是False,strict=False,断言成功结果是XPASS,断言失败结果是XFAIL;strict=True,断言成功结果是FAILED,断言失败结果是XFAIL 

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : 韧
# @wx :ren168632201
# @Blog :https://www.cnblogs.com/uncleyong/
import pytest


@pytest.mark.xfail
def test_f():
    print("---test_f")
    assert 1==1
@pytest.mark.xfail
def test_e():
    print("---test_e")
    assert 1==2

@pytest.mark.xfail(strict=False)
def test_d():
    print("---test_d")
    assert 1==1
@pytest.mark.xfail(strict=False)
def test_c():
    print("---test_c")
    assert 1==2
@pytest.mark.xfail(strict=True)
def test_b():
    print("---test_b")
    assert 1==1

@pytest.mark.xfail(strict=True)
def test_a():
    print("---test_a")
    assert 1==2

结果:

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值