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参数,标记失败原因。查看测试报告时将更直观。

相关推荐
测试渣8 小时前
测试左移实战:如何在需求阶段拦截80%缺陷?
selenium·pytest·测试覆盖率
一次旅行14 小时前
测开每日AI提效指令(Python+pytest专属)
python·pytest·测试总结
一次旅行19 小时前
接口自动化测试模板
数据库·python·pytest
Kingairy2 天前
Claude Python3+pytest Code Review skill.md
pytest·代码复审
姚青&2 天前
Pytest 参数化与调度执行
pytest
薯条不要番茄酱3 天前
【测试实战篇】“发好论坛”接口自动化测试
python·功能测试·测试工具·单元测试·测试用例·pytest·测试覆盖率
weixin_419658313 天前
当Pytest遇见AI:基于Trae的接口测试用例全自动生成实践
人工智能·python·测试工具·测试用例·pytest·ai编程
@zulnger5 天前
selenium+pytest 自动化测试实战项目(登录升级版)
selenium·测试工具·pytest
我的xiaodoujiao6 天前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 46--撰写 README项目说明文档文件
python·学习·测试工具·pytest
我的xiaodoujiao6 天前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 47--设置Selenium以无头模式运行代码
python·学习·selenium·测试工具·pytest