Skip to content

Commit

Permalink
更新部分日志输出信息的样式 (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-clan authored Sep 2, 2023
1 parent b14a4e5 commit 555d27d
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 22 deletions.
11 changes: 8 additions & 3 deletions httpfpt/common/send_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import json
import time
from json import JSONDecodeError
from typing import Literal

import allure
import httpx
Expand Down Expand Up @@ -104,9 +105,10 @@ def send_request(
self,
request_data: dict,
*,
request_engin: str = 'requests',
request_engin: Literal['requests', 'httpx'] = 'requests',
log_data: bool = True,
allure_data: bool = True,
relate_testcase: bool = False,
**kwargs,
) -> dict:
"""
Expand All @@ -116,6 +118,7 @@ def send_request(
:param request_engin: 请求引擎
:param log_data: 日志记录数据
:param allure_data: allure 记录数据
:param relate_testcase: 关联测试用例
:return: response
"""
if request_engin not in get_enum_values(EnginType):
Expand All @@ -126,6 +129,8 @@ def send_request(
request_data_parse = RequestDataParse(request_data, request_engin)
parsed_data = request_data_parse.get_request_data_parsed
log.info('请求数据解析完成')
if not relate_testcase:
log.info(f'🏷️ Case ID: {parsed_data["case_id"]}')

# 记录请求前置数据; 请注意: 此处数据中如果包含关联用例变量, 不会被替换为结果记录, 因为替换动作还未发生
if log_data:
Expand Down Expand Up @@ -191,7 +196,7 @@ def send_request(
elif request_engin == EnginType.httpx:
response = self._httpx_engin(**request_conf, **request_data_parsed, **kwargs)
else:
raise ValueError('请求发起失败,使用了不合法的请求引擎')
raise ValueError('请求发起失败,请使用合法的请求引擎')

# 记录响应数据
response_data['url'] = str(response.url)
Expand All @@ -202,6 +207,7 @@ def send_request(
try:
json_data = response.json()
except JSONDecodeError:
log.warning('响应数据解析失败,响应数据不是有效的 json 格式')
json_data = {}
response_data['json'] = json.dumps(json_data)
response_data['content'] = response.content.decode('utf-8')
Expand Down Expand Up @@ -257,7 +263,6 @@ def log_request_up(parsed_data: dict) -> None:
log.info(f"用例 env: {parsed_data['env']}")
log.info(f"用例 module: {parsed_data['module']}")
log.info(f"用例 name: {parsed_data['name']}")
log.info(f"用例 case_id: {parsed_data['case_id']}")
log.info(f"用例 description: {parsed_data['description']}")
log.info(f"请求 method: {parsed_data['method']}")
log.info(f"请求 url: {parsed_data['url']}")
Expand Down
21 changes: 11 additions & 10 deletions httpfpt/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ def session_fixture(tmp_path_factory, worker_id):

@pytest.fixture(scope='package', autouse=True)
def package_fixture():
log.info('START')
log.info('🚀 START')
yield
log.info('-------------------------------------------------------------------------')
log.info('测试用例执行结束')
# 自动清理临时变量
log.info('') # 预留空行
log.info('🏁 FINISH')

# 清理临时变量
VariableCache().clear()
log.info('FINISH')


@pytest.fixture(scope='module', autouse=True)
Expand All @@ -47,12 +47,14 @@ def class_fixture():

@pytest.fixture(scope='function', autouse=True)
def function_fixture(request):
log.info(f'----------------- Running case func: {request.function.__name__} -----------------')
log.info('') # 预留空行
log.info(f'🔥 Running: {request.function.__name__}')

def log_end():
log.info('end')
def testcase_end():
log.info('🔚 End')

request.addfinalizer(log_end) # teardown终结函数 == yield后的代码
# teardown终结函数 == yield后的代码
request.addfinalizer(testcase_end)


def pytest_configure(config):
Expand Down Expand Up @@ -81,7 +83,6 @@ def pytest_html_results_summary(prefix):
:return:
"""
# 向 html 报告中的 summary 添加额外信息
# prefix.extend([html.p(f"Department:")])
prefix.extend([html.p(f'Tester: {TESTER_NAME}')])


Expand Down
8 changes: 4 additions & 4 deletions httpfpt/db/redis_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ def init(self) -> None:
try:
self.redis.ping()
except TimeoutError:
log.error('数据库 redis 连接超时')
log.error('数据库 redis 连接超时')
sys.exit(1)
except AuthenticationError:
log.error('数据库 redis 授权认证错误')
log.error('数据库 redis 授权认证错误')
sys.exit(1)
except Exception as e:
log.error(f'数据库 redis 连接异常: {e}')
log.error(f'数据库 redis 连接异常: {e}')
sys.exit(1)
else:
log.info('数据库 redis 连接成功')
log.info('数据库 redis 连接成功')

def get(self, key: Any) -> Any:
"""
Expand Down
8 changes: 4 additions & 4 deletions httpfpt/utils/relate_testcase_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,10 @@ def relate_testcase_set_var(testcase_data: dict) -> None:
"""
from httpfpt.common.send_request import send_request

msg = '执行变量提取关联测试用例:{}'.format(testcase_data['test_steps']['case_id'])
msg = '🔗 执行关联测试用例变量提取:{}'.format(testcase_data['test_steps']['case_id'])
log.debug(msg)
allure_step(msg, '此文件为空')
response = send_request.send_request(testcase_data, log_data=False)
response = send_request.send_request(testcase_data, log_data=False, relate_testcase=True)
value = jsonpath(response, testcase_data['set_var_jsonpath'])
if value:
VariableCache().set(testcase_data['set_var_key'], value[0])
Expand All @@ -258,7 +258,7 @@ def relate_testcase_exec(testcase_data: dict) -> None:
"""
from httpfpt.common.send_request import send_request

msg = '执行关联测试用例:{}'.format(testcase_data['test_steps']['case_id'])
msg = '🔗 执行关联测试用例:{}'.format(testcase_data['test_steps']['case_id'])
log.debug(msg)
allure_step(msg, '此文件为空')
send_request.send_request(testcase_data, log_data=False)
send_request.send_request(testcase_data, log_data=False, relate_testcase=True)
2 changes: 1 addition & 1 deletion httpfpt/utils/request/hooks_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ def hook_func_value_replace(self, target: dict) -> Any:
except Exception as e:
log.error(f'请求数据函数 {hook_key} 返回值替换失败: {e}')
raise e

dict_target = eval(str_target)

# 临时解决方案:数据还原
Expand Down Expand Up @@ -95,4 +94,5 @@ def exec_any_code(code: str) -> bool:
exec('import os')
exec('import sys')
result = eval(code)
log.info(f'执行代码:{code}')
return result

0 comments on commit 555d27d

Please sign in to comment.