24、pytest通过xfail将测试函数标记为预期失败

官方实例

python 复制代码
# content of test_xfail.py
import pytest
import sys

@pytest.mark.xfail
def test_function():
    print("test_function was invoked.")
    
    
def valid_config():
    return False
    
def test_function_02():
    if not valid_config():
        pytest.xfail("failing configuration (but should work)")

解读与实操

你可以使用xfail标记来表示你希望测试失败。

测试将运行,但失败时不会报告回溯。相反,终端报告将会在"预期失败(XFAIL)或"意外通过"(XPASS)部分中列出来,或者,你也可以在测试或其它setup函数中强制将测试标记为XFAIL。

场景应用

使用reason参数,标记失败原因。查看测试报告时将更直观。

相关推荐
唐古乌梁海2 天前
【pytest】pytest详解-入门到精通
开发语言·python·pytest
我的xiaodoujiao4 天前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 36--二次封装MySQL数据库连接操作
python·学习·测试工具·pytest
兴趣使然黄小黄7 天前
【Pytest】使用Allure生成企业级测试报告
python·pytest
少云清7 天前
【接口测试】4_代码实现 _pytest框架
pytest·接口测试
屋顶那猫7 天前
使用pyinstaller打包pytest项目
python·pytest
zyx没烦恼9 天前
pytest框架
pytest
兴趣使然黄小黄11 天前
【Pytest】Pytest常用的第三方插件
python·pytest
我的xiaodoujiao11 天前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 34--基础知识 9--文件上传功能
前端·python·测试工具·ui·pytest
我一定会有钱11 天前
pytest测试框架基础
python·单元测试·自动化·pytest