def create_random_test_data(cls) -> None: r"""创建常用的随机数据,并生成.yaml文件存在临时文件目录中 """ for key, value in cls.random_data.items(): filepath = setting.RANDOM_PARAMS + key + '.yaml' with open(filepath, 'w', encoding='utf-8') as file: file.write('{}: {}'.format(key, value)) logger.log_info('生成随机测试数据 => {} 成功.'.format(filepath))
def make_test_templates(self) -> dict: r"""创建测试用例的基础数据 :Usage: make_test_templates() """ logger.log_debug(self.request_body) method = GetJsonParams.get_value(self.request_body, 'method') if method in ['get', 'GET']: temp = ('url', 'params', 'headers', 'timeout') request_body = GetJsonParams.for_keys_to_dict(*temp, my_dict=self.request_body) if request_body['params']: if '=' in request_body.get('params') or '&' in request_body.get('params'): request_body['params'] = dict(parse.parse_qsl(request_body['params'])) logger.log_info("接受GET的请求参数为{}".format( json.dumps(request_body, indent=4, ensure_ascii=False)) ) try: response = self.get(**request_body) try: response_body = response.json() except simplejson.JSONDecodeError: response_body = response.text return { "status_code": response.status_code, "response_body": response_body } except exceptions.Timeout as error: raise error if method in ['post', 'POST']: temp = ('url', 'headers', 'json', 'data', 'files', 'timeout') request_body = GetJsonParams.for_keys_to_dict(*temp, my_dict=self.request_body) logger.log_info("接受POST的请求参数为{}".format( json.dumps(request_body, indent=4, ensure_ascii=False)) ) try: response = self.post(**request_body) try: response_body = response.json() except simplejson.JSONDecodeError: response_body = response.text return { "status_code": response.status_code, "response_body": response_body } except exceptions.Timeout as error: raise error else: raise exceptions.TestApiMethodError("接口测试请求类型错误, 请检查相关用例!")
def contrast_num(expected_knowledge, actual_knowledge) -> float: r"""得出计算对比度 :Args: - expected_knowledge: 预期的待测试文本, str object. - actual_knowledge: 实际Response返回文本, str object. """ expect, res = count(expected_knowledge), count(actual_knowledge) merge = merge_word(expect, res) v1, v2 = cal_vector(expect, merge), cal_vector(res, merge) diff = round(float(cal_con_dis(v2, v1, len(merge))), 4) logger.log_info('{}{}{}'.format(expect, res, diff)) return diff
def contrast_num(expected_knowledge, actual_knowledge): r"""result of contrast :param expected_knowledge: expected result, str object. :param actual_knowledge: actual result, str object. :return: Contrast value :rtype: float object. """ expect, res = count(expected_knowledge), count(actual_knowledge) merge = merge_word(expect, res) v1, v2 = cal_vector(expect, merge), cal_vector(res, merge) diff = round(float(cal_con_dis(v2, v1, len(merge))), 4) logger.log_info('{}{}{}'.format(expect, res, diff)) return diff
def send_message(cls, message: str) -> None: r"""发送错误的日志信息给企业微信 :Args: - message: 错误的日志信息, str object. """ send_url = (' https://qyapi.weixin.qq.com/cgi-bin/message/send?' 'access_token={}'.format(cls.get_token())) post_data = { "touser": "******", "msgtype": "text", "agentid": 1000002, "text": { "content": message }, "safe": 0 } res = requests.post(send_url, data=json.dumps(post_data), verify=True) logger.log_info("The Push WeChat message result is {}".format( json.dumps(res.json(), indent=4, ensure_ascii=False)))
class TestContainer: cases = [] with CreateCase() as file: for items in file.create_template(): obj, func = items func(obj.crop['class_name'], obj.crop['setup'], obj.crop['teardown'], obj.crop['skip'], obj.crop['func_name'], obj.crop['description']) cases.append(obj.crop) logger.log_info("测试用例已自动生成完毕, 文件: {}.py -> 具体测试用例:{}".format( obj.crop['class_name'], obj.crop['func_name'])) def __iter__(self): return iter(self.cases) def __next__(self): return next(self.cases) def __repr__(self): return self.cases
def run_test_cases(case_path): r"""Run the test case and generate data for the rendering template. :param case_path: Test case file path, str object. :return: generate data for the rendering template. :rtype: tuple object. """ start = time_setting.timestamp('format_now') start_time = time.time() for file_content in fp.iter_files(os.path.join(BASE_PATH, case_path)): logger.log_debug('一共执行的测试集为{}'.format(file_content)) results = [] for filename, filepath in dict(file_content).items(): logger.log_info('本次运行的用例为{} => {}'.format(filename, filepath)) raw_list = operate_excel.read_excel(filepath) logger.log_debug(raw_list) for index, content in enumerate(raw_list): result = {} header = {} intent, expected_know = content['intent'], content['knowledge'] quest = str(int(content['question'])) if isinstance(content['question'], float) else content['question'] know = os.path.split(expected_know)[-1] if str(expected_know).find('.jpg') else expected_know logger.log_debug('当前问题是 => {}'.format(quest)) try: http = http_hander.BaseKeyWords() res, out_values = http.make_test_templates({'question': quest}) res_texts = GJ.get_value(my_dict=res, key=out_values[0]) res_headers = GJ.get_value(my_dict=res, key=out_values[1]) res_images = GJ.get_value(my_dict=res, key=out_values[2]) res_contents = GJ.get_value(my_dict=res, key=out_values[3]) logger.log_debug("问题:{},Text的值是->{} Header的值是->{} Image的值是->{} Content的值是->{}".\ format(quest, res_texts, res_headers, res_images, res_contents)) res_text, res_header, res_image, res_content = '', '', '', '' if isinstance(res_texts, Iterable): for text in res_texts: res_text += text.strip() if isinstance(res_headers, Iterable): for head in res_headers: res_header += head.strip() if isinstance(res_images, Iterable): for image in res_images: res_image += os.path.split(image)[-1] if isinstance(res_contents, Iterable): for con_val in res_contents: res_content += dict(con_val)['Value'] if not isinstance(res_texts, Iterable) and isinstance(res_headers, Iterable): res_text = res_header if not isinstance(res_texts, Iterable) and isinstance(res_images, Iterable): res_text = res_image if not isinstance(res_texts, Iterable) and isinstance(res_contents, Iterable): res_text = res_content if isinstance(res_texts, Iterable) and isinstance(res_headers, Iterable) and isinstance(res_images, Iterable) and not isinstance(res_contents, Iterable): res_text = res_header + res_text + res_image if isinstance(res_texts, Iterable) and isinstance(res_headers, Iterable) and not isinstance(res_images, Iterable) and isinstance(res_contents, Iterable): res_text = res_header + res_text + res_content if isinstance(res_texts, Iterable) and not isinstance(res_headers, Iterable) and isinstance(res_images, Iterable) and isinstance(res_contents, Iterable): res_text = res_text + res_content + res_image if not isinstance(res_texts, Iterable) and isinstance(res_headers, Iterable) and isinstance(res_images, Iterable) and isinstance(res_contents, Iterable): res_text = res_header + res_content + res_image if not isinstance(res_texts, Iterable) and not isinstance(res_headers, Iterable) and isinstance(res_images, Iterable) and isinstance(res_contents, Iterable): res_text = res_content + res_image if isinstance(res_texts, Iterable) and not isinstance(res_headers, Iterable) and isinstance(res_images, Iterable) and not isinstance(res_contents, Iterable): res_text = res_text + res_image if not isinstance(res_texts, Iterable) and isinstance(res_headers, Iterable) and isinstance(res_images, Iterable) and not isinstance(res_contents, Iterable): res_text = res_header + res_image if isinstance(res_texts, Iterable) and isinstance(res_headers, Iterable) and not isinstance(res_images, Iterable) and not isinstance(res_contents, Iterable): res_text = res_header + res_text if isinstance(res_texts, Iterable) and not isinstance(res_headers, Iterable) and not isinstance(res_images, Iterable) and isinstance(res_contents, Iterable): res_text = res_text + res_content if not isinstance(res_texts, Iterable) and isinstance(res_headers, Iterable) and not isinstance(res_images, Iterable) and isinstance(res_contents, Iterable): res_text = res_header + res_content if not isinstance(res_headers, Iterable) and not isinstance(res_texts, Iterable) and not isinstance(res_images, Iterable) and not isinstance(res_contents, Iterable): res_text = "answer.Text & answer.Header & answer.Image & content未返回任何值,机器人未根据意图查找出答案." except TypeError: res_text = "Response Body is Null." except simplejson.errors.JSONDecodeError: res_text = '抱歉,暂时没有相关的答案信息,我们已记录您的问题,感谢您的支持!' diff = JD.contrast_num(know, res_text) test_result = 'Pass' if diff > 0.7 else 'Fail' run = time.time()-start_time header.update({ 'name': filename, 'start': start, 'run': run }) result.update({ 'question': quest, 'intent': intent, 'knowledge': know, 'response': res_text, 'diff': diff, 'result': test_result }) results.append(result) make_report_template.create_test_report(filename, header, results)
def wrap(*args): for items in iter(TestContainer()): for key, value in dict(items).items(): if value == func.__name__: body = {} # 用例文件与临时变量文件相互关联 # relevant_params = items.get('body').get('relevant_parameter') relevant_params = GetJsonParams.get_value( items, 'relevant_parameter') if relevant_params: if isinstance(relevant_params, list): _relevance = {} for relevant_param in relevant_params: relevant_files = relevant_param + '.yaml' for base_path in VariablesPathList: if exists( path.join(base_path, relevant_files)): with open(base_path + relevant_files, encoding='utf-8') as file: _relevance.update( yaml.safe_load(file)) # 判断关联文件中是否存在替换的变量,将其替换 _relevance_params = GetJsonParams.get_value( _relevance, 'relevant_parameter') if _relevance_params: if isinstance(_relevance_params, list): _next_relevance = {} for _relevance_param in _relevance_params: _relevant_files = _relevance_param + '.yaml' for base_path in VariablesPathList: if exists( path.join( base_path, _relevant_files)): with open(base_path + _relevant_files, encoding='utf-8' ) as file: _next_relevance.update( yaml.safe_load(file)) _relevance_body = relevance.custom_manage( str(_relevance), _next_relevance) _relevance.update(_relevance_body) relevance_body = relevance.custom_manage( str(items['body']), _relevance) body.update(relevance_body) # 运行用例,暂支持Post与Get请求接口 handler = http_keywords.BaseKeyWords(body) result = handler.make_test_templates() logger.log_info("本次用例执行的测试结果为{}".format( json.dumps(result, indent=4, ensure_ascii=False))) # 将临时变量写入yaml文件 res_index = items.get('body').get('res_index') if res_index: if isinstance(res_index, list): for res_key in res_index: return_res = GetJsonParams.get_value( result, res_key) file_name = setting.EXTRACT_PARAMS + res_key logger.log_debug( '保存的变量值为 => {} '.format(return_res)) with open(file_name + '.yaml', 'w', encoding='utf-8') as file: file.write('{}: {}'.format( res_key, return_res)) if isinstance(res_index, str): return_res = GetJsonParams.get_value( result, res_index) file_name = setting.EXTRACT_PARAMS + res_index logger.log_debug('保存的变量值为 {}'.format(return_res)) with open(file_name, 'w', encoding='utf-8') as file: file.write('{}: {}'.format( res_index, return_res)) # 验证接口请求数据是否落库 excep_columns, res_sql = {}, {} relevant_database = items.get('body').get('relevant_sql') if relevant_database: if isinstance(relevant_database, list): for relevant_db in relevant_database: filename = relevant_db + '.yaml' relevant_sql = {} with open(setting.CASE_DATA + filename, 'rb') as file: relevant_sql.update(yaml.safe_load(file)) action = relevant_sql[relevant_db]['action'] columns = relevant_sql[relevant_db]['execSQL'][ 'columns'] table = relevant_sql[relevant_db]['execSQL'][ 'table'] params = relevant_sql[relevant_db]['execSQL'][ 'params'] desc = relevant_sql[relevant_db]['execSQL'][ 'desc'] execute_sql = '{} {} FROM {} {} {}'.format( action, columns, table, params, desc) execute_res = ExecuteSQL( DataBaseSetting).execute(execute_sql)[0][0] res_sql.update({columns: execute_res}) logger.log_debug( '执行sql结果为{}'.format(execute_res)) return func( *args, response=result, kwassert=items.get('body').get('assert'), kwassert_same=items.get('body').get('assert_same_key'), json_diff=items.get('body').get('json_diff'), execute_res=res_sql, db_check=items.get('body').get('check_db'))
def wrap(*args, **kwargs): response = kwargs.get('response') kwassert = kwargs.get('kwassert') if kwargs.get('kwassert') else {} kwassert_same = kwargs.get('kwassert_same') json_diff = kwargs.get('json_diff') database_check = kwargs.get('db_check') if kwargs.get( 'db_check') else {} execute_res = kwargs.get('execute_res') tmp = tuple(kwassert.keys()) result = GetJsonParams.for_keys_to_dict(*tmp, my_dict=response) for key, value in kwassert.items(): if isinstance(value, list): tp, _value = value if tp == "type" and key == "ResponseType": result[key] = [ tp, repr(getattr(builtins, tp)(response)).split("'")[1] ] elif tp == "type": result[key] = [ tp, repr(getattr(builtins, tp)(result.get(key))).split("'")[1] ] elif tp == "len": result[key] = [ tp, repr(getattr(builtins, tp)(result.get(key))) ] else: result[key] = [tp, getattr(builtins, tp)(result.get(key))] expect_kwassert_same, response_kwassert_content = '', '' if isinstance(kwassert_same, Iterable): for key, value in dict(kwassert_same).items(): flag = key.split('.') response_kwassert_content += GetJsonParams.get_same_content( response, flag[0], int(flag[1]), flag[2]) expect_kwassert_same += value expect_assert_value = json.dumps(result, sort_keys=True, ensure_ascii=False) kwassert_value = json.dumps(kwassert, sort_keys=True, ensure_ascii=False) expect_json_diff = json.dumps(json_diff, sort_keys=True, ensure_ascii=False) json_diff_content = diff_lib.contrast_num(str(expect_json_diff), str(response)) logger.log_info("本次Response自动对比结果为{}".format( json.dumps(json_diff_content, indent=4, ensure_ascii=False))) return func(*args, response=result, expect_assert_value=expect_assert_value, kwassert_value=kwassert_value, expect_kwassert_same=expect_kwassert_same, response_kwassert_content=response_kwassert_content, database_check=database_check, execute_res=execute_res)