【自定义logo】
- 进入Allure的安装路径,找到config目录。
- 在config目录下,找到allure.yml文件,并打开该文件。
- 在allure.yml文件中,添加custom-logo-plugin选项。
- 进入Allure的安装路径,找到plugins目录下的custom-logo-plugin目录。
- 在custom-logo-plugin目录下,找到static目录,并将自己需要展示的Logo图片放到这个目录下。
- 编辑styles.css文件(如果存在的话),可以自定义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的方式,加入到用例详细信息的列表。
代码:
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)
【中文网页标题 & 总览内容页标题】
中文网页标题:
总览内容页标题:
【logo右侧的文字】
设置方法:
为了方便修改,可以写代码,在生成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()