Skip to content

Commit

Permalink
更新项目配置全局变量名
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-clan committed Mar 18, 2024
1 parent 2f92510 commit be92116
Show file tree
Hide file tree
Showing 18 changed files with 149 additions and 144 deletions.
22 changes: 11 additions & 11 deletions httpfpt/common/send_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

from httpfpt.common.errors import AssertError, SendRequestError
from httpfpt.common.log import log
from httpfpt.core.get_conf import config
from httpfpt.core.get_conf import httpfpt_config
from httpfpt.db.mysql_db import mysql_client
from httpfpt.enums.request.body import BodyType
from httpfpt.enums.request.engin import EnginType
Expand Down Expand Up @@ -63,11 +63,11 @@ def _requests_engin(**kwargs) -> RequestsResponse:
:param kwargs:
:return:
"""
kwargs['timeout'] = kwargs['timeout'] or config.REQUEST_TIMEOUT
kwargs['verify'] = kwargs['verify'] or config.REQUEST_VERIFY
kwargs['proxies'] = kwargs['proxies'] or config.REQUEST_PROXIES_REQUESTS
kwargs['allow_redirects'] = kwargs['allow_redirects'] or config.REQUEST_REDIRECTS
request_retry = kwargs['retry'] or config.REQUEST_RETRY
kwargs['timeout'] = kwargs['timeout'] or httpfpt_config.REQUEST_TIMEOUT
kwargs['verify'] = kwargs['verify'] or httpfpt_config.REQUEST_VERIFY
kwargs['proxies'] = kwargs['proxies'] or httpfpt_config.REQUEST_PROXIES_REQUESTS
kwargs['allow_redirects'] = kwargs['allow_redirects'] or httpfpt_config.REQUEST_REDIRECTS
request_retry = kwargs['retry'] or httpfpt_config.REQUEST_RETRY
del kwargs['retry']
# 消除安全警告
requests.packages.urllib3.disable_warnings() # type: ignore
Expand All @@ -93,11 +93,11 @@ def _httpx_engin(**kwargs) -> HttpxResponse:
:param kwargs:
:return:
"""
kwargs['timeout'] = kwargs['timeout'] or config.REQUEST_TIMEOUT
verify = kwargs['verify'] or config.REQUEST_VERIFY
proxies = kwargs['proxies'] or config.REQUEST_PROXIES_HTTPX
redirects = kwargs['allow_redirects'] or config.REQUEST_REDIRECTS
request_retry = kwargs['retry'] or config.REQUEST_RETRY
kwargs['timeout'] = kwargs['timeout'] or httpfpt_config.REQUEST_TIMEOUT
verify = kwargs['verify'] or httpfpt_config.REQUEST_VERIFY
proxies = kwargs['proxies'] or httpfpt_config.REQUEST_PROXIES_HTTPX
redirects = kwargs['allow_redirects'] or httpfpt_config.REQUEST_REDIRECTS
request_retry = kwargs['retry'] or httpfpt_config.REQUEST_RETRY
del kwargs['verify']
del kwargs['proxies']
del kwargs['allow_redirects']
Expand Down
8 changes: 4 additions & 4 deletions httpfpt/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from httpfpt.common.log import log
from httpfpt.common.variable_cache import variable_cache
from httpfpt.common.yaml_handler import write_yaml_report
from httpfpt.core.get_conf import config as sys_config
from httpfpt.core.get_conf import httpfpt_config


@pytest.fixture(scope='session', autouse=True)
Expand Down Expand Up @@ -62,7 +62,7 @@ def pytest_configure(config):
if metadata:
from pytest_metadata.plugin import metadata_key

