修改allure相关代码,新增无allure html报告版本

This commit is contained in:
floraachy
2023-09-29 09:14:15 +08:00
parent f5b499fea6
commit e97fe7290f
3 changed files with 152 additions and 20 deletions

View File

@@ -4,7 +4,11 @@
# @File : platform_handle.py
# @Software: PyCharm
# @Desc: 跨平台的支持allure用于生成allure测试报告
# 标准库导入
import os.path
import platform
# 本地应用/模块导入
from config.path_config import LIB_DIR, ALLURE_RESULTS_DIR, ALLURE_HTML_DIR
class PlatformHandle:
@@ -12,18 +16,16 @@ class PlatformHandle:
@property
def allure(self):
allure_bin = os.path.join(LIB_DIR, [i for i in os.listdir(LIB_DIR) if i.startswith("allure")][0], "bin")
if platform.system() == "Windows":
cmd = "allure.bat"
# 生成测试报告 --clean 覆盖路径,将上次的结果覆盖掉
cmd2 = "{} generate {} -o {} --clean"
allure_path = os.path.join(allure_bin, "allure.bat")
else:
cmd = "allure"
# 生成测试报告 --clean 覆盖路径,将上次的结果覆盖掉
cmd2 = "sudo {} generate {} -o {} --clean"
return cmd, cmd2
allure_path = os.path.join(allure_bin, "allure")
os.system(f"chmod +x {allure_path}")
cmd = f"{allure_path} generate {ALLURE_RESULTS_DIR} -o {ALLURE_HTML_DIR} --clean"
return cmd
if __name__ == '__main__':
plat = PlatformHandle()
res = plat.allure[0]
res = PlatformHandle().allure
print(res)

14
run.py
View File

