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

相关推荐
程序员念姐1 天前
Selenium+Pytest自动化测试框架实战
selenium·测试工具·pytest
Test.X1 天前
学习15天:pytest
学习·pytest
one day3211 天前
pytest 框架学习总结
学习·pytest
天才测试猿2 天前
Pytest自动化测试框架pytest-xdist分布式测试插件
自动化测试·软件测试·分布式·python·测试工具·测试用例·pytest
qq_433716952 天前
压力测试Monkey命令参数和报告分析!
自动化测试·selenium·测试工具·单元测试·pytest·接口测试·压力测试
测试渣2 天前
API自动化测试实战:Postman + Newman/Pytest的深度解析
pytest·postman
qq_433716953 天前
UI自动化测试 —— web端元素获取&元素等待实践!
自动化测试·软件测试·selenium·测试工具·ui·pytest·测试工程师
大霞上仙4 天前
pytest+allure+jenkins 实现接口自动化测试
运维·jenkins·pytest
m0_371356154 天前
【测试框架篇】单元测试框架pytest(5):setup和teardown的详细使用
开发语言·python·单元测试·pytest
T J C4 天前
高效自动化测试:打造Python+Requests+Pytest+Allure+YAML的接口测试框架
开发语言·python·pytest