def set_case(self, case): """ 设置用例, 把Excel某一行赋值过来 :param case: list类型 :return:如果有关联的字符串,将其返回,并重新写Excel """ # openpyxl 的行和列从1开始,这里进行减1操作 if not case[cell_config.get('path') - 1].startswith('http'): path = "/" + case[cell_config.get('path') - 1] self.path = self.url + path.replace("//", "/") else: self.path = case[cell_config.get('path') - 1] if case[cell_config.get('method') - 1] is not None: self.method = case[cell_config.get('method') - 1] if case[cell_config.get('params') - 1] is None or case[ cell_config.get('params') - 1] == '' or case[ cell_config.get('params') - 1].lower() == 'none' or case[cell_config.get('params') - 1].lower() == 'null': self.params = None else: self.relation_params_temp = self._get_relations( case[cell_config.get('params') - 1]) try: self.params = json.loads(self.relation_params_temp) except Exception as e: logger.exception(traceback.format_exc()) self.params = None
def __assert(self, response): # 断言 try: # 通过反射获取模块my_assert.py的函数 func = getattr(obj, self.case[cell_config.get('assert_model') - 1]) # 执行反射获取的函数 if str(func).__contains__('assert_jsonpath'): assert_result = func( response, self.case[cell_config.get('expect_param1') - 1], self.case[cell_config.get('expect_param2') - 1]) else: assert_result = func( response, self.case[cell_config.get('expect_param1') - 1]) logger.debug('断言结果:{}'.format(assert_result)) # allure上显示测试步骤 self.__step(assert_result=assert_result, response_contain=response.text, url=self.my_inter.path, params=self.case[cell_config.get('params') - 1]) # 写断言结果 字体颜色绿色 self.excel.write(sheet_name=self.case[-2], row=self.case[-1], column=cell_config.get('expect_result'), value=assert_result) # 写测试结果 字体颜色绿色 self.excel.write(sheet_name=self.case[-2], row=self.case[-1], column=cell_config.get('status'), value='PASS', fg_color=MyColor.GREEN) assert True except Exception as e: allure.severity(allure.severity_level.CRITICAL) logger.debug('断言结果:{}'.format(e)) self.__step(assert_result=e, response_contain=response.text, url=self.my_inter.path, params=self.case[cell_config.get('params') - 1]) # 写断言结果 字体颜色红色 self.excel.write(sheet_name=self.case[-2], row=self.case[-1], column=cell_config.get('expect_result'), value=str(e), color=MyColor.RED) # 写测试结果 字体颜色红色 self.excel.write(sheet_name=self.case[-2], row=self.case[-1], column=cell_config.get('status'), value='FAIL', fg_color=MyColor.RED) pytest.mark.xfail(e) assert False
def read_no_pass_line(self, row): line = [] for j in range(1, self.column + 1): if cell_config.get('method') is not None or cell_config.get( 'method') == '': if self.sheet.cell(row, j).value is None: line.append('') else: line.append( str(self.sheet.cell(row, j).value).replace('\n', '')) return line
def __update_allure(self, response): if response is None: allure.severity(allure.severity_level.BLOCKER) self.my_method(str(self.my_inter.path)) self.my_params(str(self.case[cell_config.get('params') - 1])) self.my_response(response.text) # return False # raise Exception('无任何返回') # 写请求返回结果 self.excel.write(sheet_name=self.case[-2], row=self.case[-1], column=cell_config.get('result'), value=response.text)
def __set_allure(self): name = self.case[cell_config.get('model') - 1] if not self.name == name: allure.dynamic.feature(name) self.name = name else: allure.dynamic.feature(self.name) # 设置allure报告的title allure.dynamic.title(self.case[cell_config.get('id') - 1] + ':' + self.case[cell_config.get('case_name') - 1]) # 设置allure报告的 描述 allure.dynamic.description( self.case[cell_config.get('interface_name') - 1] + "," + self.case[cell_config.get('case_name') - 1])
def read_sheet(self): """ 获取当前sheet页所有行的 :return: """ lines = [] # 遍历每行数据,跳过第一行的标题拦 for i in range(2, self.row + 1): # line = {} line = [] # if self.sheet.cell(i, cell_config.get('is_run')).value == '是': for j in range(1, self.column + 1): if self.sheet.cell(i, j).value is None: line.append('') else: if j == cell_config.get('method'): line.append( str(self.sheet.cell(i, j).value).lower().replace( '\n', '')) else: line.append( str(self.sheet.cell(i, j).value).replace('\n', '')) else: # 当取得所有值是,把sheet页写入list中,方便进行写操作时知道要写入那个sheet页 line.append(self.sheet_name) # 把row加入lines中, 方便其他地方调用 line.append(i) # print(line) lines.append(line) return lines
def __send(self): # 写日志 现在出现写特殊字符报错,后期解决 try: logger.debug('正在执行的用例为:{}'.format(self.case)) except Exception as e: logger.exception(e) # 反射调用接口关键字 response = None # print(self.case[::-1]) try: self.my_inter.set_case(self.case) # 关联成功后,修改result_xxx.xlsx 中的参数列 if not type_judgment.is_Null(self.my_inter.params): self.case[cell_config.get('params') - 1] = self.my_inter.relation_params_temp self.excel.write(sheet_name=self.case[-2], row=self.case[-1], column=cell_config.get('params'), value=self.my_inter.relation_params_temp) # 通过反射获取Inter类的函数 response = getattr(self.my_inter, self.case[cell_config.get('method') - 1])() except Exception as e1: # 写请求返回结果 字体颜色红色 self.excel.write(sheet_name=self.case[-2], row=self.case[-1], column=cell_config.get('result'), value=e1, color=MyColor.RED) # 写测试结果 字体颜色红色 self.excel.write(sheet_name=self.case[-2], row=self.case[-1], column=cell_config.get('status'), value='FAIL', fg_color=MyColor.RED) return response
def test01(self, cases): allure.dynamic.title(cases[cell_config.get('case_name') - 1]) allure.dynamic.description(cases[cell_config.get('interface_name') - 1]) sheet_name = 'Sheet1' logger.info(cases) # 调用即可方法下 try: self.my_inter.set_case(cases) response = getattr(self.my_inter, cases[cell_config.get('method') - 1])() if response is not None: # 写请求返回结果 self.excel.write(sheet_name=sheet_name, row=cases[-1], column=cell_config.get('result'), value=response.text) except Exception as e1: # 写请求返回结果 self.excel.write(sheet_name=sheet_name, row=cases[-1], column=cell_config.get('result'), value="None", color='FF0000') assert_result = '' if response is not None: try: func = getattr(obj, cases[cell_config.get('assert_model') - 1]) if func == 'assert_jsonpath': func(response, cases[cell_config.get('expect_param1') - 1], cases[cell_config.get('expect_param2') - 1]) else: func(response, cases[cell_config.get('expect_param1') - 1]) assert_result = '断言成功!' self.__step(assert_result=assert_result, response_contain=response.text, url=self.my_inter.path, params=cases[cell_config.get('params') - 1]) self.excel.write(sheet_name=sheet_name, row=cases[-1], column=cell_config.get('expect_result'), value=assert_result) # 写测试结果 字体颜色绿色 self.excel.write(sheet_name=sheet_name, row=cases[-1], column=cell_config.get('status'), value='PASS!', color='00FF00') assert True except Exception as e: self.__step(assert_result=e, response_contain=response.text, url=self.my_inter.path, params=cases[cell_config.get('params') - 1]) # 写测试结果 字体颜色红色 self.excel.write(sheet_name=sheet_name, row=cases[-1], column=cell_config.get('expect_result'), value=str(e), color='FF0000') # 写测试结果 字体颜色红色 self.excel.write(sheet_name=sheet_name, row=cases[-1], column=cell_config.get('status'), value='FAIL', color='FF0000') assert False
class TestDev01: excel = ExcelTool(get_abspath('data/cases/test.xlsx')) def setup_class(self): # self.excel = ExcelTool(get_abspath('data/cases/test.xlsx')) self.excel.copy() self.sheet_names = self.excel.get_sheet_names() self.my_inter = Inter(host=host_dev) def run_step(self, method, url, params): func = getattr(self.my_inter, method) self.my_method(url) self.my_params(params) return func() @allure.step('请求地址:{url}') def my_method(self, url): pass @allure.step('请求参数:{params}') def my_params(self, params): pass @allure.step('响应结果:{response}') def my_response(self, response): pass @allure.step('响应结果:{assert_result}') def my_assert(self, assert_result): pass def __step(self, assert_result, response_contain, url, params): self.my_assert(assert_result) self.my_response(response_contain) self.my_method(url) self.my_params(params) # 每个方法执行参数化之前,都需要设置sheet页后,在读取excel获取对应的cases ca = excel.read("Sheet1") print(ca) # 每个sheet为一类测试用例 @allure.story(ca[0][cell_config.get('model') - 1] + ':' + ca[0][cell_config.get('path') - 1]) @pytest.mark.parametrize("cases", ca) def test01(self, cases): allure.dynamic.title(cases[cell_config.get('case_name') - 1]) allure.dynamic.description(cases[cell_config.get('interface_name') - 1]) sheet_name = 'Sheet1' logger.info(cases) # 调用即可方法下 try: self.my_inter.set_case(cases) response = getattr(self.my_inter, cases[cell_config.get('method') - 1])() if response is not None: # 写请求返回结果 self.excel.write(sheet_name=sheet_name, row=cases[-1], column=cell_config.get('result'), value=response.text) except Exception as e1: # 写请求返回结果 self.excel.write(sheet_name=sheet_name, row=cases[-1], column=cell_config.get('result'), value="None", color='FF0000') assert_result = '' if response is not None: try: func = getattr(obj, cases[cell_config.get('assert_model') - 1]) if func == 'assert_jsonpath': func(response, cases[cell_config.get('expect_param1') - 1], cases[cell_config.get('expect_param2') - 1]) else: func(response, cases[cell_config.get('expect_param1') - 1]) assert_result = '断言成功!' self.__step(assert_result=assert_result, response_contain=response.text, url=self.my_inter.path, params=cases[cell_config.get('params') - 1]) self.excel.write(sheet_name=sheet_name, row=cases[-1], column=cell_config.get('expect_result'), value=assert_result) # 写测试结果 字体颜色绿色 self.excel.write(sheet_name=sheet_name, row=cases[-1], column=cell_config.get('status'), value='PASS!', color='00FF00') assert True except Exception as e: self.__step(assert_result=e, response_contain=response.text, url=self.my_inter.path, params=cases[cell_config.get('params') - 1]) # 写测试结果 字体颜色红色 self.excel.write(sheet_name=sheet_name, row=cases[-1], column=cell_config.get('expect_result'), value=str(e), color='FF0000') # 写测试结果 字体颜色红色 self.excel.write(sheet_name=sheet_name, row=cases[-1], column=cell_config.get('status'), value='FAIL', color='FF0000') assert False