Allure企业级报告定制化自定义logo、中文标题、模块名、用例名、用例详细的测试数据如用例日志、请求信息、返回信息、logo右侧的文字等一文道尽
  s9J0zzi3ajGX 2023年11月05日 20 0

【自定义logo】

  1. 进入Allure的安装路径,找到config目录。
  2. 在config目录下,找到allure.yml文件,并打开该文件。
  3. 在allure.yml文件中,添加custom-logo-plugin选项。
  4. 进入Allure的安装路径,找到plugins目录下的custom-logo-plugin目录。
  5. 在custom-logo-plugin目录下,找到static目录,并将自己需要展示的Logo图片放到这个目录下。
  6. 编辑styles.css文件(如果存在的话),可以自定义Logo的大小、位置和样式等。

【自定义模块名,用例标题】

Allure企业级报告定制化自定义logo、中文标题、模块名、用例名、用例详细的测试数据如用例日志、请求信息、返回信息、logo右侧的文字等一文道尽_接口自动化测试

代码

@allure.story("登录模块")   #定义用例模块名称
@pytest.mark.parametrize("base_info,testcase", get_testcase_yaml("./testcases/test_Juhe_post_login_new.yaml"))
def test_post_login(self, base_info,testcase):
    #定义用例名称 如:登录失败的用例-密码不正确
    allure.dynamic.title(testcase["case_name"])


【用例详细信息】

上图,当点击用例的时候,右边会出现该用例测试结果相关的信息。这些信息如:用例日志log、接口名称、接口地址、请求方法、请求头等。他们都可以用attach的方式,加入到用例详细信息的列表。

Allure企业级报告定制化自定义logo、中文标题、模块名、用例名、用例详细的测试数据如用例日志、请求信息、返回信息、logo右侧的文字等一文道尽_企业级allure报告_02

代码:

api_name = case_info['api_name']
allure.attach(api_name, f'接口名称:{api_name}', allure.attachment_type.TEXT)
url = url_host + base_info['url']
allure.attach(api_name, f'接口地址:{url}', allure.attachment_type.TEXT)
method = case_info['method']
allure.attach(api_name, f'请求方法:{method}', allure.attachment_type.TEXT)
header = self.replace_load(case_info['header'])
allure.attach(api_name, f'请求头:{header}', allure.attachment_type.TEXT)

【中文网页标题 & 总览内容页标题】

Allure企业级报告定制化自定义logo、中文标题、模块名、用例名、用例详细的测试数据如用例日志、请求信息、返回信息、logo右侧的文字等一文道尽_allure报告自定义_03

中文网页标题:

Allure企业级报告定制化自定义logo、中文标题、模块名、用例名、用例详细的测试数据如用例日志、请求信息、返回信息、logo右侧的文字等一文道尽_接口自动化测试_04

总览内容页标题:

Allure企业级报告定制化自定义logo、中文标题、模块名、用例名、用例详细的测试数据如用例日志、请求信息、返回信息、logo右侧的文字等一文道尽_企业级allure报告_05

【logo右侧的文字】

Allure企业级报告定制化自定义logo、中文标题、模块名、用例名、用例详细的测试数据如用例日志、请求信息、返回信息、logo右侧的文字等一文道尽_接口自动化测试_06

设置方法:

Allure企业级报告定制化自定义logo、中文标题、模块名、用例名、用例详细的测试数据如用例日志、请求信息、返回信息、logo右侧的文字等一文道尽_allure报告自定义_07

为了方便修改,可以写代码,在生成allure报告之后,自动完成上述设置。

我写了一个类,希望对大家有帮助。

欢迎讨论留言,持续更新!

import json
import re
import shutil
from os import replace

from commons.recordlog import logs
from conftest import conf

