def test_update_ordered_dict(self): map_list = [{"a": 1}, {"b": 2}] ordered_dict = utils.convert_to_order_dict(map_list) override_mapping = {"a": 3, "c": 4} new_dict = utils.update_ordered_dict(ordered_dict, override_mapping) self.assertEqual(3, new_dict["a"]) self.assertEqual(4, new_dict["c"])
def bind_variables(self, variables, level="testcase"): """ bind variables to testset context or current testcase context. variables in testset context can be used in all testcases of current test suite. @param (list or OrderDict) variables, variable can be value or custom function. if value is function, it will be called and bind result to variable. e.g. OrderDict({ "TOKEN": "debugtalk", "random": "${gen_random_string(5)}", "json": {'name': 'user', 'password': '******'}, "md5": "${gen_md5($TOKEN, $json, $random)}" }) """ if isinstance(variables, list): variables = utils.convert_to_order_dict(variables) for variable_name, value in variables.items(): variable_eval_value = self.eval_content(value) if level == "testset": self.testset_shared_variables_mapping[ variable_name] = variable_eval_value self.testcase_variables_mapping[ variable_name] = variable_eval_value self.testcase_parser.update_binded_variables( self.testcase_variables_mapping)
def extract_response(self, extractors): """ extract value from requests.Response and store in OrderedDict. @param (list) extractors [ {"resp_status_code": "status_code"}, {"resp_headers_content_type": "headers.content-type"}, {"resp_content": "content"}, {"resp_content_person_first_name": "content.person.name.first_name"} ] @return (OrderDict) variable binds ordered dict """ if not extractors: return {} logger.log_info("start to extract from response object.") extracted_variables_mapping = OrderedDict() extract_binds_order_dict = utils.convert_to_order_dict(extractors) for key, field in extract_binds_order_dict.items(): if not isinstance(field, basestring): raise exception.ParamsError("invalid extractors in testcase!") extracted_variables_mapping[key] = self.extract_field(field) return extracted_variables_mapping
def extract_response(self, extractors): """ extract value from requests.Response and store in OrderedDict. @param (list) extractors [ {"resp_status_code": "status_code"}, {"resp_headers_content_type": "headers.content-type"}, {"resp_content": "content"}, {"resp_content_person_first_name": "content.person.name.first_name"} ] @return (OrderDict) variable binds ordered dict """ if not extractors: return {} logger.log_info("start to extract from response object.") extracted_variables_mapping = OrderedDict() extract_binds_order_dict = utils.convert_to_order_dict(extractors) for key, field in extract_binds_order_dict.items(): if not isinstance(field, basestring): raise exception.ParamsError("invalid extractors in testcase!") result = self.extract_field(field) extracted_variables_mapping[key] = result if not (isinstance(result, bool) or bool(result)): # False 可以return err_msg = u"extract data with delimiter can be None!\n" err_msg += u"response: {}\n".format(self.parsed_dict()) err_msg += u"regex: {}\n".format(field) logger.log_error(err_msg) raise exception.ParamsError(err_msg) return extracted_variables_mapping
def extract_response(self, extractors): """ extract value from requests.Response and store in OrderedDict. @param (list) extractors [ {"resp_status_code": "status_code"}, {"resp_headers_content_type": "headers.content-type"}, {"resp_content": "content"}, {"resp_content_person_first_name": "content.person.name.first_name"} ] @return (OrderDict) variable binds ordered dict """ if not extractors: return {} logger.log_info("start to extract from response object.") extracted_variables_mapping = OrderedDict() extract_binds_order_dict = utils.convert_to_order_dict(extractors) for key, field in extract_binds_order_dict.items(): if not isinstance(field, basestring): raise exception.ParamsError("invalid extractors in testcase!") extracted_variables_mapping[key] = self.extract_field(field) return extracted_variables_mapping
def bind_variables(self, variables, level="testcase"): """ bind variables to testset context or current testcase context. variables in testset context can be used in all testcases of current test suite. @param (list or OrderDict) variables, variable can be value or custom function. if value is function, it will be called and bind result to variable. e.g. OrderDict({ "TOKEN": "debugtalk", "random": "${gen_random_string(5)}", "json": {'name': 'user', 'password': '******'}, "md5": "${gen_md5($TOKEN, $json, $random)}" }) """ if isinstance(variables, list): variables = utils.convert_to_order_dict(variables) for variable_name, value in variables.items(): variable_evale_value = self.testcase_parser.parse_content_with_bindings(value) if level == "testset": self.testset_shared_variables_mapping[variable_name] = variable_evale_value self.testcase_variables_mapping[variable_name] = variable_evale_value self.testcase_parser.update_binded_variables(self.testcase_variables_mapping)
def test_convert_to_order_dict(self): map_list = [ {"a": 1}, {"b": 2} ] ordered_dict = utils.convert_to_order_dict(map_list) self.assertIsInstance(ordered_dict, dict) self.assertIn("a", ordered_dict)
def test_convert_to_order_dict(self): map_list = [ {"a": 1}, {"b": 2} ] ordered_dict = utils.convert_to_order_dict(map_list) self.assertIsInstance(ordered_dict, dict) self.assertIn("a", ordered_dict)
def test_update_ordered_dict(self): map_list = [ {"a": 1}, {"b": 2} ] ordered_dict = utils.convert_to_order_dict(map_list) override_mapping = {"a": 3, "c": 4} new_dict = utils.update_ordered_dict(ordered_dict, override_mapping) self.assertEqual(3, new_dict["a"]) self.assertEqual(4, new_dict["c"])