修复allure-html报告压缩后发送给他人查看,但是打开无法查看到报告内容的问题。解决方案:通过google浏览器打开测试报告,当goole浏览器未安装情况下,启动服务打开测试报告. 弊端:仅支持windows

This commit is contained in:
floraachy
2023-05-25 14:45:48 +08:00
parent bad3750fa7
commit c9595c8ca2
6 changed files with 49 additions and 6 deletions

Binary file not shown.

View File

Before

Width:  |  Height:  |  Size: 4.4 KiB

After

Width:  |  Height:  |  Size: 4.4 KiB

View File

@@ -0,0 +1,35 @@
@echo off
setlocal enabledelayedexpansion
set "chrome_path="
::从注册表查找谷歌浏览器路径
set reg_query_command=reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe" /ve
::遍历注册表查询结果
for /f "tokens=2*" %%A in ('%reg_query_command%') do (
::如果有REG_SZ则证明找到了谷歌浏览器
if "%%A"=="REG_SZ" (
::如果路径存在则设置谷歌浏览器绝对路径到变量chrome_path
set "tmp_chrome_path=%%B"
if exist "!tmp_chrome_path!" (
set "chrome_path=%%B"
)
)
)
::如果上面找到了谷歌浏览器的路径
if defined chrome_path (
::打印找到了谷歌浏览器的文件地址
echo Chrome found at: "%chrome_path%"
::带参启动谷歌浏览器,使其不校验跨域问题
"%chrome_path%" --disable-web-security --user-data-dir="./" %~dp0/index.html
) else (
::如果没找到,打印没找到
echo Chrome not found.
::打印启动web信息
echo start a webserver ...
::启动一个web服务监听5001端口在后台运行。默认用当前文件夹的index.html作为首页
start /b http-server.exe -port 5001
::使用默认浏览器打开web服务的地址并等待浏览器关闭
start /WAIT msedge.exe http://127.0.0.1:5001
)

View File

@@ -11,7 +11,7 @@
CASE_FILE_TYPE = 1
# 0表示默认不发送任何通知 1代表钉钉通知2代表企业微信通知 3代表邮件通知 4代表所有途径都发送通知
SEND_RESULT_TYPE = 4
SEND_RESULT_TYPE = 0
# 测试报告的定制化信息展示
ENV_INFO = {

18
run.py
View File

@@ -30,7 +30,7 @@ from case_utils.get_results_handle import get_test_results_from_pytest_html_repo
get_test_results_from_from_allure_report
from case_utils.send_result_handle import send_result
from case_utils.allure_handle import AllureReportBeautiful
from common_utils.files_handle import zip_file
from common_utils.files_handle import zip_file, copy_file
@click.command()
@@ -86,6 +86,7 @@ def run(env, m, report):
# 执行指定测试用例
if m is not None:
arg_list.append(f"-m {m}")
current_time = datetime.now().strftime("%Y-%m-%d+%H_%M_%S")
if report.lower() == "allure":
arg_list.extend(['-q', '--cache-clear', f'--alluredir={ALLURE_RESULTS_DIR}', '--clean-alluredir'])
"""
@@ -110,16 +111,23 @@ def run(env, m, report):
# 往allure测试报告中写入环境配置相关信息
ENV_INFO["project_env"] = env
AllureReportBeautiful(allure_html_path=ALLURE_HTML_DIR).set_report_env_on_html(env_info=ENV_INFO)
# 发送从allure-html获取测试报告
# 从allure-html测试报告获取测试结果
results = get_test_results_from_from_allure_report(ALLURE_HTML_DIR)
# 复制http_server.exe以及双击查看报告.bat文件到allure-html根目录下用于支撑电脑在未安装allure服务的情况下打开allure-html报告
# 注意ZIP文件的名称包含某些特殊字符会导致无法使用.bat文件打开allure-html报告 例如空格,/ 等
allure_config_path = os.path.join(CONF_DIR, "allure_config")
copy_file(src_file_path=os.path.join(allure_config_path, [i for i in os.listdir(allure_config_path) if i.endswith(".exe")][0]),
dest_dir_path=ALLURE_HTML_DIR)
copy_file(src_file_path=os.path.join(allure_config_path, [i for i in os.listdir(allure_config_path) if i.endswith(".bat")][0]),
dest_dir_path=ALLURE_HTML_DIR)
# 压缩allure-html报告为一个压缩文件zip
allure_zip_path = os.path.join(REPORT_DIR, f'{ENV_INFO["report_name"]}.zip')
allure_zip_path = os.path.join(REPORT_DIR, f'{ENV_INFO["report_name"]}{str(current_time)}.zip')
zip_file(in_path=ALLURE_HTML_DIR, out_path=allure_zip_path)
send_result(results=results, attachment_path=allure_zip_path)
else:
current_time = datetime.now().strftime("%Y-%m-%d %H_%M_%S")
report_path = os.path.join(REPORT_DIR, ENV_INFO["report_name"] + str(current_time) + ".html")
report_css = os.path.join(CONF_DIR, "pytest_html_report.css")
pytest_html_config_path = os.path.join(CONF_DIR, "pytest_html_config")
report_css = os.path.join(pytest_html_config_path, "pytest_html_report.css")
arg_list.extend([f'--html={report_path}', f"--css={report_css}"])
pytest.main(args=arg_list)
logger.debug("-------测试完成,发送测试报告-------")