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

相关推荐
蓝婷儿32 分钟前
6个月Python学习计划 Day 16 - 面向对象编程(OOP)基础
开发语言·python·学习
chao_7891 小时前
链表题解——两两交换链表中的节点【LeetCode】
数据结构·python·leetcode·链表
大霞上仙2 小时前
nonlocal 与global关键字
开发语言·python
Mark_Aussie2 小时前
Flask-SQLAlchemy使用小结
python·flask
程序员阿龙2 小时前
【精选】计算机毕业设计Python Flask海口天气数据分析可视化系统 气象数据采集处理 天气趋势图表展示 数据可视化平台源码+论文+PPT+讲解
python·flask·课程设计·数据可视化系统·天气数据分析·海口气象数据·pandas 数据处理
ZHOU_WUYI2 小时前
Flask与Celery 项目应用(shared_task使用)
后端·python·flask
且慢.5893 小时前
Python_day47
python·深度学习·计算机视觉
佩奇的技术笔记3 小时前
Python入门手册:异常处理
python
大写-凌祁3 小时前
论文阅读:HySCDG生成式数据处理流程
论文阅读·人工智能·笔记·python·机器学习
爱喝喜茶爱吃烤冷面的小黑黑3 小时前
小黑一层层削苹果皮式大模型应用探索:langchain中智能体思考和执行工具的demo
python·langchain·代理模式