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

相关推荐
久久学姐6 天前
Python+Playwright+Pytest+BDD,用FSM打造高效测试框架
python·pytest·bdd·playwright·fsm
小白上线*^_^*6 天前
Pytest 自动化测试框架速通指南(二)
软件测试·pytest·python测试框架·ai测试基础
Logintern097 天前
pytest的AST 断言重写
pytest
Tinyfacture8 天前
接口自动化之添加商品(pytest)
python·测试工具·自动化·pytest
ji_shuke10 天前
从零深入:基于 Playwright + Pytest + Allure 的企业级 Web 端到端自动化测试框架实战
前端·自动化测试·docker·jenkins·pytest·allure·playwright
祉猷并茂,雯华若锦11 天前
Selenium+Pytest自动化测试框架实战
selenium·测试工具·pytest
祉猷并茂,雯华若锦13 天前
Pytest+Selenium自动化测试:失败自动截图集成Allure报告
selenium·测试工具·pytest
我的xiaodoujiao16 天前
API 接口自动化测试详细图文教程学习系列32--Allure测试报告2
python·学习·测试工具·pytest
一孤程19 天前
Pytest+Selenium搭建自动化框架-保姆级实战教程
selenium·自动化·pytest
名字还没想好☜23 天前
用 pytest fixture 组织可维护的测试:告别一堆重复的 setUp
数据库·python·oracle·pytest·测试