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 测试框架(Python 3.8 + PyCharm 版)
python·pycharm·pytest
FINE!(正在努力!)4 天前
PyTest框架学习
学习·pytest
程序员杰哥5 天前
接口自动化测试之pytest 运行方式及前置后置封装
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·pytest
测试老哥5 天前
Pytest+Selenium UI自动化测试实战实例
自动化测试·软件测试·python·selenium·测试工具·ui·pytest
水银嘻嘻5 天前
07 APP 自动化- appium+pytest+allure框架封装
python·appium·自动化·pytest
天才测试猿6 天前
接口自动化测试之pytest接口关联框架封装
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·pytest
not coder7 天前
Pytest Fixture 详解
数据库·pytest
not coder7 天前
pytest 常见问题解答 (FAQ)
开发语言·python·pytest
程序员的世界你不懂7 天前
(1)pytest简介和环境准备
pytest
not coder7 天前
Pytest Fixture 是什么?
数据库·oracle·pytest