diff --git a/common/models.py b/common/models.py index 7bc8476..a22a123 100644 --- a/common/models.py +++ b/common/models.py @@ -4,8 +4,9 @@ # @Author : wangjie # @File : models.py # @project : SensoroApi - +from dataclasses import dataclass from enum import Enum +from typing import Text class Environment(Enum): @@ -15,6 +16,21 @@ class Environment(Enum): DIANJUN = 'dianjun' +@dataclass +class TestMetrics: + """ 用例执行数据 """ + total: int + passed: int + failed: int + skipped: int + xfailed: int + xpassed: int + error: int + pass_rate: float + start_time: Text + duration: float + + if __name__ == '__main__': print(Environment.DEV.name) print(Environment.DEV.value) diff --git a/common/settings.py b/common/settings.py index 52efdcf..7658c59 100644 --- a/common/settings.py +++ b/common/settings.py @@ -25,7 +25,7 @@ IS_SEND_EMAIL = False IS_SEND_WECHAT = False # 设置是否开启debug日志 -LOG_DEBUG = True +LOG_DEBUG = False # 设置是否开启控制台日志 LOG_CONSOLE = True @@ -46,20 +46,20 @@ email_config = { email_content = """ 各位同事, 大家好:
- 自动化用例于 ${case_start_time} 开始运行,运行时长:${case_duration}s, 目前已执行完成。
+ 自动化用例于 ${start_time} 开始运行,运行时长:${duration}s, 目前已执行完成。
---------------------------------------------------------------------------------------------------------------
项目名称:%s
构件编号:#%s
项目环境:%s
---------------------------------------------------------------------------------------------------------------
执行结果如下:
-   用例运行总数: ${total_case}条
-   通过用例数(passed): ${pass_case}条
-   失败用例数(failed): ${fail_case}条
-   报错用例数(error): ${error_case}条
-   跳过用例数(skipped): ${skip_case}条
-   预期失败用例数(xfail): ${xfail_case}条
-   预期通过用例数(xpass): ${xpass_case}条
+   用例运行总数: ${total}条
+   通过用例数(passed): ${passed}条
+   失败用例数(failed): ${failed}条
+   报错用例数(error): ${error}条
+   跳过用例数(skipped): ${skipped}条
+   预期失败用例数(xfail): ${xfailed}条
+   预期通过用例数(xpass): ${xpassed}条
  通过率: ${pass_rate}%%
  测试报告,点击查看: [测试报告入口]
  构建详情,点击查看: [控制台入口]
