allure报告修改默认语言为中文

1、项目根目录创建.py文件,把代码复制进去

python 复制代码
import os
from pathlib import Path


def create_settings_js_file(directory="../pytest_mytt/reports/allures/", filename='settings.js'):
    # 创建或确认目录存在
    Path(directory).mkdir(parents=True, exist_ok=True)

    # 定义JS内容
    js_content = """  
    // 尝试从 localStorage 中获取 ALLURE_REPORT_SETTINGS 的值  
    let allureSettings = JSON.parse(localStorage.getItem('ALLURE_REPORT_SETTINGS'));  

    if (allureSettings) {  
        // 如果能获取到值,则修改 language 属性为 "zh"  
        allureSettings.language = "zh";  
    } else {  
        // 如果获取不到值,则创建一个新对象并设置默认值  
        allureSettings = {  
            "language": "zh",  
            "sidebarCollapsed": false,  
            "sideBySidePosition": [46.83064516129034, 53.16935483870967]  
        };  
    }  

    // 将修改后的对象或新创建的对象存储回 localStorage  
    localStorage.setItem('ALLURE_REPORT_SETTINGS', JSON.stringify(allureSettings));  
    console.log("当前设置", JSON.stringify(allureSettings));  
    """

    # 写入文件
    with open(os.path.join(directory, filename), 'w') as file:
        file.write(js_content)
    # print(f"{filename} 文件已创建并写入内容。")


# 修改index.html文件并引入settings.js
def insert_script_tag(new_script_tag):
    """ 在 Allure 报告的 index.html 中的指定位置插入一个新的 <script> 标签,如果该标签已存在则不插入
    @param new_script_tag: 需要插入的新 <script> 标签内容, 例如: '<script src="settings.js"></script>'
    @return: 没有返回内容
    """
    report_filepath = "../pytest_mytt/reports/allures/index.html"
    tag_to_find = '<script src="app.js"></script>'

    # 首先确保 new_script_tag 格式正确
    assert new_script_tag.startswith('<script') and new_script_tag.endswith('</script>')

    with open(report_filepath, 'r+', encoding="utf-8") as f:
        lines = f.readlines()
        f.seek(0)
        f.truncate()

        should_insert = True  # 初始设置为需要插入
        for line in lines:
            if new_script_tag in line:
                should_insert = False  # 如果新标签已存在,则不需要插入
                break
            if tag_to_find in line and should_insert:
                f.write(new_script_tag + '\n')  # 在找到的位置插入新标签,并且仅当需要插入时
            f.write(line)
        f.close()


# 生成设置成默认为中文的js文件
create_settings_js_file()
# 在修改index.html代码,引入设置文件
insert_script_tag('<script src="settings.js"></script>')

注意:需要修改成自己的路径地址

2、修改directory

可以参考我的,我的路径是:.../pytest_mytt/reports/allures/,那我的目录如下图:

注意:生成的这个settings.js文件要和index.html文件在同一级目录下

3.修改report_filepath

改成自己生成的allure报告的index.html文件的路径

4、调用

生成完allure报告后,执行该.py文件,然后打开allure报告的时候默认就成中文了

参考文件:allure report 修改默认语言为中文

相关推荐
普通网友15 分钟前
使用Python进行PDF文件的处理与操作
jvm·数据库·python
MZ_ZXD00124 分钟前
springboot流浪动物救助平台-计算机毕业设计源码08780
java·spring boot·后端·python·spring·flask·课程设计
十步杀一人_千里不留行30 分钟前
解释器模式:为 LLM 构建迷你 DSL 解释器,实现 Prompt 编排语言
python·prompt·解释器模式
小小测试开发31 分钟前
JMeter高级用法全解析:从性能测试到自动化监控,解锁压测工具的隐藏实力
运维·jmeter·自动化
这儿有一堆花1 小时前
python视觉开发
开发语言·python
普通网友1 小时前
编写一个Python脚本自动下载壁纸
jvm·数据库·python
w***4813 小时前
Python中的简单爬虫
爬虫·python·信息可视化
z***56563 小时前
【玩转全栈】----Django模板语法、请求与响应
数据库·python·django
voidmort4 小时前
web3.py 简介:面向 Python 开发者的以太坊
开发语言·python·web3.py
后台开发者Ethan4 小时前
LangGraph 的持久化
python·langgraph