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

相关推荐
用户83562907805118 小时前
Python 删除 Excel 工作表中的空白行列
后端·python
Json_18 小时前
使用python-fastApi框架开发一个学校宿舍管理系统-前后端分离项目
后端·python·fastapi
数据智能老司机1 天前
精通 Python 设计模式——分布式系统模式
python·设计模式·架构
数据智能老司机1 天前
精通 Python 设计模式——并发与异步模式
python·设计模式·编程语言
数据智能老司机1 天前
精通 Python 设计模式——测试模式
python·设计模式·架构
数据智能老司机1 天前
精通 Python 设计模式——性能模式
python·设计模式·架构
c8i1 天前
drf初步梳理
python·django
每日AI新事件1 天前
python的异步函数
python
这里有鱼汤1 天前
miniQMT下载历史行情数据太慢怎么办?一招提速10倍!
前端·python
databook2 天前
Manim实现脉冲闪烁特效
后端·python·动效