@@ -44,7 +44,7 @@ from case_utils.case_fun_handle import generate_cases
from case_utils.platform_handle import PlatformHandle
from case_utils.send_result_handle import send_result
from case_utils.allure_handle import AllureReportBeautiful
from config.path_config import REPORT_DIR, LOG_DIR, AUTO_CASE_DIR, CONF_DIR, LIB_DIR, ALLURE_RESULTS_DIR, \
from config.path_config import REPORT_DIR, LOG_DIR, AUTO_CASE_DIR, CONF_DIR, ALLURE_RESULTS_DIR, \
ALLURE_HTML_DIR
from config.settings import LOG_LEVEL
from config.global_vars import GLOBAL_VARS, ENV_VARS
@@ -114,16 +114,8 @@ def run_pytest(mark_param):
pytest.main(args=arg_list)
# ------------------------ 使用allure生成测试报告 ------------------------
# 从LIB_DIR目录中寻找以allure开头的目录作为allure模块的目录并进入bin目录下
allure_path = os.path.join(LIB_DIR, [i for i in os.listdir(LIB_DIR) if i.startswith("allure")][0], "bin")
# 根据windows或linux环境判断, 执行指定的命令。
cmd = PlatformHandle().allure[1].format(
os.path.join(allure_path, PlatformHandle().allure[0]),
ALLURE_RESULTS_DIR,
ALLURE_HTML_DIR,
)
os.popen(cmd).read()
allure_cmd = PlatformHandle().allure
os.popen(allure_cmd).read()
# ------------------------ 美化allure测试报告 ------------------------
# 设置allure报告窗口标题
AllureReportBeautiful(allure_html_path=ALLURE_HTML_DIR).set_windows_title(

138
run_no_html_report.py Normal file
View File

@@ -0,0 +1,138 @@
# -*- coding: utf-8 -*-
# @Time : 2023/9/29 9:04
# @Author : Flora.Chen
# @File : run_no_html_report.py
# @Software: PyCharm
# @Desc: 框架主入口
"""
说明:
1、用例创建原则测试文件名必须以“test”开头测试函数必须以“test”开头。
2、运行方式
> python run.py (默认在test环境运行测试用例, 报告采用allure)
> python run.py -m demo 在test环境仅运行打了标记demo用例 默认报告采用allure
> python run.py -env live 在live环境运行测试用例
> python run.py -env=test 在test环境运行测试用例
pytest相关参数以下也可通过pytest.ini配置
--reruns: 失败重跑次数
--reruns-delay 失败重跑间隔时间
--count: 重复执行次数
-v: 显示错误位置以及错误的详细信息
-s: 等价于 pytest --capture=no 可以捕获print函数的输出
-q: 简化输出信息
-m: 运行指定标签的测试用例
-x: 一旦错误,则停止运行
--cache-clear 清除pytest的缓存包括测试结果缓存、抓取的fixture实例缓存和收集器信息缓存等
--maxfail: 设置最大失败次数,当超出这个阈值时,则不会在执行测试用例
"--reruns=3", "--reruns-delay=2"
allure相关参数
-alluredir这个选项用于指定存储测试结果的路径
"""
# 标准库导入
import os
import shutil
from datetime import datetime
# 第三方库导入
import pytest
from loguru import logger
import click
# 本地应用/模块导入
from case_utils.case_fun_handle import generate_cases
from config.path_config import LOG_DIR, AUTO_CASE_DIR, ALLURE_RESULTS_DIR
from config.settings import LOG_LEVEL
from config.global_vars import GLOBAL_VARS, ENV_VARS
def capture_all_logs(level=LOG_LEVEL):
logger.info("""
_ _ _ _____ _
__ _ _ __ (_) / \\ _ _| |_ __|_ _|__ ___| |_
/ _` | "_ \\| | / _ \\| | | | __/ _ \\| |/ _ \\/ __| __|
| (_| | |_) | |/ ___ \\ |_| | || (_) | | __/\\__ \\ |_
\\__,_| .__/|_/_/ \\_\\__,_|\\__\\___/|_|\\___||___/\\__|
|_|
Starting ... ... ...
""")
if level:
# 仅捕获指定级别日志
logger.add(
os.path.join(LOG_DIR, "runtime_{time}.log"),
enqueue=True,
encoding="utf-8",
rotation="00:00",
level=LOG_LEVEL.upper(),
format="{time:YYYY-MM-DD HH:mm:ss} {level} From {module}.{function} : {message}",
)
else:
# 捕获所有日志
logger.add(
os.path.join(LOG_DIR, "runtime_{time}_all.log"),
enqueue=True,
encoding="utf-8",
rotation="00:00",
format="{time:YYYY-MM-DD HH:mm:ss} {level} From {module}.{function} : {message}",
)
# 封装生成测试用例的函数
def auto_generate_test_cases():
# 删除原有的测试用例,以便生成新的测试用例
if os.path.exists(AUTO_CASE_DIR):
shutil.rmtree(AUTO_CASE_DIR)
# 根据data里面的yaml/excel文件自动生成测试用例
generate_cases()
# 封装执行 pytest 的函数
def run_pytest(mark_param):
arg_list = []
# 执行指定的测试用例
if mark_param is not None:
arg_list.append(f"-m {mark_param}")
current_time = datetime.now().strftime("%Y-%m-%d+%H_%M_%S")
# 生成 Allure 报告
arg_list.extend(
[
"-q",
"--cache-clear",
f'--alluredir={ALLURE_RESULTS_DIR}',
"--clean-alluredir",
]
)
pytest.main(args=arg_list)
# 主函数
@click.command()
@click.option("-env", default="test", help="输入运行环境test 或 live")
@click.option("-m", default=None, help="选择需要运行的用例python.ini配置的名称")
def run(env, m):
try:
# ------------------------ 捕获日志----------------------------
capture_all_logs()
# ------------------------ 设置全局变量 ------------------------
# 根据指定的环境参数将运行环境所需相关配置数据保存到GLOBAL_VARS
GLOBAL_VARS["env_key"] = env.lower()
if ENV_VARS.get(env.lower()):
GLOBAL_VARS.update(ENV_VARS[env.lower()])
# ------------------------ 自动生成测试用例 ------------------------
auto_generate_test_cases()
# ------------------------ pytest执行测试用例 ------------------------
run_pytest(mark_param=m)
except Exception as e:
raise e
if __name__ == "__main__":
run()