def run_main(self, method, url, data, header, cookie=None): try: result = '' if method.upper() == 'GET': result = self.send_get(url, data, header, cookie) elif method.upper() == 'POST': result = self.send_post(url, data, header, cookie) return result except Exception as e: logger.exception('请求主函数调用失败:{}'.format(e))
def get_value(self, key, node=None): if node == None: node = 'Test' cf = self.load_ini() try: data = cf.get(node, key) logger.info('获取配置文件的值,node:{},key:{}, data:{}'.format( node, key, data)) except Exception: logger.exception('没有获取到对应的值,node:{},key:{}'.format(node, key)) data = None return data
def test_requestOne(self, case_data): try: api_response = apiRequest.api_request(baseurl, testCaseData, case_data) api_response_data = api_response.json() # pactverity——全量契约校验 config_contract_format = Like({ "msg": "成功", "result": 0, "data": EachLike({"word": Like("testng")}) }) mPactVerify = PactVerify(config_contract_format) try: mPactVerify.verify(api_response_data) logger.info('verify_result:{},verify_info:{}'.format( mPactVerify.verify_result, mPactVerify.verify_info)) assert mPactVerify.verify_result == True except Exception: err_msg = '契约校验错误' logger.exception( '测试用例契约校验失败,verify_result:{},verify_info:{}'.format( mPactVerify.verify_result, mPactVerify.verify_info)) try: for case_validate in case_data['validate']: logger.info( '断言期望相关参数:check:{},comparator:{},expect:{}'.format( case_validate['check'], case_validate['comparator'], case_validate['expect'])) comparatorsTest.comparators_Assert( api_response, case_validate['check'], case_validate['comparator'], case_validate['expect']) logger.info('测试用例断言成功') except Exception as e: logger.exception('测试用例断言失败') except Exception as e: logger.exception('测试用例请求失败,原因:{}'.format(e))
def api_request(self, base_url, test_case_data, case_data): get_name = None get_url = None get_method = None get_headers = None get_cookies = None get_case_name = None get_case_params = None response_data = None try: get_name = test_case_data['config']['name'] get_url = base_url + test_case_data['config']['url'] get_method = test_case_data['config']['method'] get_headers = test_case_data['config']['headers'] get_cookies = test_case_data['config']['cookies'] except Exception as e: logger.exception('获取用例基本信息失败,{}'.format(e)) try: get_case_name = case_data['name'] get_case_params = case_data['params'] except Exception as e: logger.exception('获取测试用例信息失败,{}'.format(e)) with allure.step( "请求接口:%s,请求地址:%s,请求方法:%s,请求头:%s,请求Cookies:%s" % (get_name, get_url, get_method, get_headers, get_cookies)): allure.attach("接口用例描述:", "{0}".format(get_case_name)) allure.attach("接口用例请求参数:", "{0}".format(get_case_params)) logger.info('请求接口名:%r,请求地址:%r,请求方法:%r,请求头:%r,请求Cookies:%r' % (get_name, get_url, get_method, get_headers, get_cookies)) logger.info('请求接口名:%r,请求接口用例名:%r,接口用例请求参数:%r' % (get_name, get_case_name, get_case_params)) try: response_data = baseRequest.run_main(get_method, get_url, get_case_params, get_headers) except Exception as e: logger.exception('用例请求返回失败,{}'.format(e)) logger.info('请求接口名:%r,请求接口用例名:%r,返回参数:%r' % (get_name, get_case_name, response_data.json())) return response_data
def test_requestOne(self, case_data): try: api_response = apiRequest.api_request(baseurl, testCaseData, case_data) # pactverity——全量契约校验 config_contract_format = Like({ "error": 0, "msg": 'success', "lan": 'en' }) mPactVerify = PactVerify(config_contract_format) try: mPactVerify.verify(api_response.json()) logger.info('verify_result:{},verify_info:{}'.format( mPactVerify.verify_result, mPactVerify.verify_info)) assert mPactVerify.verify_result == True except Exception as e: logger.exception( '测试用例契约校验失败,verify_result:{},verify_info:{},exception:{}'. format(mPactVerify.verify_result, mPactVerify.verify_info, e)) try: for case_validate in case_data['validate']: logger.info( '断言期望相关参数:check:{},comparator:{},expect:{}'.format( case_validate['check'], case_validate['comparator'], case_validate['expect'])) comparatorsTest.comparators_Assert( api_response, case_validate['check'], case_validate['comparator'], case_validate['expect']) logger.info('测试用例断言成功') except Exception as e: logger.exception('测试用例断言失败') except Exception as e: logger.exception('测试用例请求失败,{}'.format(e))