def response_check(control_request_return): """ 返回值校验,从ControlRequest.control_request()得到返回值生成器 判断data_check 若不等于0,则直接进行指定key-value值的校验 若等于0则进行code与msg校验 """ logger.debug('enter responseCheck') logger_output.debug('enter responseCheck') #todo 需要改写测试通过的needTestFlag的值 for return_data in control_request_return: number, request_url, name, request_mode, required_parameter, expect_response, login_flag, test_flag,\ data_check, use_time, actually_response,request_parameter, skip_case = return_data if isinstance(request_parameter,dict): request_parameter = json.dumps(request_parameter,ensure_ascii=False) #将dict转为json 方便从report复制后手动校验 if data_check != 0: logger.debug('data_check= %s Type= %s' % (data_check, type(data_check))) logger_output.debug('data_check= %s Type= %s' % (data_check, type(data_check))) check_dict = ApiToolkit.parameter_extractor(data_check) try: response_dict = ApiToolkit.analysis_json(actually_response.json()) except Exception,e: logger.error('get response_dict fail! %s'%str(e)) logger_output.error('get response_dict fail! %s'%str(e)) response_dict = {} for key in check_dict.keys(): try: value = response_dict[key] except KeyError: output = u'数据校验失败!\n返回结果中没有 %s 字段、\n校验数据:\n%s\n请求参数:\n%s\n实际返回:\n%s' \ % (key,json.dumps(check_dict),request_parameter,json.dumps(response_dict)) yield (number, name, use_time, 'error', output, skip_case) try: assert check_dict[key] == value result = 'pass' output = u'数据校验通过!\n返回结果中字段 %s = %s 预期其值应为 %s\n请求参数:\n%s\n接口原始返回:\n%s' \ % (key, value, check_dict[key],request_parameter, json.dumps(response_dict,ensure_ascii=False)) except AssertionError: result = 'fail' output = u'数据校验失败!\n返回结果中字段 %s = %s 预期其值应为 %s\n请求参数:\n%s\n接口原始返回:\n%s'\ % (key, value, check_dict[key],request_parameter,json.dumps(response_dict,ensure_ascii=False)) yield (number, name, use_time, result, output, skip_case) else: try: if expect_response != None: try: expect_response = json.loads(expect_response) except TypeError,e: logger.error('expectRespone load TypeError:'+str(e)) logger_output.error('expectRespone load TypeError:'+str(e)) expect_response = {'code':'load error','msg':'load error'} else:
def control_request(case_values, fuzz_case_data): """ 请求发送控制,从case_parser接收用例列表,分别赋值后对各标志位进行判断,执行相应分支 """ logger.debug("enter control_request") logger_output.debug("enter control_request") for case_value in case_values: number, request_url, name, request_mode, required_parameter, optional_parameter, expect_response, case_level, login_flag, test_flag, data_check, relevance_case = ( case_value ) """控制运行模式,如用例等级大于当前模式,则更改test_flag=0以跳过用例执行,即只运行小于或等于当前运行等级的用例""" if case_level > TestConfig.runner_mode: test_flag = 0 """测试标志位为0,跳过用例执行,并使skip_case+1""" if test_flag == 0: TestConfig.skip_case += 1 logger.info(u"跳过执行用例: %s" % name) logger_output.info(u"跳过执行用例: %s" % name) continue """登录标志位为1,执行登录函数,并赋值给baseParameter""" baseParameter = {} if login_flag == 1: token = login_data_center(TestConfig.login_parameter, TestConfig.root_url) baseParameter = {"token": token} """当token=None存在于参数中时,改写token的值,容错步骤,正常情况此类用例login_flag将为0""" if required_parameter: if "token=None" in required_parameter: baseParameter["token"] = None logger.info(u"请求接口名称:" + name) logger.info(u"请求接口地址:" + request_url) logger_output.info(u"请求接口名称:" + name) logger_output.info(u"请求接口地址:" + request_url) """关联用例执行控制,根据relevance_case的数字确定执行次数""" relevance_parameter = {} if relevance_case: logger.debug(relevance_case) logger.debug(type(relevance_case)) if not isinstance(relevance_case, long): raise TypeError, u"relevance_case 填写错误" for relevance in xrange(relevance_case): logger.info(u"开始执行关联用例%s" % (relevance + 1)) logger_output.info(u"开始执行关联用例%s" % (relevance + 1)) relevance_request(case_values, relevance_parameter) # todo 此处考虑在关联用例执行完毕后,删除主用例不需要的参数,参考依据为主用例所填写的必传参数与选传参数key值 required_parameter_dict = ApiToolkit.parameter_extractor(required_parameter) optional_parameter_dict = ApiToolkit.parameter_extractor(optional_parameter) try: """合并基础参数即token,选传参数以及关联参数""" required_parameter_dict.update(baseParameter) required_parameter_dict.update(optional_parameter_dict) required_parameter_dict.update(relevance_parameter) except AttributeError, e: logger.error(u"全参数字典更新失败:" + str(e)) logger_output.error(u"全参数字典更新失败:" + str(e)) logger.info(u"请求参数:" + json.dumps(required_parameter_dict, ensure_ascii=False)) logger_output.info(u"请求参数:" + json.dumps(required_parameter_dict, ensure_ascii=False)) actually_response, use_time = send_request(required_parameter_dict, request_url, request_mode) """构建测试用例时使用,获取原始返回并写入excel用例预期返回栏中""" if TestConfig.is_first_run: logger.info(u"首次运行,写入expect_response") logger_output.info(u"首次运行,写入expect_response") try: actually_response_str = json.dumps(actually_response.json(), ensure_ascii=False) except ValueError: actually_response_str = actually_response.text try: first_run(7, number + 1, actually_response_str) except NameError, e: logger.error(u"首次运行,写入expect_response失败:%" % e) logger_output.error(u"首次运行,写入expect_response失败:%" % e)
logger.debug("enter relevance_request") logger_output.debug("enter relevance_request") try: relevance_case_value = case_values.next() request_url, name, reuqest_mode, required_parameter, optional_parameter, expect_response = relevance_case_value[ 1:7 ] except StopIteration, e: logger.error(e) raise StopIteration, "case_values iteration error!" """组合关联用例的 required_parameter与optional_parameter,传入parameter_extractor获得参数字典""" if required_parameter: if optional_parameter: required_parameter = required_parameter + "&" + optional_parameter parameter = ApiToolkit.parameter_extractor(required_parameter) # 此处将parameter与relevance_parameter合并,实现关联用例之间的关联 如下 parameter.update(relevance_parameter) logger.info(u"关联接口名称:" + name) logger.info(u"关联接口地址:" + request_url) logger_output.info(u"关联接口名称:" + name) logger_output.info(u"关联接口地址:" + request_url) logger.info(u"关联接口请求参数:" + json.dumps(parameter, ensure_ascii=False)) logger_output.info(u"关联接口请求参数:" + json.dumps(parameter, ensure_ascii=False)) try: if reuqest_mode.upper() == "GET": actually_response = requests.get(request_url, params=parameter) if actually_response.status_code == 200: try: logger_info = json.dumps(actually_response.json(), ensure_ascii=False) logger.info(u"get关联接口返回:\n" + logger_info)