调整发送钉钉/企业微信/邮件里面关于测试人员信息,参数替换没有成功的问题

This commit is contained in:
floraachy
2023-11-17 14:21:16 +08:00
parent 4c175e101c
commit 2d79a82dc3
5 changed files with 91 additions and 68 deletions
@@ -3,10 +3,9 @@
# @Author : chenyinhua # @Author : chenyinhua
# @File : send_result_handle.py # @File : send_result_handle.py
# @Software: PyCharm # @Software: PyCharm
# @Desc: 根据配置文件,发送指定类型的测试结果 # @Desc: 根据配置文件,发送指定通知
# 标准库导入 # 第三方模块
# 第三方库导入
from loguru import logger from loguru import logger
# 本地应用/模块导入 # 本地应用/模块导入
from config.models import NotificationType from config.models import NotificationType
@@ -72,18 +71,21 @@ def send_wechat(webhook_url, content, attachment=None):
logger.error(f"发送企业微信通知异常, 错误信息:{e}") logger.error(f"发送企业微信通知异常, 错误信息:{e}")
def send_result(report_path, attachment_path=None): def send_result(report_info: dict, report_path: str, attachment_path: str = None):
""" """
根据用户配置,采取指定方式,发送测试结果 根据用户配置,采取指定方式,发送测试结果
:param report_info: 报告相关信息,包括tester, department, env
:param report_path: 报告路径 :param report_path: 报告路径
:param attachment_path: 发送的附件, pytest-html就是报告本身作为附件发送, allure是压缩包发送 :param attachment_path: 发送的附件, pytest-html就是报告本身作为附件发送, allure是压缩包发送
""" """
# 默认不发送任何通知 # 默认不发送任何通知
if SEND_RESULT_TYPE == NotificationType.DEFAULT.value: if SEND_RESULT_TYPE == NotificationType.DEFAULT.value:
logger.debug(f"SEND_RESULT_TYPE={SEND_RESULT_TYPE} 配置了不发送任何邮件") logger.info(f"SEND_RESULT_TYPE={SEND_RESULT_TYPE} 配置了不发送任何邮件")
return return
results = get_test_results_from_from_allure_report(report_path) results = get_test_results_from_from_allure_report(report_path)
for k, v in report_info.items():
results[k] = v
# 建立发送消息的内容、函数以及参数的映射关系 # 建立发送消息的内容、函数以及参数的映射关系
notification_mappings = { notification_mappings = {
@@ -121,7 +123,7 @@ def send_result(report_path, attachment_path=None):
if SEND_RESULT_TYPE in notification_mappings: if SEND_RESULT_TYPE in notification_mappings:
notification = notification_mappings[SEND_RESULT_TYPE] notification = notification_mappings[SEND_RESULT_TYPE]
# 获取消息内容并替换 # 获取消息内容并替换
notification['sender_args']['content'] = data_handle(notification['sender_args']['content'], notification['sender_args']['content'] = data_handle(obj=notification['sender_args']['content'],
source=results) source=results)
# 获取消息发送函数 # 获取消息发送函数
sender = notification['sender'] sender = notification['sender']
@@ -134,7 +136,7 @@ def send_result(report_path, attachment_path=None):
# 遍历所有消息发送方式 # 遍历所有消息发送方式
for notification in notification_mappings.values(): for notification in notification_mappings.values():
# 获取消息内容并替换 # 获取消息内容并替换
notification['sender_args']['content'] = data_handle(notification['sender_args']['content'], notification['sender_args']['content'] = data_handle(obj=notification['sender_args']['content'],
source=results) source=results)
# 获取消息发送函数 # 获取消息发送函数
sender = notification['sender'] sender = notification['sender']
+20 -2
View File
@@ -514,12 +514,30 @@ if __name__ == '__main__':
print("-----------测试场景17---------------------") print("-----------测试场景17---------------------")
data_17 = { data_17 = {
"winner_id": "${winner_id}", "winner_id": "${winner_id}",
"user_id": "${user_id}" "user_id": "${user_id}",
"assignee_id": "${assignee_id}"
} }
source = { source = {
"winner_id": "1,2,4", "winner_id": "1,2,4",
"user_id": [] "assignee_id": [],
"user_id": 1076
} }
res = data_handle(obj=data_17, source=source) res = data_handle(obj=data_17, source=source)
print(res) print(res)
print("-----------测试场景18---------------------")
payload = {'id': 'case_login_01', 'title': '用户名密码正确,登录成功(不校验数据库)', 'severity': 'NORMAL',
'url': ' https://testforgeplus.trustie.net/api/accounts/login.json', 'run': True, 'method': 'POST',
'headers': {'Content-Type': 'application/json; charset=utf-8;'}, 'cookies': None, 'request_type': 'JSON',
'payload': {'login': 'autotest', 'password': '********', 'autologin': 1}, 'files': None,
'extract': {'nickname': '$.username', 'login': '$.login', 'user_id': '$.user_id'},
'assert_response': {'eq': {'http_code': 200, '$.user_id': "${user_id}"}}, 'assert_sql': None}
source = {
"winner_id": "1,2,4",
"assignee_id": [],
"user_id": 1076
}
res = data_handle(obj=payload, source=source)
print(res)
+1 -2
View File
@@ -7,7 +7,6 @@ import pytest
import allure import allure
from loguru import logger from loguru import logger
# 本地应用/模块导入 # 本地应用/模块导入
from config.settings import db_info
from config.global_vars import GLOBAL_VARS from config.global_vars import GLOBAL_VARS
from case_utils.assert_utils.assert_handle import assert_response, assert_sql from case_utils.assert_utils.assert_handle import assert_response, assert_sql
from case_utils.requests_utils.request_data_handle import RequestPreDataHandle, RequestHandle, after_request_extract from case_utils.requests_utils.request_data_handle import RequestPreDataHandle, RequestHandle, after_request_extract
@@ -37,7 +36,7 @@ class ${class_title}Auto:
# 进行响应断言 # 进行响应断言
assert_response(response, case_data["assert_response"]) assert_response(response, case_data["assert_response"])
# 进行数据库断言 # 进行数据库断言
assert_sql(db_info[GLOBAL_VARS["env_key"]], case_data["assert_sql"]) assert_sql(db_info=GLOBAL_VARS["db_info"], expected=case_data["assert_sql"])
# 断言成功后进行参数提取 # 断言成功后进行参数提取
after_request_extract(response, case_data.get("extract", None)) after_request_extract(response, case_data.get("extract", None))
logger.info("\n-----------------------------END-用例执行结束-----------------------------\n") logger.info("\n-----------------------------END-用例执行结束-----------------------------\n")
+57 -50
View File
@@ -9,36 +9,63 @@
# ------------------------------------ 测试数据配置 ----------------------------------------------------# # ------------------------------------ 测试数据配置 ----------------------------------------------------#
ENV_VARS = { ENV_VARS = {
"common": { "common": {
"报告标题": "API自动化测试报告", "报告标题": "UI自动化测试报告",
"项目名称": "GitLink 确实开源", "项目名称": "GitLink 确实开源",
"测试人": "陈银花", "tester": "陈银花",
"所属部门": "开源中心" "department": "开源中心",
"env": ""
}, },
"test": { "test": {
# 示例测试环境及示例测试账号 # 示例测试环境及示例测试账号
"host": "https://testforgeplus.trustie.net", "host": "https://testforgeplus.trustie.net",
"login": "autotest", "login": "autotest",
"password": "***autotest***", # 运行时需要手动更改密码 "password": "****autotest-test****", # 运行时需要手动更改密码
"nickname": "autotest", "nickname": "autotest",
"user_id": "106", "user_id": 106,
"super_login": "floraachy", "super_login": "floraachy",
"super_password": "***floraachy***", "super_password": "****floraachy-test****",
"project_id": "59", "project_id": 59,
"repo_id": "59", "repo_id": 59,
"project_url": "/autotest/auotest" "project_url": "/autotest/auotest",
"db_info": {
"db_host": "xx.xx.xx.xx",
"db_port": 3306,
"db_user": "root",
"db_pwd": "**********",
"db_database": "test**********",
"ssh": True,
"ssh_host": "xx.xx.xx.xx",
"ssh_port": 3306,
"ssh_user": "root",
"ssh_pwd": "**********"
}
}, },
"live": { "live": {
"host": "https://www.gitlink.org.cn", "host": "https://www.gitlink.org.cn",
"login": "autotest", "login": "floraachy",
"password": "***autotest***", # 运行时需要手动更改密码 "password": "****floraachy-live****",
"nickname": "autotest", "nickname": "🌼陈银花",
"user_id": "106", "user_id": 87611,
"super_login": "floraachy", "super_login": "chenyh",
"super_password": "***floraachy***", "super_password": "****chenyh-live****",
"project_id": "", "project_id": 1445676,
"repo_id": "", "repo_id": 1447291,
"project_url": "" "project_url": "/floraachy/auotest",
"db_info": {
"db_host": "xx.xx.xx.xx",
"db_port": 3306,
"db_user": "root",
"db_pwd": "**********",
"db_database": "test**********",
"ssh": True,
"ssh_host": "xx.xx.xx.xx",
"ssh_port": 3306,
"ssh_user": "root",
"ssh_pwd": "**********"
}
} }
} }
@@ -60,10 +87,10 @@ class RunConfig:
# ------------------------------------ 配置信息 ----------------------------------------------------# # ------------------------------------ 配置信息 ----------------------------------------------------#
# 0代表执行Excel和yaml两种格式的用例, 1 代表 yaml文件,2 用例代表Excel用例, 其他数值将不自动生成用例,仅能执行手动编写的用例 # 0代表执行Excel和yaml两种格式的用例, 1 代表 yaml文件,2 用例代表Excel用例, 其他数值将不自动生成用例,仅能执行手动编写的用例
CASE_FILE_TYPE = 0 CASE_FILE_TYPE = 1
# 0表示默认不发送任何通知, 1代表钉钉通知,2代表企业微信通知, 3代表邮件通知, 4代表所有途径都发送通知 # 0表示默认不发送任何通知, 1代表钉钉通知,2代表企业微信通知, 3代表邮件通知, 4代表所有途径都发送通知
SEND_RESULT_TYPE = 0 SEND_RESULT_TYPE = 1
# 指定日志收集级别 # 指定日志收集级别
LOG_LEVEL = "DEBUG" LOG_LEVEL = "DEBUG"
@@ -77,39 +104,19 @@ LOG_LEVEL = "DEBUG"
ERROR: 表示错误和异常情况,但程序仍然可以继续运行。 ERROR: 表示错误和异常情况,但程序仍然可以继续运行。
CRITICAL: 表示严重的错误和异常情况,可能导致程序崩溃或无法正常运行。 CRITICAL: 表示严重的错误和异常情况,可能导致程序崩溃或无法正常运行。
""" """
# ------------------------------------ 数据库相关配置 ----------------------------------------------------#
db_info = {
"test": {
"db_host": "xx.xx.xx.xx",
"db_port": 3306,
"db_user": "root",
"db_pwd": "**********",
"db_database": "test**********",
"ssh": True,
"ssh_host": "xx.xx.xx.xx",
"ssh_port": 3306,
"ssh_user": "root",
"ssh_pwd": "**********"
},
"live": {
}
}
# ------------------------------------ 邮件配置信息 ----------------------------------------------------# # ------------------------------------ 邮件配置信息 ----------------------------------------------------#
# 发送邮件的相关配置信息 # 发送邮件的相关配置信息
email = { email = {
"user": "******", # 发件人邮箱 "user": "****email-user****", # 发件人邮箱
"password": "******", # 发件人邮箱授权码 "password": "****email-password****", # 发件人邮箱授权码
"host": "smtp.qq.com", "host": "smtp.qq.com",
"to": ["******", "******"] # 收件人邮箱 "to": ["****email-user-1****", "****email-user-2****"] # 收件人邮箱
} }
# ------------------------------------ 邮件通知内容 ----------------------------------------------------# # ------------------------------------ 邮件通知内容 ----------------------------------------------------#
email_subject = f"接口自动化报告" email_subject = f"UI自动化报告"
email_content = """ email_content = """
各位同事, 大家好: 各位同事, 大家好:
@@ -117,7 +124,7 @@ email_content = """
--------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------------------
测试人:<strong> ${tester} </strong> 测试人:<strong> ${tester} </strong>
所属部门:<strong> ${department} </strong> 所属部门:<strong> ${department} </strong>
项目环境:<strong> ${run_env} </strong> 项目环境:<strong> ${env} </strong>
--------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------------------
执行结果如下: 执行结果如下:
&nbsp;&nbsp;用例运行总数:<strong> ${total} 个</strong> &nbsp;&nbsp;用例运行总数:<strong> ${total} 个</strong>
@@ -133,12 +140,12 @@ email_content = """
""" """
# ------------------------------------ 钉钉相关配置 ----------------------------------------------------# # ------------------------------------ 钉钉相关配置 ----------------------------------------------------#
ding_talk = { ding_talk = {
"webhook_url": "https://oapi.dingtalk.com/robot/send?access_token=***********", "webhook_url": "https://oapi.dingtalk.com/robot/send?access_token=********",
"secret": "***********" "secret": "****ding****"
} }
# ------------------------------------ 钉钉通知内容 ----------------------------------------------------# # ------------------------------------ 钉钉通知内容 ----------------------------------------------------#
ding_talk_title = f"接口自动化报告" ding_talk_title = f"UI自动化报告"
ding_talk_content = """ ding_talk_content = """
各位同事, 大家好: 各位同事, 大家好:
@@ -146,7 +153,7 @@ ding_talk_content = """
--------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------------------
#### 测试人: ${tester} #### 测试人: ${tester}
#### 所属部门: ${department} #### 所属部门: ${department}
#### 项目环境: ${run_env} #### 项目环境: ${env}
--------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------------------
#### 执行结果如下: #### 执行结果如下:
- 用例运行总数: ${total} - 用例运行总数: ${total}
@@ -172,7 +179,7 @@ wechat_content = """
-------------------------------- --------------------------------
#### 测试人: ${tester} #### 测试人: ${tester}
#### 所属部门: ${department} #### 所属部门: ${department}
#### 项目环境: ${run_env} #### 项目环境: ${env}
-------------------------------- --------------------------------
#### 执行结果如下: #### 执行结果如下:
- 用例运行总数: ${total} - 用例运行总数: ${total}
+4 -7
View File
@@ -70,13 +70,10 @@ def run(env, m, report):
""") """)
# ------------------------ 处理一下获取到的参数---------------------------- # ------------------------ 处理一下获取到的参数----------------------------
print(f"打印一下run方法的入参:\nreport={report}\nenv={env}\nm={m}")
# 根据指定的环境参数,将运行环境所需相关配置数据保存到GLOBAL_VARS # 根据指定的环境参数,将运行环境所需相关配置数据保存到GLOBAL_VARS
GLOBAL_VARS["env_key"] = env.lower() ENV_VARS["common"]["env"] = ENV_VARS[env]["host"]
if ENV_VARS.get(env.lower()): GLOBAL_VARS.update(ENV_VARS["common"])
GLOBAL_VARS.update(ENV_VARS[env.lower()]) GLOBAL_VARS.update(ENV_VARS[env])
# ------------------------ 自动生成测试用例 ------------------------ # ------------------------ 自动生成测试用例 ------------------------
# 删除原有的测试用例,以便生成新的测试用例 # 删除原有的测试用例,以便生成新的测试用例
if os.path.exists(AUTO_CASE_DIR): if os.path.exists(AUTO_CASE_DIR):
@@ -109,7 +106,7 @@ def run(env, m, report):
f'autotest_report.zip')) f'autotest_report.zip'))
# ------------------------ 发送测试结果 ------------------------ # ------------------------ 发送测试结果 ------------------------
send_result(report_path=report_path, attachment_path=attachment_path) send_result(report_info=ENV_VARS["common"], report_path=report_path, attachment_path=attachment_path)
except Exception as e: except Exception as e:
raise e raise e