diff --git a/common/settings.py b/common/settings.py
index 91e6c13..692905e 100644
--- a/common/settings.py
+++ b/common/settings.py
@@ -5,18 +5,15 @@
# @File : settings.py
# @project : SensoroApi
-from common.models import Environment
+from utils.command_parser import command_parser
from utils.jenkins_handle import ProjectName, BUILD_NUMBER, ALLURE_URL, BUILD_URL
# ------------------------------------ 通用配置 ----------------------------------------------------#
-'''
-开发环境:Environment.DEV
-测试环境:Environment.TEST
-生产环境:Environment.PROD
-点军环境:Environment.DIANJUN
-'''
-# 设置运行环境
-ENV = Environment.TEST
+# 获取命令行参数
+args = command_parser()
+
+# 设置默认运行环境,如果命令行中有 env 参数并且有效,使用命令行的值,否则使用默认值,支持的环境参考env_config中配置的环境
+ENV = (args.env or "dev").upper()
# 失败重跑次数
rerun = 0
@@ -27,11 +24,11 @@ reruns_delay = 5
# 当用例达到最大失败数,整个测试停止执行
max_fail = 100
-# 设置是否需要发送邮件:Ture发送,False不发送
-IS_SEND_EMAIL = False
+# 设置是否需要发送邮件:Ture发送,False不发送,如果命令行中有 send_email 参数并且有效,使用命令行的值,否则使用默认值
+IS_SEND_EMAIL = args.send_email == 'True' if args.send_email else False
-# 设置是否需要发送企业微信消息:Ture发送,False不发送
-IS_SEND_WECHAT = False
+# 设置是否需要发送企业微信消息:Ture发送,False不发送,如果命令行中有 send_wechat 参数并且有效,使用命令行的值,否则使用默认值
+IS_SEND_WECHAT = args.send_wechat == 'True' if args.send_wechat else False
# 设置是否开启debug日志
LOG_DEBUG = False
@@ -75,7 +72,7 @@ email_content = """
**********************************
附件为具体的测试报告,详细情况可下载附件查看, 非相关负责人员可忽略此消息。谢谢。
- """ % (ProjectName, BUILD_NUMBER, ENV.name, ALLURE_URL, BUILD_URL)
+ """ % (ProjectName, BUILD_NUMBER, ENV, ALLURE_URL, BUILD_URL)
# ------------------------------------ 企业微信相关配置 ----------------------------------------------------#
@@ -99,4 +96,4 @@ wechat_content = """******用例执行结果统计******
> 用例执行时长:${duration}s
> 测试报告,点击查看>>[测试报告入口](%s)
> 构建详情,点击查看>>[控制台入口](%s)
- > <@汪杰>""" % (ProjectName, BUILD_NUMBER, ENV.name, ALLURE_URL, BUILD_URL)
+ > <@汪杰>""" % (ProjectName, BUILD_NUMBER, ENV, ALLURE_URL, BUILD_URL)
diff --git a/configs/env_config.py b/configs/env_config.py
index f169d0e..edb7048 100644
--- a/configs/env_config.py
+++ b/configs/env_config.py
@@ -7,7 +7,6 @@
import inspect
from common.settings import ENV
-from utils.command_parser import command_parser
class DevConfig:
@@ -44,9 +43,8 @@ class ProdConfig:
class EnvConfig:
"""环境配置入口"""
- # 获取命令行参数中指定的运行环境,如果没有的话就用settings中指定的环境
- args = command_parser()
- env = args.env.lower() if args.env else ENV.value
+ # 获取settings中指定的运行环境
+ _ENV = ENV
# 缓存 _ENV_CONFIGS 字典
_ENV_CONFIGS = None
@@ -57,19 +55,20 @@ class EnvConfig:
if cls._ENV_CONFIGS is None: # 只在第一次访问时生成
cls._ENV_CONFIGS = {}
for name, obj in globals().items():
- if inspect.isclass(obj) and name.endswith('Config'):
- env_name = name.lower().replace('config', '') # 自动获取环境名称(例如 DevConfig -> dev)
+ if inspect.isclass(obj) and name.endswith('Config') and name != 'EnvConfig':
+ env_name = name.upper().replace('CONFIG', '') # 自动获取环境名称(例如 DevConfig -> DEV)
cls._ENV_CONFIGS[env_name] = obj
+ return cls._ENV_CONFIGS
@classmethod
def get_config(cls):
"""获取指定环境的配置"""
cls._generate_env_configs() # 确保在第一次访问时生成配置字典
- if cls.env not in cls._ENV_CONFIGS:
+ if cls._ENV not in cls._ENV_CONFIGS:
raise ValueError(
- f'运行的环境 "{cls.env}" 不存在,请检查运行的环境参数,目前支持的环境有:{", ".join(cls._ENV_CONFIGS.keys())}'
+ f'运行的环境 "{cls._ENV}" 不存在,请检查运行的环境参数,目前支持的环境有:{", ".join(cls._ENV_CONFIGS.keys())}'
)
- return cls._ENV_CONFIGS.get(cls.env)
+ return cls._ENV_CONFIGS.get(cls._ENV)
@classmethod
def URL(cls):
@@ -88,5 +87,6 @@ class EnvConfig:
if __name__ == '__main__':
+ print(EnvConfig._generate_env_configs())
print(EnvConfig.URL())
print(EnvConfig.DEFAULT_HEADERS())
diff --git a/run.py b/run.py
index 4263b65..9ffd323 100755
--- a/run.py
+++ b/run.py
@@ -15,14 +15,13 @@ from dataclasses import asdict
import pytest
from common.base_log import logger
-from utils.allure_handle import AllureReportBeautiful
-from utils.command_parser import command_parser
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 configs.paths_config import BASE_DIR, TEMP_DIR, PYTEST_REPORT_DIR, PYTEST_RESULT_DIR, ALLURE_REPORT_DIR, \
+from configs.paths_config import TEMP_DIR, PYTEST_REPORT_DIR, PYTEST_RESULT_DIR, ALLURE_REPORT_DIR, \
FILES_DIR
+from utils.allure_handle import AllureReportBeautiful
from utils.data_handle import DataProcessor
from utils.file_handle import FileHandle
from utils.report_data_handle import ReportDataHandle
@@ -38,11 +37,6 @@ if __name__ == '__main__':
Starting ... ... ...
""")
- # 获取命令行参数中指定的通知状态,如果没有的话就用settings中指定的通知状态
- args = command_parser()
- IS_SEND_EMAIL = eval(args.send_email) if args.send_email else IS_SEND_EMAIL
- IS_SEND_WECHAT = eval(args.send_wechat) if args.send_wechat else IS_SEND_WECHAT
-
pytest.main([
# '-q', # 代表 "quiet",即安静模式,它可以将 pytest 的输出精简化,只输出测试用例的执行结果,而不会输出额外的信息,如测试用例的名称、执行时间等等
'-vs', # 指定输出用例执行信息,并打印程序中的print/logging输出
diff --git a/utils/allure_handle.py b/utils/allure_handle.py
index acb07fe..5159d51 100644
--- a/utils/allure_handle.py
+++ b/utils/allure_handle.py
@@ -133,7 +133,7 @@ class AllureReportBeautiful:
"""
# 需要写入的环境信息
allure_env = {
- 'OperatingEnvironment': ENV.name,
+ 'OperatingEnvironment': ENV,
'BaseUrl': EnvConfig.URL(),
'PythonVersion': platform.python_version(),
'Platform': platform.platform(),
diff --git a/utils/command_parser.py b/utils/command_parser.py
index dea141b..8fb4388 100644
--- a/utils/command_parser.py
+++ b/utils/command_parser.py
@@ -19,11 +19,11 @@ def command_parser():
help='指定是否需要发送企业微信群消息')
parser.add_argument('-e', '--send-email', choices=['False', 'True'], type=str, default=None,
help='指定是否需要发送邮件')
- parser.add_argument('-env', '--env', choices=['DEV', 'TEST', 'PROD'], type=str, default=None,
- help='指定运行环境')
+ parser.add_argument('-env', '--env', choices=['DEV', 'TEST', 'PROD'], type=lambda s: s.upper(), default=None,
+ help='指定运行环境,并支持大小输入')
# 解析命令行参数
- args,unknown_args = parser.parse_known_args()
+ args, unknown_args = parser.parse_known_args()
return args