@@ -78,16 +78,16 @@ wechat_content = """******用例执行结果统计****** > 项目名称:%s > 构件编号:#%s > 测试环境:%s - > 总用例数:${total_case}条 - > 通过用例数:${pass_case}条 - > 失败用例数:${fail_case}条 - > 报错用例数:${error_case}条 - > 跳过用例数:${skip_case}条 - > 预期失败用例数:${xfail_case}条 - > 预期通过用例数:${xpass_case}条 + > 总用例数:${total}条 + > 通过用例数:${passed}条 + > 失败用例数:${failed}条 + > 报错用例数:${error}条 + > 跳过用例数:${skipped}条 + > 预期失败用例数:${xfailed}条 + > 预期通过用例数:${xpassed}条 > 通过率:${pass_rate}%% - > 用例开始时间:${case_start_time} - > 用例执行时长:${case_duration}s + > 用例开始时间:${start_time} + > 用例执行时长:${duration}s > 测试报告,点击查看>>[测试报告入口](%s) > 构建详情,点击查看>>[控制台入口](%s) > <@汪杰>""" % (ProjectName, BUILD_NUMBER, ENV.name, ALLURE_URL, BUILD_URL) diff --git a/conftest.py b/conftest.py index fc3bc93..43faf80 100644 --- a/conftest.py +++ b/conftest.py @@ -6,15 +6,15 @@ # @project : SensoroApi import os.path import platform -import shutil import time import pytest +from common.models import TestMetrics from common.settings import ENV from configs.dir_path_config import BASE_DIR from configs.lins_environment import EntryPoint -from utils.reportdatahandle import ReportDataHandle +from utils.report_data_handle import ReportDataHandle def pytest_sessionstart(): @@ -101,16 +101,17 @@ def pytest_runtest_makereport(item, call): # description取值为用例说明__ def pytest_terminal_summary(terminalreporter, exitstatus, config): """收集测试结果展示在控制台""" - pytest_result = ReportDataHandle.pytest_json_report_case_count() + pytest_result = TestMetrics(**ReportDataHandle.pytest_json_report_case_count()) run_time = round((time.time() - terminalreporter._sessionstarttime), 2) print("******用例执行结果统计******") - print(f"总用例数:{pytest_result['total_case']}条") - print(f"通过:{pytest_result['pass_case']}条") - print(f"失败:{pytest_result['fail_case']}条") - print(f"跳过:{pytest_result['skip_case']}条") - print(f"预期失败:{pytest_result['xfail_case']}条") - print(f"预期通过:{pytest_result['xpass_case']}条") - print(f"报错:{pytest_result['error_case']}条") - print(f"用例通过率:{pytest_result['pass_rate']}%") - print(f"用例执行时间:{pytest_result['case_duration']}s") + print(f"总用例数:{pytest_result.total}条") + print(f"通过:{pytest_result.passed}条") + print(f"失败:{pytest_result.failed}条") + print(f"跳过:{pytest_result.skipped}条") + print(f"预期失败:{pytest_result.xfailed}条") + print(f"预期通过:{pytest_result.xpassed}条") + print(f"报错:{pytest_result.error}条") + print(f"用例通过率:{pytest_result.pass_rate}%") + print(f"用例开始时间:{pytest_result.start_time}") + print(f"用例执行时间:{pytest_result.duration}s") print(f"总用时(算上了生成报告的时间):{run_time}s") diff --git a/run.py b/run.py index ac468b7..2ded42e 100755 --- a/run.py +++ b/run.py @@ -10,10 +10,12 @@ """ import os +from dataclasses import asdict import pytest from common.base_log import logger +from common.models import TestMetrics from utils.command_parser import command_parser from common.mail_sender import MailSender from common.robot_sender import EnterpriseWechatNotification @@ -22,7 +24,7 @@ from common.settings import IS_SEND_EMAIL, IS_SEND_WECHAT, wechat_webhook_url, w from configs.dir_path_config import BASE_DIR, TEMP_DIR, PYTEST_REPORT_DIR, PYTEST_RESULT_DIR, ALLURE_REPORT_DIR from utils.data_handle import DataProcessor from utils.file_handle import FileHandle -from utils.reportdatahandle import ReportDataHandle +from utils.report_data_handle import ReportDataHandle if __name__ == '__main__': logger.info(""" @@ -68,7 +70,7 @@ if __name__ == '__main__': FileHandle.copy_file(BASE_DIR + os.sep + '查看allure报告方法', ALLURE_REPORT_DIR) # 发送企业微信群聊 - pytest_result = ReportDataHandle.pytest_json_report_case_count() + pytest_result = asdict(TestMetrics(**ReportDataHandle.pytest_json_report_case_count())) if IS_SEND_WECHAT: # 判断是否需要发送企业微信 EnterpriseWechatNotification(wechat_webhook_url).send_markdown( DataProcessor().process_data(wechat_content, pytest_result)) diff --git a/utils/report_data_handle.py b/utils/report_data_handle.py new file mode 100644 index 0000000..bb02d37 --- /dev/null +++ b/utils/report_data_handle.py @@ -0,0 +1,50 @@ +# !/usr/bin/python +# -*- coding:utf-8 -*- +# @Time : 2023/6/7 16:00 +# @Author : wangjie +# @File : report_data_handle.py +# @project : SensoroApi +import json +import os + +from configs.dir_path_config import PYTEST_RESULT_DIR +from utils.time_utils import TimeUtil + + +class ReportDataHandle: + + @staticmethod + # TODO:完善allure报告的统计 + def allure_case_count(): + """统计allure报告收集的case数量""" + pass + + @staticmethod + def pytest_json_report_case_count(): + """统计pytest_json_report报告收集的case数量""" + with open(PYTEST_RESULT_DIR + os.sep + 'pytest_result.json', 'r', encoding='utf-8') as f: + pytest_result = json.loads(f.read()) + case_count = {} + case_count["total"] = pytest_result['summary'].get("total", 0) # 用例总数 + case_count["passed"] = pytest_result["summary"].get("passed", 0) # 通过用例数 + case_count["failed"] = pytest_result["summary"].get("failed", 0) # 失败用例数 + case_count["skipped"] = pytest_result["summary"].get("skipped", 0) # 跳过用例数 + case_count["xfailed"] = pytest_result["summary"].get("xfailed", 0) # 预期失败用例数 + case_count["xpassed"] = pytest_result["summary"].get("xpassed", 0) # 预期成功用例数 + case_count["error"] = pytest_result["summary"].get("error", 0) # 报错用例数(比如语法错误导致) + # 判断运行用例总数大于0 + if case_count["total"] > 0: + # 计算成功率 + case_count["pass_rate"] = round( + (case_count["passed"] + case_count["xpassed"]) / case_count["total"] * 100, 2) + else: + # 如果未运行用例,则成功率为 0.0 + case_count["pass_rate"] = 0.0 + case_count["duration"] = round(pytest_result["duration"], 2) # 用例运行时间 + case_count["start_time"] = TimeUtil.unix_time_to_str(int('%.f' % pytest_result["created"])) # 用例开始时间 + + return case_count + + +if __name__ == '__main__': + print(ReportDataHandle.pytest_json_report_case_count()) diff --git a/utils/reportdatahandle.py b/utils/reportdatahandle.py deleted file mode 100644 index 815890f..0000000 --- a/utils/reportdatahandle.py +++ /dev/null @@ -1,50 +0,0 @@ -# !/usr/bin/python -# -*- coding:utf-8 -*- -# @Time : 2023/6/7 16:00 -# @Author : wangjie -# @File : report_data_handle.py -# @project : SensoroApi -import json -import os - -from configs.dir_path_config import BASE_DIR, PYTEST_RESULT_DIR -from utils.time_utils import TimeUtil - - -class ReportDataHandle: - - @staticmethod - # TODO:完善allure报告的统计 - def allure_case_count(): - """统计allure报告收集的case数量""" - pass - - @staticmethod - def pytest_json_report_case_count(): - """统计pytest_json_report报告收集的case数量""" - with open(PYTEST_RESULT_DIR + os.sep + 'pytest_result.json', 'r', encoding='utf-8') as f: - pytest_result = json.loads(f.read()) - case_count = {} - case_count["total_case"] = pytest_result['summary'].get("total", 0) # 用例总数 - case_count["pass_case"] = pytest_result["summary"].get("passed", 0) # 通过用例数 - case_count["fail_case"] = pytest_result["summary"].get("failed", 0) # 失败用例数 - case_count["skip_case"] = pytest_result["summary"].get("skipped", 0) # 跳过用例数 - case_count["xfail_case"] = pytest_result["summary"].get("xfailed", 0) # 预期失败用例数 - case_count["xpass_case"] = pytest_result["summary"].get("xpassed", 0) # 预期成功用例数 - case_count["error_case"] = pytest_result["summary"].get("error", 0) # 报错用例数(比如语法错误导致) - # 判断运行用例总数大于0 - if case_count["total_case"] > 0: - # 计算成功率 - case_count["pass_rate"] = round( - (case_count["pass_case"] + case_count["xpass_case"]) / case_count["total_case"] * 100, 2) - else: - # 如果未运行用例,则成功率为 0.0 - case_count["pass_rate"] = 0.0 - case_count["case_duration"] = round(pytest_result["duration"], 2) # 用例运行时间 - case_count["case_start_time"] = TimeUtil.unix_time_to_str(int('%.f' % pytest_result["created"])) # 用例开始时间 - - return case_count - - -if __name__ == '__main__': - print(ReportDataHandle.pytest_json_report_case_count())