增加测试结果数据模型定义

This commit is contained in:
wangjie 2023-09-22 20:02:37 +08:00
parent e8bfbf58a8
commit 4fe89e001c
6 changed files with 102 additions and 83 deletions

View File

@ -4,8 +4,9 @@
# @Author : wangjie # @Author : wangjie
# @File : models.py # @File : models.py
# @project : SensoroApi # @project : SensoroApi
from dataclasses import dataclass
from enum import Enum from enum import Enum
from typing import Text
class Environment(Enum): class Environment(Enum):
@ -15,6 +16,21 @@ class Environment(Enum):
DIANJUN = 'dianjun' 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__': if __name__ == '__main__':
print(Environment.DEV.name) print(Environment.DEV.name)
print(Environment.DEV.value) print(Environment.DEV.value)

View File

@ -25,7 +25,7 @@ IS_SEND_EMAIL = False
IS_SEND_WECHAT = False IS_SEND_WECHAT = False
# 设置是否开启debug日志 # 设置是否开启debug日志
LOG_DEBUG = True LOG_DEBUG = False
# 设置是否开启控制台日志 # 设置是否开启控制台日志
LOG_CONSOLE = True LOG_CONSOLE = True
@ -46,20 +46,20 @@ email_config = {
email_content = """ email_content = """
各位同事, 大家好:<br> 各位同事, 大家好:<br>
自动化用例于 <strong>${case_start_time}</strong> 开始运行运行时长<strong>${case_duration}s</strong> 目前已执行完成<br> 自动化用例于 <strong>${start_time}</strong> 开始运行运行时长<strong>${duration}s</strong> 目前已执行完成<br>
---------------------------------------------------------------------------------------------------------------<br> ---------------------------------------------------------------------------------------------------------------<br>
项目名称<strong>%s</strong> <br> 项目名称<strong>%s</strong> <br>
构件编号<strong>#%s</strong><br> 构件编号<strong>#%s</strong><br>
项目环境<strong>%s</strong><br> 项目环境<strong>%s</strong><br>
---------------------------------------------------------------------------------------------------------------<br> ---------------------------------------------------------------------------------------------------------------<br>
执行结果如下:<br> 执行结果如下:<br>
&nbsp;&nbsp;用例运行总数:<strong> ${total_case}</strong><br> &nbsp;&nbsp;用例运行总数:<strong> ${total}</strong><br>
&nbsp;&nbsp;通过用例数passed: <strong><font color="green" >${pass_case}</font></strong><br> &nbsp;&nbsp;通过用例数passed: <strong><font color="green" >${passed}</font></strong><br>
&nbsp;&nbsp;失败用例数failed: <strong><font color="red" >${fail_case}</font></strong><br> &nbsp;&nbsp;失败用例数failed: <strong><font color="red" >${failed}</font></strong><br>
&nbsp;&nbsp;报错用例数error: <strong><font color="orange" >${error_case}</font></strong><br> &nbsp;&nbsp;报错用例数error: <strong><font color="orange" >${error}</font></strong><br>
&nbsp;&nbsp;跳过用例数skipped: <strong><font color="grey" >${skip_case}</font></strong><br> &nbsp;&nbsp;跳过用例数skipped: <strong><font color="grey" >${skipped}</font></strong><br>
&nbsp;&nbsp;预期失败用例数xfail: <strong><font color="grey" >${xfail_case}</font></strong><br> &nbsp;&nbsp;预期失败用例数xfail: <strong><font color="grey" >${xfailed}</font></strong><br>
&nbsp;&nbsp;预期通过用例数xpass: <strong><font color="grey" >${xpass_case}</font></strong><br> &nbsp;&nbsp;预期通过用例数xpass: <strong><font color="grey" >${xpassed}</font></strong><br>
&nbsp;&nbsp;通过率: <strong><font color="green" >${pass_rate}%%</font></strong><br> &nbsp;&nbsp;通过率: <strong><font color="green" >${pass_rate}%%</font></strong><br>
&nbsp;&nbsp;测试报告点击查看: <a href='%s'>[测试报告入口]</a><br> &nbsp;&nbsp;测试报告点击查看: <a href='%s'>[测试报告入口]</a><br>
&nbsp;&nbsp;构建详情点击查看: <a href='%s'>[控制台入口]</a><br> &nbsp;&nbsp;构建详情点击查看: <a href='%s'>[控制台入口]</a><br>
@ -78,16 +78,16 @@ wechat_content = """******用例执行结果统计******
> 项目名称:%s > 项目名称:%s
> 构件编号:#%s > 构件编号:#%s
> 测试环境:%s > 测试环境:%s
> 总用例数<font color=\"info\">${total_case}条</font> > 总用例数<font color=\"info\">${total}条</font>
> 通过用例数<font color=\"info\">${pass_case}条</font> > 通过用例数<font color=\"info\">${passed}条</font>
> 失败用例数<font color=\"red\">${fail_case}条</font> > 失败用例数<font color=\"red\">${failed}条</font>
> 报错用例数<font color=\"red\">${error_case}条</font> > 报错用例数<font color=\"red\">${error}条</font>
> 跳过用例数<font color=\"warning\">${skip_case}条</font> > 跳过用例数<font color=\"warning\">${skipped}条</font>
> 预期失败用例数<font color=\"comment\">${xfail_case}条</font> > 预期失败用例数<font color=\"comment\">${xfailed}条</font>
> 预期通过用例数<font color=\"comment\">${xpass_case}条</font> > 预期通过用例数<font color=\"comment\">${xpassed}条</font>
> 通过率<font color=\"info\">${pass_rate}%%</font> > 通过率<font color=\"info\">${pass_rate}%%</font>
> 用例开始时间:<font color=\"info\">${case_start_time}</font> > 用例开始时间:<font color=\"info\">${start_time}</font>
> 用例执行时长<font color=\"info\">${case_duration}s</font> > 用例执行时长<font color=\"info\">${duration}s</font>
> 测试报告点击查看>>[测试报告入口](%s) > 测试报告点击查看>>[测试报告入口](%s)
> 构建详情点击查看>>[控制台入口](%s) > 构建详情点击查看>>[控制台入口](%s)
> <@汪杰>""" % (ProjectName, BUILD_NUMBER, ENV.name, ALLURE_URL, BUILD_URL) > <@汪杰>""" % (ProjectName, BUILD_NUMBER, ENV.name, ALLURE_URL, BUILD_URL)

View File

@ -6,15 +6,15 @@
# @project : SensoroApi # @project : SensoroApi
import os.path import os.path
import platform import platform
import shutil
import time import time
import pytest import pytest
from common.models import TestMetrics
from common.settings import ENV from common.settings import ENV
from configs.dir_path_config import BASE_DIR from configs.dir_path_config import BASE_DIR
from configs.lins_environment import EntryPoint from configs.lins_environment import EntryPoint
from utils.reportdatahandle import ReportDataHandle from utils.report_data_handle import ReportDataHandle
def pytest_sessionstart(): def pytest_sessionstart():
@ -101,16 +101,17 @@ def pytest_runtest_makereport(item, call): # description取值为用例说明__
def pytest_terminal_summary(terminalreporter, exitstatus, config): 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) run_time = round((time.time() - terminalreporter._sessionstarttime), 2)
print("******用例执行结果统计******") print("******用例执行结果统计******")
print(f"总用例数:{pytest_result['total_case']}") print(f"总用例数:{pytest_result.total}")
print(f"通过:{pytest_result['pass_case']}") print(f"通过:{pytest_result.passed}")
print(f"失败:{pytest_result['fail_case']}") print(f"失败:{pytest_result.failed}")
print(f"跳过:{pytest_result['skip_case']}") print(f"跳过:{pytest_result.skipped}")
print(f"预期失败:{pytest_result['xfail_case']}") print(f"预期失败:{pytest_result.xfailed}")
print(f"预期通过:{pytest_result['xpass_case']}") print(f"预期通过:{pytest_result.xpassed}")
print(f"报错:{pytest_result['error_case']}") print(f"报错:{pytest_result.error}")
print(f"用例通过率:{pytest_result['pass_rate']}%") print(f"用例通过率:{pytest_result.pass_rate}%")
print(f"用例执行时间:{pytest_result['case_duration']}s") print(f"用例开始时间:{pytest_result.start_time}")
print(f"用例执行时间:{pytest_result.duration}s")
print(f"总用时(算上了生成报告的时间){run_time}s") print(f"总用时(算上了生成报告的时间){run_time}s")

6
run.py
View File

@ -10,10 +10,12 @@
""" """
import os import os
from dataclasses import asdict
import pytest import pytest
from common.base_log import logger from common.base_log import logger
from common.models import TestMetrics
from utils.command_parser import command_parser from utils.command_parser import command_parser
from common.mail_sender import MailSender from common.mail_sender import MailSender
from common.robot_sender import EnterpriseWechatNotification 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 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.data_handle import DataProcessor
from utils.file_handle import FileHandle from utils.file_handle import FileHandle
from utils.reportdatahandle import ReportDataHandle from utils.report_data_handle import ReportDataHandle
if __name__ == '__main__': if __name__ == '__main__':
logger.info(""" logger.info("""
@ -68,7 +70,7 @@ if __name__ == '__main__':
FileHandle.copy_file(BASE_DIR + os.sep + '查看allure报告方法', ALLURE_REPORT_DIR) 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: # 判断是否需要发送企业微信 if IS_SEND_WECHAT: # 判断是否需要发送企业微信
EnterpriseWechatNotification(wechat_webhook_url).send_markdown( EnterpriseWechatNotification(wechat_webhook_url).send_markdown(
DataProcessor().process_data(wechat_content, pytest_result)) DataProcessor().process_data(wechat_content, pytest_result))

View File

@ -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())

View File

@ -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())