From ad6e51219c526ebbbb047998b3a5121c43bcb6c9 Mon Sep 17 00:00:00 2001 From: zhanhuasheng Date: Thu, 16 May 2024 18:28:58 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TestCase/account/test_01_account.py | 2 +- TestCase/automation/test_01_template.py | 13 +++++++ TestData/automation/template_test_data.py | 6 ++++ TestFile/automation/template_select.py | 38 ++++++++++++++++++++ Utils/req_handler.py | 43 ++++++++++++----------- YamlCase/automation/template.yaml | 24 +++++++++++++ 6 files changed, 105 insertions(+), 21 deletions(-) create mode 100644 TestCase/automation/test_01_template.py create mode 100644 TestData/automation/template_test_data.py create mode 100644 TestFile/automation/template_select.py create mode 100644 YamlCase/automation/template.yaml diff --git a/TestCase/account/test_01_account.py b/TestCase/account/test_01_account.py index 0a0757f..1543b34 100644 --- a/TestCase/account/test_01_account.py +++ b/TestCase/account/test_01_account.py @@ -1,7 +1,6 @@ import pytest from Utils import yaml_handler from Utils import req_handler -from Utils.config_handler import configHandler from TestData.account.account_test_data import accountData class TestAccount: @@ -12,3 +11,4 @@ class TestAccount: @pytest.mark.parametrize('case',yaml_data) def test_account(self,case): req_handler.ReqHandler.send_requests(case=case,var_class=accountData) + diff --git a/TestCase/automation/test_01_template.py b/TestCase/automation/test_01_template.py new file mode 100644 index 0000000..5c7b7e1 --- /dev/null +++ b/TestCase/automation/test_01_template.py @@ -0,0 +1,13 @@ +import pytest +from Utils import yaml_handler +from Utils import req_handler +from TestData.automation.template_test_data import templateData + +class TestTemplate: + + yaml_path = r'automation/template.yaml' + yaml_data = yaml_handler.yaml_handler.get_case(yaml_path) + @pytest.mark.flaky(reruns=templateData.rerun, reruns_delay=templateData.rerun_delay) + @pytest.mark.parametrize('case',yaml_data) + def test_template(self,case): + req_handler.ReqHandler.send_requests(case=case,var_class=templateData) diff --git a/TestData/automation/template_test_data.py b/TestData/automation/template_test_data.py new file mode 100644 index 0000000..20a09cd --- /dev/null +++ b/TestData/automation/template_test_data.py @@ -0,0 +1,6 @@ +from Utils.global_variate import Global +from Utils.config_handler import base_config + +class templateData(Global): + pass + diff --git a/TestFile/automation/template_select.py b/TestFile/automation/template_select.py new file mode 100644 index 0000000..0443f50 --- /dev/null +++ b/TestFile/automation/template_select.py @@ -0,0 +1,38 @@ +from Utils.sql_handler import test_env_conn +from TestData.automation.template_test_data import templateData +template_parent_select_sql = f'select * from rule_template where status = 1' +template_parent_select_result = test_env_conn.select_many_value(sql=template_parent_select_sql) +for i in template_parent_select_result: + if i['tpl_category'] == 'autoanswer': + workflow_parent_template_id = i['tpl_id'] + setattr(templateData, 'workflow_parent_template_id', workflow_parent_template_id) + elif i['tpl_category'] == 'willdesk': + rule_parent_template_id = i['tpl_id'] + setattr(templateData, 'rule_parent_template_id', rule_parent_template_id) +workflow_template_select_sql = f'select * from rule_tpl_usecase where tpl_id = "{workflow_parent_template_id}" and status = 1' +workflow_template_select_result = test_env_conn.select_many_value(sql=workflow_template_select_sql) +workflow_template_num = len(workflow_template_select_result) +workflow_template_id = workflow_template_select_result[0]['tpld_id'] +workflow_template_title = workflow_template_select_result[0]['title'] +workflow_template_describe = workflow_template_select_result[0]['describe'] +workflow_template_event_id = workflow_template_select_result[0]['event_id'] + +setattr(templateData, 'workflow_template_num', workflow_template_num) +setattr(templateData, 'workflow_template_id', workflow_template_id) +setattr(templateData, 'workflow_template_title', workflow_template_title) +setattr(templateData, 'workflow_template_describe', workflow_template_describe) +setattr(templateData, 'workflow_template_event_id', workflow_template_event_id) + +rule_template_select_sql = f'select * from rule_tpl_usecase where tpl_id = "{rule_parent_template_id}" and status = 1' +rule_template_select_result = test_env_conn.select_many_value(sql=rule_template_select_sql) +rule_template_num = len(rule_template_select_result) +rule_template_id = rule_template_select_result[0]['tpld_id'] +rule_template_title = rule_template_select_result[0]['title'] +rule_template_describe = rule_template_select_result[0]['describe'] +rule_template_event_id = rule_template_select_result[0]['event_id'] + +setattr(templateData, 'rule_template_num', rule_template_num) +setattr(templateData, 'rule_template_id', rule_template_id) +setattr(templateData, 'rule_template_title', rule_template_title) +setattr(templateData, 'rule_template_describe', rule_template_describe) +setattr(templateData, 'rule_template_event_id', rule_template_event_id) diff --git a/Utils/req_handler.py b/Utils/req_handler.py index ada93f6..a24dd91 100644 --- a/Utils/req_handler.py +++ b/Utils/req_handler.py @@ -25,27 +25,30 @@ class ReqHandler: :param var_class: 传入对应的变量类(变量类必须先继承Global公用变量类) :return: 返回处理好的case字典 ''' - case = str(case) - replace_words = re.findall(r'\$\{(.+?)\}',case) - if not replace_words: + try: + case = str(case) + replace_words = re.findall(r'\$\{(.+?)\}',case) + if not replace_words: + return eval(case) + else: + for word in replace_words: + try: + value = getattr(var_class,word) #找到正确的变量值 + if not isinstance(value,str): + case = case.replace(r"'${%s}'"%word,str(value)) #替换掉${}变量符 + else: + case = case.replace(r"${%s}"%word,str(value)) #替换掉${}变量符 + if re.findall(r'\$\{(.+?)\}',case): #保底 + case = case.replace(r'${%s}'%word,str(value)) + except Exception: + import traceback + print(traceback.print_exc()) + print(f'{word}变量未找到') + raise Exception return eval(case) - else: - for word in replace_words: - try: - value = getattr(var_class,word) #找到正确的变量值 - if not isinstance(value,str): - case = case.replace(r"'${%s}'"%word,str(value)) #替换掉${}变量符 - else: - case = case.replace(r"${%s}"%word,str(value)) #替换掉${}变量符 - if re.findall(r'\$\{(.+?)\}',case): #保底 - case = case.replace(r'${%s}'%word,str(value)) - except Exception: - import traceback - print(traceback.print_exc()) - print(f'{word}变量未找到') - raise Exception - return eval(case) - + except SyntaxError: + print('内容可能含有单引号或者换行导致报错,用例:',case) + raise Exception @classmethod def send_requests(self,case,var_class): try: diff --git a/YamlCase/automation/template.yaml b/YamlCase/automation/template.yaml new file mode 100644 index 0000000..d912fbb --- /dev/null +++ b/YamlCase/automation/template.yaml @@ -0,0 +1,24 @@ +api1: + title: 检查workflow模板列表 + url: /api/v1/rule/tplUseCaseList + method: post + before_sql: automation/template_select.py + data: {"page":1,"pageSize":100,"tplCateId":"${workflow_parent_template_id}","v":"${v}"} + expected: + - eq: {"$.code":0} + - eq: {"$.data.total": "${workflow_template_num}"} + - eq: {"$.data.list[?(@.tpldId == '${workflow_template_id}')].describe": "${workflow_template_describe}"} + - eq: {"$.data.list[?(@.tpldId == '${workflow_template_id}')].eventId": "${workflow_template_event_id}"} + - eq: {"$.data.list[?(@.tpldId == '${workflow_template_id}')].title": "${workflow_template_title}"} + +api2: + title: 检查rule模板列表 + url: /api/v1/rule/tplUseCaseList + method: post + data: {"page":1,"pageSize":100,"tplCateId":"${rule_parent_template_id}","v":"${v}"} + expected: + - eq: {"$.code":0} + - eq: {"$.data.total": "${rule_template_num}"} + - eq: {"$.data.list[?(@.tpldId == '${rule_template_id}')].describe": "${rule_template_describe}"} + - eq: {"$.data.list[?(@.tpldId == '${rule_template_id}')].eventId": "${rule_template_event_id}"} + - eq: {"$.data.list[?(@.tpldId == '${rule_template_id}')].title": "${rule_template_title}"} \ No newline at end of file -- GitLab