config.stash[metadata_key]['Project Name'] = sys_config.PROJECT_NAME
config.stash[metadata_key]['Project Name'] = httpfpt_config.PROJECT_NAME
del config.stash[metadata_key]['Packages']
del config.stash[metadata_key]['Platform']
del config.stash[metadata_key]['Plugins']
Expand All @@ -76,7 +76,7 @@ def pytest_html_results_summary(prefix):
:return:
"""
# 向 html 报告中的 summary 添加额外信息
prefix.extend([html.p(f'Tester: {sys_config.TESTER_NAME}')])
prefix.extend([html.p(f'Tester: {httpfpt_config.TESTER_NAME}')])


@pytest.mark.optionalhook
Expand All @@ -87,7 +87,7 @@ def pytest_html_report_title(report):
:param report:
:return:
"""
report.title = f'{sys_config.TEST_REPORT_TITLE}'
report.title = f'{httpfpt_config.TEST_REPORT_TITLE}'


@pytest.mark.optionalhook
Expand Down
106 changes: 53 additions & 53 deletions httpfpt/core/get_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,101 +10,101 @@

class Config:
def __init__(self) -> None:
self.__config = read_toml(str(Path(__file__).resolve().parent), 'conf.toml')
self._config = read_toml(str(Path(__file__).resolve().parent), 'conf.toml')

# 项目目录名
self.PROJECT_NAME = glom(self.__config, 'project.name')
self.PROJECT_NAME = glom(self._config, 'project.name')

# 测试报告
self.TEST_REPORT_TITLE = glom(self.__config, 'report.title')
self.TESTER_NAME = glom(self.__config, 'report.tester_name')
self.TEST_REPORT_TITLE = glom(self._config, 'report.title')
self.TESTER_NAME = glom(self._config, 'report.tester_name')

# mysql 数据库
self.MYSQL_HOST = glom(self.__config, 'mysql.host')
self.MYSQL_PORT = glom(self.__config, 'mysql.port')
self.MYSQL_USER = glom(self.__config, 'mysql.user')
self.MYSQL_PASSWORD = glom(self.__config, 'mysql.password')
self.MYSQL_DATABASE = glom(self.__config, 'mysql.database')
self.MYSQL_CHARSET = glom(self.__config, 'mysql.charset')
self.MYSQL_HOST = glom(self._config, 'mysql.host')
self.MYSQL_PORT = glom(self._config, 'mysql.port')
self.MYSQL_USER = glom(self._config, 'mysql.user')
self.MYSQL_PASSWORD = glom(self._config, 'mysql.password')
self.MYSQL_DATABASE = glom(self._config, 'mysql.database')
self.MYSQL_CHARSET = glom(self._config, 'mysql.charset')

# redis 数据库
self.REDIS_HOST = glom(self.__config, 'redis.host')
self.REDIS_PORT = glom(self.__config, 'redis.port')
self.REDIS_PASSWORD = glom(self.__config, 'redis.password')
self.REDIS_DATABASE = glom(self.__config, 'redis.database')
self.REDIS_TIMEOUT = glom(self.__config, 'redis.timeout')
self.REDIS_HOST = glom(self._config, 'redis.host')
self.REDIS_PORT = glom(self._config, 'redis.port')
self.REDIS_PASSWORD = glom(self._config, 'redis.password')
self.REDIS_DATABASE = glom(self._config, 'redis.database')
self.REDIS_TIMEOUT = glom(self._config, 'redis.timeout')

# 邮件
self.EMAIL_SERVER = glom(self.__config, 'email.host')
self.EMAIL_PORT = glom(self.__config, 'email.port')
self.EMAIL_USER = glom(self.__config, 'email.user')
self.EMAIL_PASSWORD = glom(self.__config, 'email.password')
self.EMAIL_SEND_TO = glom(self.__config, 'email.receiver')
self.EMAIL_SSL = glom(self.__config, 'email.ssl')
self.EMAIL_SEND = glom(self.__config, 'email.send')
self.EMAIL_SERVER = glom(self._config, 'email.host')
self.EMAIL_PORT = glom(self._config, 'email.port')
self.EMAIL_USER = glom(self._config, 'email.user')
self.EMAIL_PASSWORD = glom(self._config, 'email.password')
self.EMAIL_SEND_TO = glom(self._config, 'email.receiver')
self.EMAIL_SSL = glom(self._config, 'email.ssl')
self.EMAIL_SEND = glom(self._config, 'email.send')

