遇一山,过一山,处处有风景;只要勇敢向前,一路尽是繁花盛开。 | (点击查看→)【测试干货】python/java自动化、持续集成、性能、测开、简历、笔试面试等

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

 

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

应用场景

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

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

 

源码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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成功

1
2
3
4
5
6
7
8
9
10
11
12
#!/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失败

1
2
3
4
5
6
7
8
9
10
11
12
#!/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

1
2
3
4
5
6
7
8
9
10
11
12
#!/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

1
2
3
4
5
6
7
8
9
10
11
12
#!/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

  

结果:

 

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

示例:

1
2
3
4
5
6
7
8
9
10
11
12
#!/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后面代码没执行

 

类级预期失败

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/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

  

结果:

 

模块级预期失败

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/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不会执行方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/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("异常")<br>
@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 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/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

  

结果:

 

posted @   全栈测试笔记  阅读(166)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
历史上的今天:
2022-02-23 答疑记录:jmeter从返回的html中提取指定内容
浏览器标题切换
浏览器标题切换end
点击右上角即可分享
微信分享提示