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 修改默认语言为中文

相关推荐
B站_计算机毕业设计之家1 小时前
机器学习实战项目:Python+Flask 汽车销量分析可视化系统(requests爬车主之家+可视化 源码+文档)✅
人工智能·python·机器学习·数据分析·flask·汽车·可视化
羊羊小栈1 小时前
基于「多模态大模型 + BGE向量检索增强RAG」的航空维修智能问答系统(vue+flask+AI算法)
vue.js·人工智能·python·语言模型·flask·毕业设计
星期天要睡觉1 小时前
模型部署——Flask 部署 PyTorch 模型
pytorch·python·flask
weixin_456904271 小时前
SHAP可视化代码详细讲解
python
DTS小夏1 小时前
算法社Python基础入门面试题库(新手版·含答案)
python·算法·面试
刘一哥GIS2 小时前
Windows环境搭建:PostGreSQL+PostGIS安装教程
数据库·python·arcgis·postgresql·postgis
西柚小萌新2 小时前
【深入浅出PyTorch】--4.PyTorch基础实战
人工智能·pytorch·python
用户8356290780512 小时前
掌控PDF页面:使用Python轻松实现添加与删除
后端·python
ManageEngineITSM2 小时前
IT 服务自动化的时代:让效率与体验共进
运维·数据库·人工智能·自动化·itsm·工单系统
用户3721574261352 小时前
Python 实现 Excel 文件加密与保护
python