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 分钟前
《Effective Python》第1章 Pythonic 思维总结——编写优雅、高效的 Python 代码
开发语言·python
zhou18516 分钟前
MySQL保姆级安装教程(附资源包+5分钟极速配置+环境变量调试技巧)
java·python·mysql·php
lczdyx41 分钟前
PNG转ico图标(支持圆角矩形/方形+透明背景)Python脚本 - 随笔
图像处理·python
struggle20251 小时前
ebook2audiobook开源程序使用动态 AI 模型和语音克隆将电子书转换为带有章节和元数据的有声读物。支持 1,107+ 种语言
人工智能·开源·自动化
lgily-12251 小时前
常用的设计模式详解
java·后端·python·设计模式
陈苏同学2 小时前
MPC控制器从入门到进阶(小车动态避障变道仿真 - Python)
人工智能·python·机器学习·数学建模·机器人·自动驾驶
mahuifa2 小时前
python实现usb热插拔检测(linux)
linux·服务器·python
MyhEhud2 小时前
kotlin @JvmStatic注解的作用和使用场景
开发语言·python·kotlin
狐凄3 小时前
Python实例题:pygame开发打飞机游戏
python·游戏·pygame
漫谈网络3 小时前
Telnet 类图解析
python·自动化·netdevops·telnetlib·网络自动化运维