提交 55cf7fd9 authored 作者: 陈泽健's avatar 陈泽健

调整日志记录器。

上级 6a5c62c6
...@@ -190,9 +190,10 @@ def enter_areafuntion_manage(): ...@@ -190,9 +190,10 @@ def enter_areafuntion_manage():
except (TimeoutException, ElementNotInteractableException) as error: except (TimeoutException, ElementNotInteractableException) as error:
logging.error(f"Error clicking manage button: {error}") logging.error(f"Error clicking manage button: {error}")
def dingding_send_message(test_report_url, title, text, mobile): def dingding_send_message(test_report_url, title, mobile, ding_type):
""" """
发送钉钉机器人消息 发送钉钉机器人消息
参考接口文档:https://open.dingtalk.com/document/orgapp/custom-robots-send-group-messages#title-7fs-kgs-36x
:param test_report_url: 测试报告链接 :param test_report_url: 测试报告链接
:param title: 消息标题 :param title: 消息标题
...@@ -201,10 +202,17 @@ def dingding_send_message(test_report_url, title, text, mobile): ...@@ -201,10 +202,17 @@ def dingding_send_message(test_report_url, title, text, mobile):
""" """
# 记录调用此函数的日志 # 记录调用此函数的日志
logging.info("开始构建并发送钉钉机器人消息") logging.info("开始构建并发送钉钉机器人消息")
# 钉钉机器人的 Webhook URL 和密钥(正式环境)
# webhook_url = 'https://oapi.dingtalk.com/robot/send?access_token=b0eea0bbf097ce3badb4c832d2cd0267a50486f395ec8beca6e2042102bb295b'
# secret = 'SEC928b11659c5fd6476cfa2042edbf56da876abf759289f7e4d3c671fb9a81bf43'
# 钉钉机器人的 Webhook URL 和密钥(测试环境)
# 钉钉机器人的 Webhook URL 和密钥 if ding_type == '标准版巡检':
webhook_url = 'https://oapi.dingtalk.com/robot/send?access_token=7fbf40798cad98b1b5db55ff844ba376b1816e80c5777e6f47ae1d9165dacbb4' webhook_url = 'https://oapi.dingtalk.com/robot/send?access_token=7fbf40798cad98b1b5db55ff844ba376b1816e80c5777e6f47ae1d9165dacbb4'
secret = 'SEC610498ed6261ae2df1d071d0880aaa70abf5e67efe47f75a809c1f2314e0dbd6' secret = 'SEC610498ed6261ae2df1d071d0880aaa70abf5e67efe47f75a809c1f2314e0dbd6'
elif ding_type == '展厅巡检':
webhook_url = 'https://oapi.dingtalk.com/robot/send?access_token=061b6e9b1ae436f356cfda7fe19b6e58e46b62670046a78bd3a4d869118c612d'
secret = 'SEC93212bd880aad638cc0df2b28a72ef4fdf6651cacb8a6a4bc71dcf09705d458d'
# 生成时间戳 # 生成时间戳
timestamp = str(round(time.time() * 1000)) timestamp = str(round(time.time() * 1000))
...@@ -235,11 +243,11 @@ def dingding_send_message(test_report_url, title, text, mobile): ...@@ -235,11 +243,11 @@ def dingding_send_message(test_report_url, title, text, mobile):
'link': { 'link': {
'title': title, 'title': title,
'messageUrl': test_report_url, 'messageUrl': test_report_url,
'text': text 'text': test_report_url
}, },
"at": { "at": {
"atMobiles": [mobile], "atMobiles": [mobile],
"isAtAll": False "isAtAll": True
} }
} }
...@@ -292,7 +300,7 @@ def get_latest_report_file(report_dir, base_url): ...@@ -292,7 +300,7 @@ def get_latest_report_file(report_dir, base_url):
last_modified_time = datetime.fromtimestamp(os.path.getmtime(latest_file)).strftime('%Y-%m-%d %H:%M:%S') last_modified_time = datetime.fromtimestamp(os.path.getmtime(latest_file)).strftime('%Y-%m-%d %H:%M:%S')
# 记录最新报告文件的信息 # 记录最新报告文件的信息
logging.info(f"最新文件: {latest_file}, 最后修改时间: {last_modified_time}") logging.info(f"最新报告文件: {latest_file}, 最后修改时间: {last_modified_time}")
# 将文件路径转换为相对于基础URL的相对路径 # 将文件路径转换为相对于基础URL的相对路径
relative_path = os.path.relpath(latest_file, report_dir) relative_path = os.path.relpath(latest_file, report_dir)
...@@ -303,10 +311,9 @@ def get_latest_report_file(report_dir, base_url): ...@@ -303,10 +311,9 @@ def get_latest_report_file(report_dir, base_url):
# 返回完整的URL # 返回完整的URL
return full_url return full_url
def get_reportfile_send_dingding(report_title, report_url_prefix, ding_type):
def get_reportfile_send_dingding(report_title, report_url_prefix):
try: try:
# 获取文件所在的目录 # 获取报告文件所在的目录
report_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..','log') report_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..','log')
# 获取基础URL # 获取基础URL
...@@ -324,7 +331,7 @@ def get_reportfile_send_dingding(report_title, report_url_prefix): ...@@ -324,7 +331,7 @@ def get_reportfile_send_dingding(report_title, report_url_prefix):
logging.info("开始调用钉钉消息通知函数") logging.info("开始调用钉钉消息通知函数")
# 调用钉钉发送消息接口进行推送测试报告链接 # 调用钉钉发送消息接口进行推送测试报告链接
dingding_send_message(latest_report, report_title, f"{report_title}_执行完成", "13169131600") dingding_send_message(latest_report, report_title, "13724387318", ding_type)
# 记录钉钉消息通知函数调用成功的日志 # 记录钉钉消息通知函数调用成功的日志
logging.info("钉钉消息通知函数调用成功") logging.info("钉钉消息通知函数调用成功")
...@@ -345,7 +352,6 @@ def get_reportfile_send_dingding(report_title, report_url_prefix): ...@@ -345,7 +352,6 @@ def get_reportfile_send_dingding(report_title, report_url_prefix):
# 无论是否成功,都记录测试结束的日志 # 无论是否成功,都记录测试结束的日志
logging.info("自动化测试完成。") logging.info("自动化测试完成。")
def read_csv_data(csv_file_path): def read_csv_data(csv_file_path):
""" """
读取CSV文件中的数据,并将其转换为一个包含字典的列表,每个字典代表一行测试用例数据。 读取CSV文件中的数据,并将其转换为一个包含字典的列表,每个字典代表一行测试用例数据。
...@@ -374,3 +380,45 @@ def read_csv_data(csv_file_path): ...@@ -374,3 +380,45 @@ def read_csv_data(csv_file_path):
INFO("CSV文件已读取") INFO("CSV文件已读取")
# 返回包含所有测试用例数据的列表 # 返回包含所有测试用例数据的列表
return ddt_cases return ddt_cases
def run_automation_test(report_title, report_url_prefix , ding_type):
"""
运行自动化测试并生成报告。
参数:
- report_title: 报告的标题
- report_url_prefix: 报告URL的前缀
- test_case: 测试用例脚本执行的标签名
"""
# 记录测试开始的日志
logging.info("开始自动化测试...")
# 构建运行测试命令
command = [
'hytest',
'--report_title', report_title,
'--report_url_prefix', report_url_prefix
]
# 记录将要执行的命令日志
logging.info(f"执行命令: {' '.join(command)}")
try:
# 执行测试命令并获取结果
result = subprocess.run(command, capture_output=True, text=True, check=True)
# 记录命令的标准输出和错误输出
logging.debug(f"命令标准输出: {result.stdout}")
logging.debug(f"命令错误输出: {result.stderr}")
except subprocess.CalledProcessError as e:
# 处理子进程调用失败的异常
logging.error(f"命令执行失败,返回码 {e.returncode}: {e.output}")
except OSError as e:
# 处理操作系统相关的异常
logging.error(f"发生操作系统错误: {e}")
finally:
# 无论测试是否成功,都记录测试结束的日志
logging.info("自动化测试完成。")
# 调用回调函数处理后续操作
get_reportfile_send_dingding(f"{report_title}", report_url_prefix, ding_type)
This source diff could not be displayed because it is too large. You can view the blob instead.
server_addr: "ngrok.ubsyun.com:9083"
trust_host_root_certs: false
tunnels:
nat1:
remote_port: 31135
proto:
tcp: "192.168.1.114:81"
\ No newline at end of file
ngrok -config=ngrok.cfg start nat1
\ No newline at end of file
This source diff could not be displayed because it is too large. You can view the blob instead.
import schedule
import threading
import queue
from lib.base import *
import time
import logging
# 配置日志记录器,仅输出到控制台
logging.basicConfig(
level=logging.DEBUG, # 设置日志级别为 DEBUG
format='%(asctime)s - %(levelname)s - %(message)s', # 日志格式:时间 - 日志级别 - 消息
handlers=[
logging.StreamHandler() # 将日志输出到控制台
]
)
"""
执行指令:
1.打开一个终端输入:
- cd .\运维集控\项目测试\运维标准版\
- python -m http.server 80 --directory reports
2.打开新终端输入:
- cd .\运维集控\项目测试\运维标准版\ngrok\
- .\start.bat
2.再打开一个终端输入:
- cd .\运维集控\项目测试\运维标准版\
- python .\定时任务执行.py
"""
# 创建一个任务队列,用于存储待处理的任务
task_queue = queue.Queue()
def run_task(task, *args, **kwargs):
# 将任务及其参数放入任务队列
task_queue.put((task, args, kwargs))
logging.debug(f"任务已加入队列: {task.__name__} with args: {args} and kwargs: {kwargs}")
def worker():
# 工作线程的主循环
while True:
# 从任务队列中获取任务及其参数
task, args, kwargs = task_queue.get()
try:
# 记录任务开始执行的时间
logging.debug(f"开始执行任务: {task.__name__} with args: {args} and kwargs: {kwargs}")
# 执行任务并获取结果
result = task(*args, **kwargs)
# 如果任务有返回结果,记录日志
if result:
logging.info(result)
except Exception as e:
# 捕获任务执行过程中发生的任何异常并记录错误日志
logging.error(f"执行任务时发生错误: {e}", exc_info=True)
finally:
# 无论任务是否成功执行,都标记任务已完成
task_queue.task_done()
# 记录任务完成的时间
logging.debug(f"任务完成: {task.__name__}")
def start_workers(num_workers):
# 启动指定数量的工作线程
for _ in range(num_workers):
# 创建一个新的工作线程,目标函数为 worker,设置为守护线程
threading.Thread(target=worker, daemon=True).start()
# 启动3个工作线程
start_workers(3)
# 定义每天定时执行的任务
# 每天早上07:50执行后台系统设置功能测试
schedule.every().day.at("17:51").do(run_task, run_automation_test, report_title="运维系统测试报告", report_url_prefix="http://nat.ubainsyun.com:31135", ding_type="标准版巡检")
try:
# 无限循环,持续检查并执行计划任务
while True:
schedule.run_pending() # 检查并执行所有待处理的任务
time.sleep(1) # 每秒检查一次
except KeyboardInterrupt:
# 捕获用户中断信号 (Ctrl+C)
logging.info("Scheduler interrupted by user.")
except Exception as e:
# 捕获其他未预期的异常
logging.error(f"Unexpected error: {e}", exc_info=True)
\ No newline at end of file
...@@ -170,3 +170,5 @@ ...@@ -170,3 +170,5 @@
- 调整会议预约的判断流程,适配网络不稳定场景。 - 调整会议预约的判断流程,适配网络不稳定场景。
39. 2025-01-13 39. 2025-01-13
- 调整展厅巡检窗帘控制的等待时间。调整标准版功能巡检至测试发布192.168.5.235环境。去除移动端自动化测试完成后的退出操作。 - 调整展厅巡检窗帘控制的等待时间。调整标准版功能巡检至测试发布192.168.5.235环境。去除移动端自动化测试完成后的退出操作。
40. 2025-01-14
- 调整日志记录器。
\ No newline at end of file
...@@ -15,15 +15,6 @@ current_dir = os.path.dirname(os.path.abspath(__file__)) ...@@ -15,15 +15,6 @@ current_dir = os.path.dirname(os.path.abspath(__file__))
# 构建CSV文件的绝对路径 # 构建CSV文件的绝对路径
csv_file_path = os.path.join(current_dir, '../../../测试数据/账号管理/用户管理模块/用户管理主流程.csv') csv_file_path = os.path.join(current_dir, '../../../测试数据/账号管理/用户管理模块/用户管理主流程.csv')
# 配置日志记录器,仅输出到控制台
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.StreamHandler()
]
)
class Main_User_Manage_000x: class Main_User_Manage_000x:
# tags = ['用户管理功能'] # tags = ['用户管理功能']
""" """
......
...@@ -21,16 +21,6 @@ current_dir = os.path.dirname(os.path.abspath(__file__)) ...@@ -21,16 +21,6 @@ current_dir = os.path.dirname(os.path.abspath(__file__))
# 构建CSV文件的绝对路径 # 构建CSV文件的绝对路径
csv_file_path = os.path.join(current_dir, '../../../测试数据/账号管理/用户管理模块/用户修改密码.csv') csv_file_path = os.path.join(current_dir, '../../../测试数据/账号管理/用户管理模块/用户修改密码.csv')
# 配置日志记录器,仅输出到控制台
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.StreamHandler()
]
)
class User_Chanage_Pwd_000x: class User_Chanage_Pwd_000x:
tags = ['用户管理功能' , '后台管理功能','预定系统功能'] tags = ['用户管理功能' , '后台管理功能','预定系统功能']
""" """
......
...@@ -21,15 +21,6 @@ current_dir = os.path.dirname(os.path.abspath(__file__)) ...@@ -21,15 +21,6 @@ current_dir = os.path.dirname(os.path.abspath(__file__))
# 构建CSV文件的绝对路径 # 构建CSV文件的绝对路径
csv_file_path = os.path.join(current_dir, '../../../测试数据/账号管理/用户管理模块/用户新增.csv') csv_file_path = os.path.join(current_dir, '../../../测试数据/账号管理/用户管理模块/用户新增.csv')
# 配置日志记录器,仅输出到控制台
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.StreamHandler()
]
)
class Add_User_000x: class Add_User_000x:
tags = ['用户管理功能' , '后台管理功能','预定系统功能'] tags = ['用户管理功能' , '后台管理功能','预定系统功能']
""" """
......
...@@ -17,15 +17,6 @@ current_dir = os.path.dirname(os.path.abspath(__file__)) ...@@ -17,15 +17,6 @@ current_dir = os.path.dirname(os.path.abspath(__file__))
# 构建CSV文件的绝对路径 # 构建CSV文件的绝对路径
csv_file_path = os.path.join(current_dir, '../../../测试数据/账号管理/用户管理模块/用户查询.csv') csv_file_path = os.path.join(current_dir, '../../../测试数据/账号管理/用户管理模块/用户查询.csv')
# 配置日志记录器,仅输出到控制台
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.StreamHandler()
]
)
class Query_User_000x: class Query_User_000x:
tags = ['用户管理功能' , '后台管理功能','预定系统功能'] tags = ['用户管理功能' , '后台管理功能','预定系统功能']
""" """
......
...@@ -17,15 +17,6 @@ current_dir = os.path.dirname(os.path.abspath(__file__)) ...@@ -17,15 +17,6 @@ current_dir = os.path.dirname(os.path.abspath(__file__))
# 构建CSV文件的绝对路径 # 构建CSV文件的绝对路径
csv_file_path = os.path.join(current_dir, '../../../测试数据/账号管理/用户管理模块/用户删除.csv') csv_file_path = os.path.join(current_dir, '../../../测试数据/账号管理/用户管理模块/用户删除.csv')
# 配置日志记录器,仅输出到控制台
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.StreamHandler()
]
)
class Delete_User_000x: class Delete_User_000x:
tags = ['用户管理功能' , '后台管理功能','预定系统功能'] tags = ['用户管理功能' , '后台管理功能','预定系统功能']
""" """
......
...@@ -21,15 +21,6 @@ current_dir = os.path.dirname(os.path.abspath(__file__)) ...@@ -21,15 +21,6 @@ current_dir = os.path.dirname(os.path.abspath(__file__))
# 构建CSV文件的绝对路径 # 构建CSV文件的绝对路径
csv_file_path = os.path.join(current_dir, '../../../测试数据/账号管理/用户管理模块/用户状态设置.csv') csv_file_path = os.path.join(current_dir, '../../../测试数据/账号管理/用户管理模块/用户状态设置.csv')
# 配置日志记录器,仅输出到控制台
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.StreamHandler()
]
)
class User_Status_000x: class User_Status_000x:
tags = ['用户管理功能' , '后台管理功能','预定系统功能'] tags = ['用户管理功能' , '后台管理功能','预定系统功能']
""" """
......
...@@ -21,14 +21,6 @@ current_dir = os.path.dirname(os.path.abspath(__file__)) ...@@ -21,14 +21,6 @@ current_dir = os.path.dirname(os.path.abspath(__file__))
# 构建CSV文件的绝对路径 # 构建CSV文件的绝对路径
csv_file_path = os.path.join(current_dir, '../../../测试数据/账号管理/用户管理模块/用户编辑.csv') csv_file_path = os.path.join(current_dir, '../../../测试数据/账号管理/用户管理模块/用户编辑.csv')
# 配置日志记录器,仅输出到控制台
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.StreamHandler()
]
)
class User_Edit_000x: class User_Edit_000x:
tags = ['用户管理功能' , '后台管理功能','预定系统功能'] tags = ['用户管理功能' , '后台管理功能','预定系统功能']
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论