# 钉钉
self.DINGDING_WEBHOOK = glom(self.__config, 'dingding.webhook')
self.DINGDING_WEBHOOK = glom(self._config, 'dingding.webhook')
self.DINGDING_PROXY = {
'http': glom(self.__config, 'dingding.proxies.http')
if glom(self.__config, 'dingding.proxies.http') != ''
'http': glom(self._config, 'dingding.proxies.http')
if glom(self._config, 'dingding.proxies.http') != ''
else None,
'https': glom(self.__config, 'dingding.proxies.https')
if glom(self.__config, 'dingding.proxies.https') != ''
'https': glom(self._config, 'dingding.proxies.https')
if glom(self._config, 'dingding.proxies.https') != ''
else None,
}
self.DINGDING_SEND = glom(self.__config, 'dingding.send')
self.DINGDING_SEND = glom(self._config, 'dingding.send')

# 飞书
self.FEISHU_WEBHOOK = glom(self.__config, 'feishu.webhook')
self.FEISHU_WEBHOOK = glom(self._config, 'feishu.webhook')
self.FEISHU_PROXY = {
'http': glom(self.__config, 'feishu.proxies.http')
if glom(self.__config, 'feishu.proxies.http') != ''
'http': glom(self._config, 'feishu.proxies.http')
if glom(self._config, 'feishu.proxies.http') != ''
else None,
'https': glom(self.__config, 'feishu.proxies.https')
if glom(self.__config, 'feishu.proxies.https') != ''
'https': glom(self._config, 'feishu.proxies.https')
if glom(self._config, 'feishu.proxies.https') != ''
else None,
}
self.FEISHU_SEND = glom(self.__config, 'feishu.send')
self.FEISHU_SEND = glom(self._config, 'feishu.send')

# 企业微信
self.WECHAT_WEBHOOK = glom(self.__config, 'wechat.webhook')
self.WECHAT_WEBHOOK = glom(self._config, 'wechat.webhook')
self.WECHAT_PROXY = {
'http': glom(self.__config, 'wechat.proxies.http')
if glom(self.__config, 'wechat.proxies.http') != ''
'http': glom(self._config, 'wechat.proxies.http')
if glom(self._config, 'wechat.proxies.http') != ''
else None,
'https': glom(self.__config, 'wechat.proxies.https')
if glom(self.__config, 'wechat.proxies.https') != ''
'https': glom(self._config, 'wechat.proxies.https')
if glom(self._config, 'wechat.proxies.https') != ''
else None,
}
self.WECHAT_SEND = glom(self.__config, 'wechat.send')
self.WECHAT_SEND = glom(self._config, 'wechat.send')

# 请求发送
self.REQUEST_TIMEOUT = glom(self.__config, 'request.timeout')
self.REQUEST_VERIFY = glom(self.__config, 'request.verify')
self.REQUEST_REDIRECTS = glom(self.__config, 'request.redirects')
self.REQUEST_TIMEOUT = glom(self._config, 'request.timeout')
self.REQUEST_VERIFY = glom(self._config, 'request.verify')
self.REQUEST_REDIRECTS = glom(self._config, 'request.redirects')
self.REQUEST_PROXIES_REQUESTS = {
'http': glom(self.__config, 'request.proxies.http')
if glom(self.__config, 'request.proxies.http') != ''
'http': glom(self._config, 'request.proxies.http')
if glom(self._config, 'request.proxies.http') != ''
else None,
'https': glom(self.__config, 'request.proxies.https')
if glom(self.__config, 'request.proxies.https') != ''
'https': glom(self._config, 'request.proxies.https')
if glom(self._config, 'request.proxies.https') != ''
else None,
}
self.REQUEST_PROXIES_HTTPX = {
'http://': glom(self.__config, 'request.proxies.http')
if glom(self.__config, 'request.proxies.http') != ''
'http://': glom(self._config, 'request.proxies.http')
if glom(self._config, 'request.proxies.http') != ''
else None,
'https://': glom(self.__config, 'request.proxies.https')
if glom(self.__config, 'request.proxies.https') != ''
'https://': glom(self._config, 'request.proxies.https')
if glom(self._config, 'request.proxies.https') != ''
else None,
}
self.REQUEST_RETRY = glom(self.__config, 'request.retry')
self.REQUEST_RETRY = glom(self._config, 'request.retry')


