def wrapped(args): all_testdata = get_testdata(args) jsonpath_for_expected = "$..expected_values" expect_data = json_get(all_testdata, jsonpath_for_expected) or all_testdata testdata = expect_data if input_values: jsonpath_for_input = "$..input_values" input_data = json_get(all_testdata, jsonpath_for_input) testdata = (expect_data, input_data) return func(args, testdata)
def get_testdata(obj): test_data_dir_path = (os.path.dirname(inspect.getfile( obj.__class__)).replace("testcases", "testdata")) test_module_name = obj.test_module.__class__.__name__.lower() test_data_path = test_data_dir_path + '\\' + test_module_name + ".json" expr_with_testcase = "$..%s" % obj._testMethodName.lower() testdata = jsonFile_get(test_data_path, expr_with_testcase) current_branch = "4.5.300" # sample if current_branch: current_branch = 'branch_' + current_branch.replace('.', '_') branch_group = current_branch[0:10] + '_all' else: current_branch = " " branch_group = " " priority_list = [current_branch, branch_group, "common"] testdata_with_branch = json_get_with_priority(testdata, priority_list) or testdata # with platform platform = "windows" # sample expr_with_platform = "$..%s" % platform testdata_with_platform = json_get( testdata_with_branch, expr_with_platform) or testdata_with_branch return testdata_with_platform
def json_get_with_priority(json_content, priority_list): priority = 0 while priority < len(priority_list): xpath = json_get(json_content, '$..%s' % priority_list[priority]) if xpath != None: return xpath priority += 1 return None
def get_expected_value(self): ''' it will get the testdata from [biz_module].json from a key whose name is the same with unittest's test case method ''' all_testdata = self.get_testdata() jsonpath_for_expected = "$..expected_values" testdata = json_get(all_testdata, jsonpath_for_expected) or all_testdata if testdata is None: raise BaseException("please confirm your data in file %s could be gotten with jsonpath %s" \ % (self.testdata_file_path, "$..%s" % self._testMethodName)) return testdata
def print_test_message(self, expected_value, compared_diff_msg): ''' print the checkpoints' test result as human language ''' for checkpoint, expected in expected_value.items(): try: checkpoint_detail_result = json_get( ast.literal_eval( str(compared_diff_msg).replace("[", "").replace( "]", "").replace('"', "")), "$..%s" % checkpoint) except: checkpoint_detail_result = compared_diff_msg pass_the_checkpoint = checkpoint_detail_result is None print_message = checkpoint.replace("_", " ") if isinstance(expected, bool): if expected == False: print_message = (print_message.replace( "is", "is not").replace("appear", "not appear").replace( "exist", "not exist")) if pass_the_checkpoint: if isinstance(expected, bool): print("[Pass check point] check %s" % print_message) else: print( "[Pass check point] check %s with [expected_value]--> %s" % (print_message, expected)) else: if isinstance(expected, bool): checkpoint_detail_result = "" else: checkpoint_detail_result = ( "\r\n[compare result]:\r\n" + json.dumps(checkpoint_detail_result, indent=4, ensure_ascii=False)) print("[Fail check point] check %s" % (print_message + checkpoint_detail_result))
def get_input_data(self): all_testdata = self.get_testdata() jsonpath_for_input = "$..input_values" return json_get(all_testdata, jsonpath_for_input) or all_testdata
def wrapped(args): all_testdata = get_testdata(args) jsonpath_for_expected = "$..expected_values" testdata = json_get(all_testdata, jsonpath_for_expected) or all_testdata return func(args, testdata)