class allureUtil:
    def __init__(self):
        self.title = conf.get_section_ALLURE_REPORT_CUSTOM("title")
        self.LogoFile = conf.get_section_ALLURE_REPORT_CUSTOM("LogoFile")
        self.reportFilePath = conf.get_section_ALLURE_REPORT_CUSTOM("reportFilePath")
        self.logoText = conf.get_section_ALLURE_REPORT_CUSTOM("logoText")
        self.reportContentTitle = conf.get_section_ALLURE_REPORT_CUSTOM("reportContentTitle")

    def doAllureCustom(self):
        self.replaceWebSiteTitle()
        self.replaceReportContentPageTitle()
        self.replaceFavicon()
        self.replaceLogosvg()
        self.replacelogoText()

    def replacelogoText(self):
        #图标右侧的展示文案
        # 获取CSS文件的路径
        Css_filepath = self.reportFilePath + r"/plugins/custom-logo/styles.css"
        try:
            file = open(Css_filepath, 'r', encoding="utf-8")
            content = file.read()

            oldText = re.search('content: "(.*?)";', content)
            if oldText is None:
                file.close()
                with open(Css_filepath, 'a',encoding="UTF-8") as file:
                    file.write('\n')
                    file.write(".side-nav__brand span{\n")
                    file.write("  display: none;\n")
                    file.write("}\n")
                    file.write(".side-nav__brand:after{\n")
                    file.write("  content: \"" + self.logoText + "\";\n")
                    file.write("  margin-left: 20px;\n")
                    file.write("}\n")
                file.close()
            else:
                oldText = oldText.group(1)
                content = content.replace(f"content: \"" + oldText, f"content: \"" + self.logoText)
                file.close()

                file = open(Css_filepath, 'w', encoding="utf-8")
                file.write(content)
        except Exception as e:
            logs.error(f'获取【{Css_filepath}】文件数据时出现未知错误: {str(e)}')
        finally:
            file.close()

    def replaceFavicon(self):
        # 定义源文件和目标目录
        target_Favicon_filepath = self.reportFilePath + r"/"
        source_Favicon_filepath = "./imgs/favicon.ico"
        # 使用shutil.copy2()函数复制文件到目标目录并覆盖已存在的文件
        shutil.copy2(source_Favicon_filepath, target_Favicon_filepath)

    def replaceLogosvg(self):
        # 定义源文件和目标目录
        target_Logosvg_filepath = self.reportFilePath + r"/plugins/custom-logo/"
        source_Logosvg_filepath = r"./imgs/custom-logo.svg"
        # 使用shutil.copy2()函数复制文件到目标目录并覆盖已存在的文件
        shutil.copy2(source_Logosvg_filepath, target_Logosvg_filepath)

    def replaceReportContentPageTitle(self):
        # 获取文件的路径
        report_filepath = self.reportFilePath + r"/widgets/summary.json"
        try:
            file = open(report_filepath, 'r', encoding="utf-8")
            content = file.read()
            contentJson = json.loads(content)
            contentJson["reportName"] = self.reportContentTitle
            file.close()

            content = json.dumps(contentJson, ensure_ascii=False)
            file = open(report_filepath, 'w', encoding="utf-8")
            file.write(content)
        except Exception as e:
            logs.error(f'获取【{report_filepath}】文件数据时出现未知错误: {str(e)}')
        finally:
            file.close()

    def replaceWebSiteTitle(self):
        #获取HTML测试报告的路径
        report_filepath = self.reportFilePath + r"/index.html"
        try:
            file = open(report_filepath,'r',encoding="utf-8")
            content = file.read()
            #替换web页面的标题
            oldTitle = re.search("<title>(.*?)</title>", content)
            oldTitle = oldTitle.group(1)
            content = content.replace(f"<title>" + oldTitle,f"<title>" + self.title)
            file.close()

            file = open(report_filepath, 'w', encoding="utf-8")
            file.write(content)
        except Exception as e:
            logs.error(f'获取【{report_filepath}】文件数据时出现未知错误: {str(e)}')
        finally:
            file.close()



【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

s9J0zzi3ajGX
最新推荐 更多