@lru_cache(maxsize=None)
def cache_config() -> Config:
return Config()


config = cache_config()
httpfpt_config = cache_config()
14 changes: 7 additions & 7 deletions httpfpt/db/mysql_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from httpfpt.common.errors import SQLSyntaxError
from httpfpt.common.log import log
from httpfpt.core.get_conf import config
from httpfpt.core.get_conf import httpfpt_config
from httpfpt.enums.query_fetch_type import QueryFetchType
from httpfpt.enums.sql_type import SqlType
from httpfpt.utils.enum_control import get_enum_values
Expand All @@ -24,12 +24,12 @@ class MysqlDB:
def __init__(self) -> None:
self._pool = PooledDB(
pymysql,
host=config.MYSQL_HOST,
port=config.MYSQL_PORT,
user=config.MYSQL_USER,
password=config.MYSQL_PASSWORD,
database=config.MYSQL_DATABASE,
charset=config.MYSQL_CHARSET,
host=httpfpt_config.MYSQL_HOST,
port=httpfpt_config.MYSQL_PORT,
user=httpfpt_config.MYSQL_USER,
password=httpfpt_config.MYSQL_PASSWORD,
database=httpfpt_config.MYSQL_DATABASE,
charset=httpfpt_config.MYSQL_CHARSET,
maxconnections=15,
blocking=True, # 连接池中如果没有可用连接后,是否阻塞等待
autocommit=False, # 是否自动提交
Expand Down
12 changes: 6 additions & 6 deletions httpfpt/db/redis_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
from redis import AuthenticationError, Redis

from httpfpt.common.log import log
from httpfpt.core.get_conf import config
from httpfpt.core.get_conf import httpfpt_config


class RedisDB(Redis):
def __init__(self) -> None:
super().__init__(
host=config.REDIS_HOST,
port=config.REDIS_PORT,
password=config.REDIS_PASSWORD,
db=config.REDIS_DATABASE,
socket_timeout=config.REDIS_TIMEOUT,
host=httpfpt_config.REDIS_HOST,
port=httpfpt_config.REDIS_PORT,
password=httpfpt_config.REDIS_PASSWORD,
db=httpfpt_config.REDIS_DATABASE,
socket_timeout=httpfpt_config.REDIS_TIMEOUT,
decode_responses=True, # 转码 utf-8
)
self.prefix = 'httpfpt'
Expand Down
18 changes: 10 additions & 8 deletions httpfpt/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

from httpfpt.common.log import log
from httpfpt.common.yaml_handler import read_yaml
from httpfpt.core.get_conf import config
from httpfpt.core.get_conf import httpfpt_config
from httpfpt.core.path_conf import (
ALLURE_ENV_FILE,
ALLURE_REPORT_ENV_FILE,
Expand Down Expand Up @@ -57,7 +57,7 @@ def startup(

run_args = [log_level]

default_case_path = os.sep.join([TEST_CASE_PATH, config.PROJECT_NAME])
default_case_path = os.sep.join([TEST_CASE_PATH, httpfpt_config.PROJECT_NAME])
if case_path:
if '::' not in case_path:
raise ValueError(
Expand All @@ -76,7 +76,7 @@ def startup(
run_path = default_case_path
run_args.append(run_path)

html_report_filename = f'{config.PROJECT_NAME}_{get_current_time("%Y-%m-%d %H_%M_%S")}.html'
html_report_filename = f'{httpfpt_config.PROJECT_NAME}_{get_current_time("%Y-%m-%d %H_%M_%S")}.html'
if html_report:
if not os.path.exists(HTML_REPORT_PATH):
os.makedirs(HTML_REPORT_PATH)
Expand Down Expand Up @@ -123,7 +123,9 @@ def startup(
format_run_args.append(i)
run_pytest_command_args = ' '.join(_ for _ in format_run_args)

log.info(f'开始运行项目:{config.PROJECT_NAME}' if run_path == default_case_path else f'开始运行:{run_path}')
log.info(
f'开始运行项目:{httpfpt_config.PROJECT_NAME}' if run_path == default_case_path else f'开始运行:{run_path}'
)
log.info(f'Pytest CLI: pytest {run_pytest_command_args}')
log.info('🚀 START')
pytest.main(run_args)
Expand All @@ -133,16 +135,16 @@ def startup(
yaml_report_files.sort()
test_result = read_yaml(YAML_REPORT_PATH, filename=yaml_report_files[-1])

if html_report and config.EMAIL_SEND:
if html_report and httpfpt_config.EMAIL_SEND:
SendEmail(test_result, html_report_filename).send_report()

if config.DINGDING_SEND:
if httpfpt_config.DINGDING_SEND:
DingDing(test_result).send()

if config.FEISHU_SEND:
if httpfpt_config.FEISHU_SEND:
FeiShu(test_result).send()

if config.WECHAT_SEND:
if httpfpt_config.WECHAT_SEND:
WeChat(test_result).send()

if allure:
Expand Down
8 changes: 4 additions & 4 deletions httpfpt/utils/case_auto_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from pathlib import Path

from httpfpt.core.get_conf import config
from httpfpt.core.get_conf import httpfpt_config
from httpfpt.core.path_conf import TEST_CASE_PATH
from httpfpt.utils.file_control import get_file_property, search_all_case_data_files, search_all_testcase_files
from httpfpt.utils.rich_console import console
Expand Down Expand Up @@ -88,12 +88,12 @@ def {testcase_func_name}(self, data):
send_request.send_request(data)
'''
# 创建测试用例文件
tag = case_filename.split(config.PROJECT_NAME)[1].split(os.path.sep)[1:-1]
tag = case_filename.split(httpfpt_config.PROJECT_NAME)[1].split(os.path.sep)[1:-1]
new_testcase_filename = testcase_func_name + '.py'
if tag:
case_path = os.path.join(TEST_CASE_PATH, config.PROJECT_NAME, *tag, new_testcase_filename)
case_path = os.path.join(TEST_CASE_PATH, httpfpt_config.PROJECT_NAME, *tag, new_testcase_filename)
else:
case_path = os.path.join(TEST_CASE_PATH, config.PROJECT_NAME, new_testcase_filename)
case_path = os.path.join(TEST_CASE_PATH, httpfpt_config.PROJECT_NAME, new_testcase_filename)
new_testcase_dir = Path(case_path).parent
if not new_testcase_dir.exists():
new_testcase_dir.mkdir(parents=True, exist_ok=True)
Expand Down
4 changes: 2 additions & 2 deletions httpfpt/utils/cli/about_testcase.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from httpfpt.common.json_handler import read_json_file
from httpfpt.common.yaml_handler import read_yaml
from httpfpt.core.get_conf import config
from httpfpt.core.get_conf import httpfpt_config
from httpfpt.core.path_conf import CASE_DATA_PATH
from httpfpt.enums.case_data_type import CaseDataType
from httpfpt.schemas.case_data import CaseData
Expand Down Expand Up @@ -37,7 +37,7 @@ def testcase_data_verify(verify: str) -> None:
console.print(f'🔥 开始验证 {verify} 测试数据结构...')
file_type = get_file_property(verify)[2]
if os.path.isfile(verify):
data_path = os.path.join(CASE_DATA_PATH, config.PROJECT_NAME)
data_path = os.path.join(CASE_DATA_PATH, httpfpt_config.PROJECT_NAME)
if file_type == CaseDataType.JSON:
file_data = read_json_file(str(data_path), verify)
else:
Expand Down
Loading

0 comments on commit be92116

Please sign in to comment.