def setup_class(self): '''执行用例前执行一次''' excelhandler = ExcelHandler() table = excelhandler.table_name() mysql = MysqlHandler() mysql.create_table(table) #创建依赖数据表 run = NotRunBeDependCase() run.not_run_be_depend_case() # 执行有被依赖但是不执行的用例
def savedependvalue(self, depend_data_key, response): excelhandler = ExcelHandler() table = excelhandler.table_name() getvalue = GetValue() depend_value = getvalue.get_json_value_by_key(response, depend_data_key) for value in depend_value: sql = MysqlHandler() # 把每一个依赖key,value保存到数据库 # print(table, depend_data_key, value) sql.insert_data(table, depend_data_key, value)
def getparam(self,case_depend_key,param): excelhandler = ExcelHandler() table = excelhandler.table_name() mysql = MysqlHandler() # 获取数据库的依赖值 repl = mysql.select_table('value', table, case_depend_key) pattern = '<' + case_depend_key + '>' # repl替换掉string中被pattern匹配的字符, count表示最大替换次数,flags表示正则表达式的常量 param = str(param) # print(pattern,repl,param) value = re.sub(pattern, repl, param, count=1, flags=re.IGNORECASE) param = eval(value) return param
class Test_case(object): def setup_class(self): '''执行用例前执行一次''' excelhandler = ExcelHandler() table = excelhandler.table_name() mysql = MysqlHandler() mysql.create_table(table) #创建依赖数据表 run = NotRunBeDependCase() run.not_run_be_depend_case() # 执行有被依赖但是不执行的用例 @pytest.mark.parametrize('case', ExcelHandler().get_all_excel_data) def test_case(self, case): case_model = case['case_model'] case_params = case['case_params'] case_run = case['case_run'] if case_run.upper() != 'YES': pytest.skip('不执行') if case_params != '': params = ReadYaml().get_yaml_param(case_model, case_params) # 读取yaml文件,获取请求参数 if case['case_depend_key'] != '': #判断参数是否有依赖数据 param = GetParam().getparam(case['case_depend_key'], params) #读取数据依赖数据库,更换请求数据 case['case_params'] = param #请求参数赋值 else: #没有依赖数据 case['case_params'] = params response, assert_value = RequestHandler( case).get_response #获取到响应数据和断言数据 if case['case_response_key'] != '': #判断响应数据是否有被依赖 savevalue = SaveRunBeDependValue() savevalue.savedependvalue(case['case_response_key'], response) #把依赖的数据实时保存到数据库 # 制作 allure 报告 """ 执行断言 """ assert assert_value[0] == assert_value[1] allure.dynamic.title(case['case_name']) allure.dynamic.description('<font color="red">请求URL:</font>{}<br />' '<font color="red">期望值:</font>{}'.format( case['case_url'], case['case_expect'])) allure.dynamic.feature(case['case_name']) allure.dynamic.story(case['case_method']) def teardown_class(self): """llure命令 """ AllureHandler().execute_command()
class Test_case(object): @pytest.mark.parametrize('case', ExcelHandler().get_excel_data) def test_case(self, case): """ 执行断言 """ # print(case) # 发请求 response = RequestHandler(case).get_response # 制作 allure 报告 allure.dynamic.title(case['case_project']) allure.dynamic.description('<font color="red">请求URL:</font>{}<br />' '<font color="red">期望值:</font>{}'.format(case['case_url'], case['case_description'])) allure.dynamic.feature(case['case_project']) allure.dynamic.story(case['case_method']) assert response[0] == response[1] def teardown_class(self): """ 执行alllure命令 """ AllureHandler().execute_command() # 发邮件 EmailHandler().send_email()
class NotRunBeDependCase(object): ''' 1、执行被依赖但不执行的case 2、获取依赖的值 3、以key,value的形式保存到数据库 ''' def __init__(self): self.excelhandler = ExcelHandler() self.dependhandler = DependHandler() self.getvalue = GetValue() def not_run_be_depend_case(self): #获取所有的case excel_data = self.excelhandler.get_all_excel_data #获取插入表 table = self.excelhandler.table_name() mysql = MysqlHandler() mysql.create_table(table) # 循环遍历每一条case for case in excel_data: is_run = case['case_run'].upper() if case['case_response_key'] != '' and is_run != 'YES': url = case['case_url'] method = case['case_method'] type = case['params_type'] data = case['case_params'] #执行被依赖用例 response = self.dependhandler.send_depend_request( url, method, type, data) depend_key = case['case_response_key'] depend_value = self.getvalue.get_json_value_by_key( response, depend_key) for value in depend_value: sql = MysqlHandler() # 把每一个依赖key,value保存到数据库 sql.insert_data(table, depend_key, value)
def __init__(self): self.excelhandler = ExcelHandler() self.dependhandler = DependHandler() self.getvalue = GetValue()
def __init__(self): self.excelhandler = ExcelHandler()