From 2d79a82dc39b5aa19f349b5d281818f718a1a2e3 Mon Sep 17 00:00:00 2001 From: floraachy <1622042529@qq.com> Date: Fri, 17 Nov 2023 14:21:16 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E5=8F=91=E9=80=81=E9=92=89?= =?UTF-8?q?=E9=92=89/=E4=BC=81=E4=B8=9A=E5=BE=AE=E4=BF=A1/=E9=82=AE?= =?UTF-8?q?=E4=BB=B6=E9=87=8C=E9=9D=A2=E5=85=B3=E4=BA=8E=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E4=BA=BA=E5=91=98=E4=BF=A1=E6=81=AF=EF=BC=8C=E5=8F=82=E6=95=B0?= =?UTF-8?q?=E6=9B=BF=E6=8D=A2=E6=B2=A1=E6=9C=89=E6=88=90=E5=8A=9F=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- case_utils/report_utils/send_result_handle.py | 16 +-- case_utils/requests_utils/data_handle.py | 22 +++- config/case_template.txt | 3 +- config/settings.py | 107 ++++++++++-------- run.py | 11 +- 5 files changed, 91 insertions(+), 68 deletions(-) diff --git a/case_utils/report_utils/send_result_handle.py b/case_utils/report_utils/send_result_handle.py index cb31c8f..2674b03 100644 --- a/case_utils/report_utils/send_result_handle.py +++ b/case_utils/report_utils/send_result_handle.py @@ -3,10 +3,9 @@ # @Author : chenyinhua # @File : send_result_handle.py # @Software: PyCharm -# @Desc: 根据配置文件,发送指定类型的测试结果 +# @Desc: 根据配置文件,发送指定通知 -# 标准库导入 -# 第三方库导入 +# 第三方模块 from loguru import logger # 本地应用/模块导入 from config.models import NotificationType @@ -72,18 +71,21 @@ def send_wechat(webhook_url, content, attachment=None): 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 attachment_path: 发送的附件, pytest-html就是报告本身作为附件发送, allure是压缩包发送 """ # 默认不发送任何通知 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 results = get_test_results_from_from_allure_report(report_path) + for k, v in report_info.items(): + results[k] = v # 建立发送消息的内容、函数以及参数的映射关系 notification_mappings = { @@ -121,7 +123,7 @@ def send_result(report_path, attachment_path=None): if SEND_RESULT_TYPE in notification_mappings: 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) # 获取消息发送函数 sender = notification['sender'] @@ -134,7 +136,7 @@ def send_result(report_path, attachment_path=None): # 遍历所有消息发送方式 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) # 获取消息发送函数 sender = notification['sender'] diff --git a/case_utils/requests_utils/data_handle.py b/case_utils/requests_utils/data_handle.py index c7c94c4..d5f12a3 100644 --- a/case_utils/requests_utils/data_handle.py +++ b/case_utils/requests_utils/data_handle.py @@ -514,12 +514,30 @@ if __name__ == '__main__': print("-----------测试场景17---------------------") data_17 = { "winner_id": "${winner_id}", - "user_id": "${user_id}" + "user_id": "${user_id}", + "assignee_id": "${assignee_id}" } source = { "winner_id": "1,2,4", - "user_id": [] + "assignee_id": [], + "user_id": 1076 } res = data_handle(obj=data_17, source=source) 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) diff --git a/config/case_template.txt b/config/case_template.txt index 1f31352..28c1498 100644 --- a/config/case_template.txt +++ b/config/case_template.txt @@ -7,7 +7,6 @@ import pytest import allure from loguru import logger # 本地应用/模块导入 -from config.settings import db_info from config.global_vars import GLOBAL_VARS 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 @@ -37,7 +36,7 @@ class ${class_title}Auto: # 进行响应断言 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)) logger.info("\n-----------------------------END-用例执行结束-----------------------------\n") diff --git a/config/settings.py b/config/settings.py index a13e722..4e1cc23 100644 --- a/config/settings.py +++ b/config/settings.py @@ -9,36 +9,63 @@ # ------------------------------------ 测试数据配置 ----------------------------------------------------# ENV_VARS = { "common": { - "报告标题": "API自动化测试报告", + "报告标题": "UI自动化测试报告", "项目名称": "GitLink 确实开源", - "测试人": "陈银花", - "所属部门": "开源中心" + "tester": "陈银花", + "department": "开源中心", + "env": "" }, "test": { # 示例测试环境及示例测试账号 "host": "https://testforgeplus.trustie.net", "login": "autotest", - "password": "***autotest***", # 运行时需要手动更改密码 + "password": "****autotest-test****", # 运行时需要手动更改密码 "nickname": "autotest", - "user_id": "106", + "user_id": 106, "super_login": "floraachy", - "super_password": "***floraachy***", - "project_id": "59", - "repo_id": "59", - "project_url": "/autotest/auotest" + "super_password": "****floraachy-test****", + "project_id": 59, + "repo_id": 59, + "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": { "host": "https://www.gitlink.org.cn", - "login": "autotest", - "password": "***autotest***", # 运行时需要手动更改密码 - "nickname": "autotest", - "user_id": "106", - "super_login": "floraachy", - "super_password": "***floraachy***", - "project_id": "", - "repo_id": "", - "project_url": "" + "login": "floraachy", + "password": "****floraachy-live****", + "nickname": "🌼陈银花", + "user_id": 87611, + "super_login": "chenyh", + "super_password": "****chenyh-live****", + "project_id": 1445676, + "repo_id": 1447291, + "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用例, 其他数值将不自动生成用例,仅能执行手动编写的用例 -CASE_FILE_TYPE = 0 +CASE_FILE_TYPE = 1 # 0表示默认不发送任何通知, 1代表钉钉通知,2代表企业微信通知, 3代表邮件通知, 4代表所有途径都发送通知 -SEND_RESULT_TYPE = 0 +SEND_RESULT_TYPE = 1 # 指定日志收集级别 LOG_LEVEL = "DEBUG" @@ -77,39 +104,19 @@ LOG_LEVEL = "DEBUG" ERROR: 表示错误和异常情况,但程序仍然可以继续运行。 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 = { - "user": "******", # 发件人邮箱 - "password": "******", # 发件人邮箱授权码 + "user": "****email-user****", # 发件人邮箱 + "password": "****email-password****", # 发件人邮箱授权码 "host": "smtp.qq.com", - "to": ["******", "******"] # 收件人邮箱 + "to": ["****email-user-1****", "****email-user-2****"] # 收件人邮箱 } # ------------------------------------ 邮件通知内容 ----------------------------------------------------# -email_subject = f"接口自动化报告" +email_subject = f"UI自动化报告" email_content = """ 各位同事, 大家好: @@ -117,7 +124,7 @@ email_content = """ --------------------------------------------------------------------------------------------------------------- 测试人: ${tester} 所属部门: ${department} - 项目环境: ${run_env} + 项目环境: ${env} --------------------------------------------------------------------------------------------------------------- 执行结果如下:   用例运行总数: ${total} 个 @@ -133,12 +140,12 @@ email_content = """ """ # ------------------------------------ 钉钉相关配置 ----------------------------------------------------# ding_talk = { - "webhook_url": "https://oapi.dingtalk.com/robot/send?access_token=***********", - "secret": "***********" + "webhook_url": "https://oapi.dingtalk.com/robot/send?access_token=********", + "secret": "****ding****" } # ------------------------------------ 钉钉通知内容 ----------------------------------------------------# -ding_talk_title = f"接口自动化报告" +ding_talk_title = f"UI自动化报告" ding_talk_content = """ 各位同事, 大家好: @@ -146,7 +153,7 @@ ding_talk_content = """ --------------------------------------------------------------------------------------------------------------- #### 测试人: ${tester} #### 所属部门: ${department} - #### 项目环境: ${run_env} + #### 项目环境: ${env} --------------------------------------------------------------------------------------------------------------- #### 执行结果如下: - 用例运行总数: ${total} 个 @@ -172,7 +179,7 @@ wechat_content = """ -------------------------------- #### 测试人: ${tester} #### 所属部门: ${department} - #### 项目环境: ${run_env} + #### 项目环境: ${env} -------------------------------- #### 执行结果如下: - 用例运行总数: ${total} 个 diff --git a/run.py b/run.py index a0afb23..07199dc 100644 --- a/run.py +++ b/run.py @@ -70,13 +70,10 @@ def run(env, m, report): """) # ------------------------ 处理一下获取到的参数---------------------------- - print(f"打印一下run方法的入参:\nreport={report}\nenv={env}\nm={m}") - # 根据指定的环境参数,将运行环境所需相关配置数据保存到GLOBAL_VARS - GLOBAL_VARS["env_key"] = env.lower() - if ENV_VARS.get(env.lower()): - GLOBAL_VARS.update(ENV_VARS[env.lower()]) - + ENV_VARS["common"]["env"] = ENV_VARS[env]["host"] + GLOBAL_VARS.update(ENV_VARS["common"]) + GLOBAL_VARS.update(ENV_VARS[env]) # ------------------------ 自动生成测试用例 ------------------------ # 删除原有的测试用例,以便生成新的测试用例 if os.path.exists(AUTO_CASE_DIR): @@ -109,7 +106,7 @@ def run(env, m, report): 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: raise e