优化settings.py中配置项名称

This commit is contained in:
wangjie 2025-04-10 11:26:44 +08:00
parent 219fa1c91e
commit f15a830abc
2 changed files with 58 additions and 55 deletions

View File

@ -16,13 +16,13 @@ args = command_parser()
ENV = (args.env or "test").upper()
# 失败重跑次数
rerun = 2
RERUN_COUNT = 2
# 失败重跑间隔时间
reruns_delay = 5
RERUN_DELAY_SECONDS = 5
# 当用例达到最大失败数,整个测试停止执行
max_fail = 100
# 当本次测试流程所有失败用例达到最大失败数,停止执行
MAX_FAIL_COUNT = 100
# 设置是否需要发送邮件Ture发送False不发送如果命令行中有 send_email 参数并且有效使用命令行的值否则使用else后的默认值
IS_SEND_EMAIL = args.send_email == 'true' if args.send_email else False
@ -39,7 +39,7 @@ LOG_CONSOLE = True
# ------------------------------------ 邮件配置信息 ----------------------------------------------------#
# 发送邮件配置信息
email_config = {
EMAIL_CONFIG = {
'mail_subject': '接口自动化测试报告', # 邮件标题
'sender_username': 'xxxxx@qq.com', # 发件人邮箱
'sender_password': 'ASDsdasda', # 发件人邮箱授权码
@ -49,53 +49,53 @@ email_config = {
}
# 邮件通知内容
email_content = """
各位同事, 大家好:<br>
EMAIL_CONTENT = f"""
各位同事, 大家好:<br>
自动化用例于 <strong>${start_time}</strong> 开始运行运行时长<strong>${duration}s</strong> 目前已执行完成<br>
--------------------------------------------------------------------------------------------------------<br>
项目名称<strong>%s</strong> <br>
构件编号<strong>#%s</strong><br>
项目环境<strong>%s</strong><br>
--------------------------------------------------------------------------------------------------------<br>
执行结果如下:<br>
&nbsp;&nbsp;用例运行总数:<strong> ${total}</strong><br>
&nbsp;&nbsp;通过用例数passed: <strong><font color="green" >${passed}</font></strong><br>
&nbsp;&nbsp;重试通过用例数rerun: <strong><font color="green" >${rerun}</font></strong><br>
&nbsp;&nbsp;失败用例数failed: <strong><font color="red" >${failed}</font></strong><br>
&nbsp;&nbsp;报错用例数error: <strong><font color="orange" >${error}</font></strong><br>
&nbsp;&nbsp;跳过用例数skipped: <strong><font color="grey" >${skipped}</font></strong><br>
&nbsp;&nbsp;预期失败用例数xfailed: <strong><font color="grey" >${xfailed}</font></strong><br>
&nbsp;&nbsp;预期通过用例数xpassed: <strong><font color="grey" >${xpassed}</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>
自动化用例于 <strong>${{start_time}}</strong> 开始运行运行时长<strong>${{duration}}s</strong>目前已执行完成<br>
--------------------------------------------------------------------------------------------------------<br>
项目名称<strong>{ProjectName}</strong> <br>
构件编号<strong>#{BUILD_NUMBER}</strong><br>
项目环境<strong>{ENV}</strong><br>
--------------------------------------------------------------------------------------------------------<br>
执行结果如下:<br>
&nbsp;&nbsp;用例运行总数:<strong> ${{total}}</strong><br>
&nbsp;&nbsp;通过用例数passed: <strong><font color="green">${{passed}}</font></strong><br>
&nbsp;&nbsp;重试通过用例数rerun: <strong><font color="green">${{rerun}}</font></strong><br>
&nbsp;&nbsp;失败用例数failed: <strong><font color="red">${{failed}}</font></strong><br>
&nbsp;&nbsp;报错用例数error: <strong><font color="orange">${{error}}</font></strong><br>
&nbsp;&nbsp;跳过用例数skipped: <strong><font color="grey">${{skipped}}</font></strong><br>
&nbsp;&nbsp;预期失败用例数xfailed: <strong><font color="grey">${{xfailed}}</font></strong><br>
&nbsp;&nbsp;预期通过用例数xpassed: <strong><font color="grey">${{xpassed}}</font></strong><br>
&nbsp;&nbsp;通过率: <strong><font color="green">${{pass_rate}}%%</font></strong><br>
&nbsp;&nbsp;测试报告点击查看: <a href='{ALLURE_URL}'>[测试报告入口]</a><br>
&nbsp;&nbsp;构建详情点击查看: <a href='{BUILD_URL}'>[控制台入口]</a><br>
**********************************<br>
附件为具体的测试报告详细情况可下载附件查看 非相关负责人员可忽略此消息谢谢
""" % (ProjectName, BUILD_NUMBER, ENV, ALLURE_URL, BUILD_URL)
**********************************<br>
附件为具体的测试报告详细情况可下载附件查看非相关负责人员可忽略此消息谢谢
"""
# ------------------------------------ 企业微信相关配置 ----------------------------------------------------#
# 企业微信通知群聊
wechat_webhook_url = ["https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxxxxxxxx"]
WECHAT_WEBHOOK_URLS = ["https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxxxxxxxx"]
# 企业微信通知内容
wechat_content = """******用例执行结果统计******
> 项目名称:%s
> 构件编号:#%s
> 测试环境:%s
> 总用例数<font color=\"info\">${total}条</font>
> 通过用例数<font color=\"info\">${passed}条</font>
> 重试通过用例数<font color=\"info\">${rerun}条</font>
> 失败用例数<font color=\"red\">${failed}条</font>
> 报错用例数<font color=\"red\">${error}条</font>
> 跳过用例数<font color=\"warning\">${skipped}条</font>
> 预期失败用例数<font color=\"comment\">${xfailed}条</font>
> 预期通过用例数<font color=\"comment\">${xpassed}条</font>
> 通过率<font color=\"info\">${pass_rate}%%</font>
> 用例开始时间:<font color=\"info\">${start_time}</font>
> 用例执行时长<font color=\"info\">${duration}s</font>
> 测试报告点击查看>>[测试报告入口](%s)
> 构建详情点击查看>>[控制台入口](%s)
> <@汪杰>""" % (ProjectName, BUILD_NUMBER, ENV, ALLURE_URL, BUILD_URL)
WECHAT_CONTENT = f"""******用例执行结果统计******
> 项目名称: {ProjectName}
> 构件编号: #{BUILD_NUMBER}
> 测试环境: {ENV}
> 总用例数<font color="info">${{total}}</font>
> 通过用例数<font color="info">${{passed}}</font>
> 重试通过用例数<font color="info">${{rerun}}</font>
> 失败用例数<font color="red">${{failed}}</font>
> 报错用例数<font color="red">${{error}}</font>
> 跳过用例数<font color="warning">${{skipped}}</font>
> 预期失败用例数<font color="comment">${{xfailed}}</font>
> 预期通过用例数<font color="comment">${{xpassed}}</font>
> 通过率<font color="info">${{pass_rate}}%%</font>
> 用例开始时间: <font color="info">${{start_time}}</font>
> 用例执行时长<font color="info">${{duration}}s</font>
> 测试报告点击查看 >> [测试报告入口]({ALLURE_URL})
> 构建详情点击查看 >> [控制台入口]({BUILD_URL})
"""

19
run.py
View File

@ -17,8 +17,8 @@ import pytest
from common.base_log import logger
from common.mail_sender import MailSender
from common.robot_sender import EnterpriseWechatNotification
from common.settings import IS_SEND_EMAIL, IS_SEND_WECHAT, wechat_webhook_url, wechat_content, email_content, \
email_config, max_fail, rerun, reruns_delay
from common.settings import IS_SEND_EMAIL, IS_SEND_WECHAT, WECHAT_WEBHOOK_URLS, WECHAT_CONTENT, EMAIL_CONTENT, \
EMAIL_CONFIG, MAX_FAIL_COUNT, RERUN_COUNT, RERUN_DELAY_SECONDS
from configs.paths_config import TEMP_DIR, PYTEST_REPORT_DIR, PYTEST_RESULT_DIR, ALLURE_REPORT_DIR, \
FILES_DIR
from utils.allure_handle import AllureReportBeautiful
@ -41,7 +41,8 @@ if __name__ == '__main__':
# '-q', # 代表 "quiet",即安静模式,它可以将 pytest 的输出精简化,只输出测试用例的执行结果,而不会输出额外的信息,如测试用例的名称、执行时间等等
'-vs', # 指定输出用例执行信息并打印程序中的print/logging输出
'testCase/', # 执行用例的目录
f"--maxfail={max_fail}", f"--reruns={rerun}", f"--reruns-delay={reruns_delay}", # 指定最大失败次数、重运行次数和重运行间隔时间
f"--maxfail={MAX_FAIL_COUNT}", # 指定最大失败次数
f"--reruns={RERUN_COUNT}", f"--reruns-delay={RERUN_DELAY_SECONDS}", # 指定重运行次数和重运行间隔时间
'--alluredir', f'{TEMP_DIR}', '--clean-alluredir', # 先清空旧的alluredir目录再将生成Allure原始报告需要的数据,并存放在 /Temp 目录
f'--html={os.path.join(PYTEST_REPORT_DIR, "pytest_report.html")}', # 指定pytest-html报告的存放位置
'--self-contained-html', # 将css样式合并到pytest-html报告文件中便于发送邮件
@ -72,14 +73,16 @@ if __name__ == '__main__':
FileHandle.copy_file(allure_files + os.sep + '查看allure报告方法', ALLURE_REPORT_DIR)
# ------------------------------发送通知消息----------------------------------
# 发送企业微信群聊
# 获取pytest-json报告数据
pytest_result = asdict(ReportDataHandle.pytest_json_report_case_count())
# 发送企业微信群聊
if IS_SEND_WECHAT: # 判断是否需要发送企业微信
EnterpriseWechatNotification(wechat_webhook_url).send_markdown(
DataProcessor().process_data(wechat_content, pytest_result))
EnterpriseWechatNotification(WECHAT_WEBHOOK_URLS).send_markdown(
DataProcessor().process_data(WECHAT_CONTENT, pytest_result))
# 发送邮件
if IS_SEND_EMAIL: # 判断是否需要发送邮件
file_path = PYTEST_REPORT_DIR + os.sep + 'pytest_report.html'
ms = MailSender(email_config)
ms.attach_text(DataProcessor().process_data(email_content, pytest_result)).attach_file(file_path).send()
ms = MailSender(EMAIL_CONFIG)
ms.attach_text(DataProcessor().process_data(EMAIL_CONTENT, pytest_result)).attach_file(file_path).send()