【PyTest】玩转HTML报告:修改、汉化和优化_pytest html报告设置 errors
    		       		warning:
    		            这篇文章距离上次修改已过443天,其中的内容可能已经有所变动。
    		        
        		                
                
import pytest
 
# 自定义错误信息处理函数
def pytest_configure(config):
    config._metadata.clear()  # 清空元数据
    config._metadata['Python 测试报告'] = '自定义测试报告信息'
 
# 自定义错误信息显示
def pytest_terminal_summary(terminalreporter, exitstatus, config):
    terminalreporter.section('自定义错误信息', yellow=True)
    terminalreporter.line(f"错误信息: 测试未通过,请检查测试结果。", yellow=True)
 
# 修改HTML报告的标题和描述
def pytest_html_report_title(report):
    report.title = "自定义测试报告"
 
def pytest_html_meta_tags(meta):
    meta.append(
        pytesthtml.nodes.Meta(charset="utf-8")
    )
    meta.append(
        pytesthtml.nodes.Meta(http_equiv="X-UA-Compatible", content="IE=edge")
    )
    meta.append(
        pytesthtml.nodes.Meta(name="description", content="这是一个示例测试报告")
    )
    meta.append(
        pytesthtml.nodes.Meta(name="keywords", content="测试,报告,自定义")
    )
    meta.append(
        pytesthtml.nodes.Meta(name="author", content="测试团队")
    )
    meta.append(
        pytesthtml.nodes.Meta(name="viewport", content="width=device-width, initial-scale=1")
    )
 
# 使用上述定义的函数运行pytest测试会生成带有自定义信息的测试报告这个代码示例展示了如何在PyTest中自定义错误信息处理和HTML报告的标题和元数据。这些自定义可以通过编写特定的Pytest插件钩子函数来实现,并且可以根据项目需求进一步扩展和优化。
评论已关闭