From f0e38e9c3105da4de5858804f77dbc63c3eb15f0 Mon Sep 17 00:00:00 2001 From: aaronchenyongzhi <262667641@qq.com> Date: Fri, 16 Jun 2023 02:28:03 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=B5=8B=E8=AF=95=E8=84=9A?= =?UTF-8?q?=E6=9C=AC=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- OutPut/Log/2023-06-16.log | 6 +++ OutPut/Log/2023-06-16_error.logger | 2 + OutPut/Log/2023-06-16_info.logger | 2 + common/utils/load_and_execute_script.py | 2 + test_script/automation/__init__.py | 3 +- test_script/automation/test_api.py | 61 ++++++++++--------------- 6 files changed, 37 insertions(+), 39 deletions(-) create mode 100644 OutPut/Log/2023-06-16_error.logger create mode 100644 OutPut/Log/2023-06-16_info.logger diff --git a/OutPut/Log/2023-06-16.log b/OutPut/Log/2023-06-16.log index 26a2658..0fe70cf 100644 --- a/OutPut/Log/2023-06-16.log +++ b/OutPut/Log/2023-06-16.log @@ -775,3 +775,9 @@ 2023-06-16 02:14:44 | INFO | | called get_init | args: ('E:\\apitest\\cases\\cases\\test_api.xlsx',) kwargs:{} 2023-06-16 02:14:44 | ERROR | | called get_init | error: 读取excel中初始化数据异常: "There is no item named 'xl/sharedStrings.xml' in the archive" 2023-06-16 02:14:44 | INFO | -----------分割线----------- +2023-06-16 02:24:12 | INFO | -----------分割线----------- +2023-06-16 02:24:12 | INFO | | called load_and_execute_script | args: ('E:\\apitest\\scripts', 'request_script_sheetname_id.py', 'setup', {'y': 'z'}) kwargs:{} +2023-06-16 02:24:12 | INFO | -----------分割线----------- +2023-06-16 02:24:12 | INFO | | called load_script | args: ('E:\\apitest\\scripts\\request_script_sheetname_id.py',) kwargs:{} +2023-06-16 02:24:12 | INFO | | end called load_script | return:, duration:0.000835s +2023-06-16 02:24:12 | INFO | | end called load_and_execute_script | return:{'y': 'z'}, duration:0.003896s diff --git a/OutPut/Log/2023-06-16_error.logger b/OutPut/Log/2023-06-16_error.logger new file mode 100644 index 0000000..06cdabc --- /dev/null +++ b/OutPut/Log/2023-06-16_error.logger @@ -0,0 +1,2 @@ +2023-06-16 02:22:02,537-ERROR-logger.py-[ line:62 ] - 日志信息:读取excel中初始化数据异常: "There is no item named 'xl/sharedStrings.xml' in the archive" +2023-06-16 02:26:50,713-ERROR-logger.py-[ line:62 ] - 日志信息:读取excel中初始化数据异常: "There is no item named 'xl/sharedStrings.xml' in the archive" diff --git a/OutPut/Log/2023-06-16_info.logger b/OutPut/Log/2023-06-16_info.logger new file mode 100644 index 0000000..b23feed --- /dev/null +++ b/OutPut/Log/2023-06-16_info.logger @@ -0,0 +1,2 @@ +2023-06-16 02:22:02,534-INFO-logger.py-[ line:64 ] - 日志信息:读取测试用例excel文件:E:\apitest\cases\cases\test_api.xlsx +2023-06-16 02:26:50,710-INFO-logger.py-[ line:64 ] - 日志信息:读取测试用例excel文件:E:\apitest\cases\cases\test_api.xlsx diff --git a/common/utils/load_and_execute_script.py b/common/utils/load_and_execute_script.py index bd6faf2..dd95b40 100644 --- a/common/utils/load_and_execute_script.py +++ b/common/utils/load_and_execute_script.py @@ -1,6 +1,8 @@ import importlib.util import os +import sys +sys.path.append('../../common') from common.utils import logger diff --git a/test_script/automation/__init__.py b/test_script/automation/__init__.py index 1dd7909..ef7641d 100644 --- a/test_script/automation/__init__.py +++ b/test_script/automation/__init__.py @@ -2,6 +2,7 @@ import sys sys.path.append("../../../") sys.path.append("../../common") + from common.config import Config from common.validation.validator import Validator from common.file_handling.get_excel_init import get_init @@ -9,7 +10,7 @@ from common.data_extraction.dependent_parameter import DependentParameter from common.database.mysql_client import MysqlClient from common.utils.mylogger import MyLogger from common.dependence import Dependence -from common.validation import loaders + test_file = Config.test_api # 获取 excel 文件路径 excel_handle, init_data, test_case = get_init(test_file) diff --git a/test_script/automation/test_api.py b/test_script/automation/test_api.py index 7d723fd..1faf7a1 100644 --- a/test_script/automation/test_api.py +++ b/test_script/automation/test_api.py @@ -6,30 +6,15 @@ from ddt import ddt, data sys.path.append("../../../") sys.path.append("../../common") -from common.config import Config -from common.file_handling.get_excel_init import get_init -from common.data_extraction.dependent_parameter import DependentParameter -from common.data_extraction.data_extractor import DataExtractor -from common.crypto.encryption_main import do_encrypt -from common.database.do_mysql import DoMysql -from common.utils.http_client import http_client -from common.utils.mylogger import MyLogger +from test_script.automation import * from common.validation import loaders -from common.dependence import Dependence as dep -from common.validation.validator import Validator from common import bif_functions +from common.crypto.encryption_main import do_encrypt +from common.data_extraction.data_extractor import DataExtractor +from common.http_client.http_client import http_client from common.utils.load_and_execute_script import load_and_execute_script -test_file = Config.test_api # 获取 excel 文件路径 -excel_handle, init_data, test_case = get_init(test_file) -databases = init_data.get('databases') # 获取数据库配置信息 -mysql = DoMysql(databases) # 初始化 mysql 链接 -dep.set_dep(eval(init_data.get("initialize_data"))) # 初始化依赖表 -dep_par = DependentParameter() # 参数提取类实例化 -log = MyLogger() -host = init_data.get('host', "") + init_data.get("path", "") - @ddt class TestProjectApi(unittest.TestCase): @@ -37,10 +22,10 @@ class TestProjectApi(unittest.TestCase): @classmethod def setUpClass(cls) -> None: - log.info("开始加载内置方法...") + logger.info("开始加载内置方法...") loaders.set_bif_fun(bif_functions) # 加载内置方法 - log.info("内置方法加载完成") - log.info(f"所有用例执行开始...") + logger.info("内置方法加载完成") + logger.info(f"所有用例执行开始...") @data(*test_case) # {"":""} def test_api(self, item): # item = {測試用例} @@ -61,17 +46,18 @@ class TestProjectApi(unittest.TestCase): keys = item.pop("正则变量") deps = item.pop("绝对路径表达式") jp_dict = item.pop("Jsonpath") + print(f"当前的:{item}") if not item.get("Run") or item.get("Run").upper() != "YES": - log.info(f"测试用例:{item_id} 不执行,跳过!!!") + logger.info(f"测试用例:{item_id} 不执行,跳过!!!") return if sleep_time: try: time.sleep(int(sleep_time)) - log.info(f"暂停:{sleep_time}") + logger.info(f"暂停:{sleep_time}") except Exception as e: - log.info(f'暂停时间必须是数字') + logger.info(f'暂停时间必须是数字') raise e # 首先执行sql替换,将sql替换为正确的sql语句 @@ -79,17 +65,16 @@ class TestProjectApi(unittest.TestCase): try: execute_sql_results = mysql.do_mysql(sql) - log.info(f'sql 执行成功:{execute_sql_results}') + logger.info(f'sql 执行成功:{execute_sql_results}') if execute_sql_results and sql_variable: # 执行sql数据提取 DataExtractor(execute_sql_results).substitute_data(jp_dict=sql_variable) - log.info('sql 提取成功') + logger.info('sql 提取成功') if method == "SQL" and mysql: return except Exception as e: - log.error(f'执行 sql 失败:{sql},异常信息:{e}') + logger.error(f'执行 sql 失败:{sql},异常信息:{e}') raise e - # 拼接动态代码段文件 prepost_script = f"prepost_script_{sheet}_{item_id}.py" item = load_and_execute_script(Config.SCRIPTS_DIR, prepost_script, "setup", item) @@ -123,7 +108,7 @@ class TestProjectApi(unittest.TestCase): 'headers': headers } response = http_client(host, url, method, **kwargs) - result_tuple = Validator().run_validate(expected, response.json()) # 执行断言 返回结果元组 + result_tuple = validator.run_validate(expected, response.json()) # 执行断言 返回结果元组 self.assertNotIn("FAIL", result_tuple, "FAIL 存在结果元组中") try: # 提取响应 @@ -131,15 +116,15 @@ class TestProjectApi(unittest.TestCase): # 执行后置代码片段 load_and_execute_script(Config.SCRIPTS_DIR, prepost_script, "teardown", response) except Exception as err: - log.error(f"提取响应失败:{sheet}_{item_id}_{name}_{description}" - f"\nregex={regex};" - f" \nkeys={keys};" - f"\ndeps={deps};" - f"\njp_dict={jp_dict}" - f"\n{err}") + logger.error(f"提取响应失败:{sheet}_{item_id}_{name}_{description}" + f"\nregex={regex};" + f" \nkeys={keys};" + f"\ndeps={deps};" + f"\njp_dict={jp_dict}" + f"\n{err}") except Exception as e: result = "FAIL" - log.error(f'异常用例: {sheet}_{item_id}_{name}_{description}\n{e}') + logger.error(f'异常用例: {sheet}_{item_id}_{name}_{description}\n{e}') raise e finally: response_value = response.text if response is not None else str(response) @@ -150,7 +135,7 @@ class TestProjectApi(unittest.TestCase): @classmethod def tearDownClass(cls) -> None: - log.info(f"所有用例执行完毕") + logger.info(f"所有用例执行完毕") if __name__ == '__main__':