In automated testing, a whole set of business processes will often be tested on a set of interfaces. At this time, there will often be data dependencies between interfaces. How to implement this dependency?
Here’s the idea:
- Extract the return value of the previous interface and store it in the global variable dictionary.
- When initializing an interface request, global variables in the request header, request parameters, and other information are parsed and replaced.
- Make a request.
Core code implementation:
The return value of the extraction interface is stored in the global variable dictionary
If set_global_vars and isinstance(set_global_vars, list): if isinstance(set_global_var, dict) and isinstance(set_global_var.get('name'), str): Query = set_global_var.get('name') # query = set_global_var.get('query') # query = value = Common.dict_get (response_json, query) # response_json self.global_vars[name] = valueCopy the code
Set_global_vars represents the global variable setting list of the current test case, and self.global_vars represents the global variable dictionary of the test class instance. For details about the implementation of common.dict_get, please refer to the implementation of the method
Parse and replace global variables in the string
Def resolve_global_var(pre_resolve_var, global_var_dic, global_var_regex='\${.*? }', match2key_sub_string_start_index=2, match2key_sub_string_end_index=-1): ''' :param pre_resolve_var: Variables to be parsed < STR > :param global_var_dic: dictionary of global variables <dict> :param global_var_regex: < STR > :param match2KEY_sub_string_start_index: <int> :param match2KEY_sub_string_end_index: end index of the string where the global variable expression is param match2key_sub_string_end_index <int> :return: < STR > "" if not isinstance(pre_resolve_var, STR): raise TypeError('pre_resolve_var must be STR! ') if not isinstance(global_var_dic, dict): Raise TypeError('global_var_dic must be dict! ') if not isinstance(global_var_regex, STR): raise TypeError('global_var_regex must be STR! ') if not isinstance(match2key_sub_string_start_index, int): Raise TypeError(' match2KEY_sub_string_STARt_index must be int! ') if not ISinstance (match2KEY_sub_string_end_index, int): Raise TypeError(' match2KEY_sub_string_end_index must be int! ') re_global_var = re.compile(global_var_regex) def global_var_repl(match_obj): start_index = match2key_sub_string_start_index end_index = match2key_sub_string_end_index match_value = global_var_dic.get(match_obj.group()[start_index:end_index]) return match_value if match_value else match_obj.group() resolved_var = re.sub(pattern=re_global_var, string=pre_resolve_var, repl=global_var_repl) return resolved_varCopy the code
Here, you first create regular rules that identify global variables and then replace them with the re.sub method. Where the repl argument in re.sub accepts functions as arguments. The global_var_repl method uses the global_var_dic dictionary to get the matching value and return it.
${GLOBALVAR_NAME} is used to find and replace global variables with global_var_dic, using the default start/end index parameters. I feel a little strange writing this way, but I have not come up with a better way, if you have a better way to implement it welcome to discuss 🙂
Best practices
Let’s simulate the effect of a global variable substitution:
if __name__ == '__main__':
pre_resolve_var = 'left ${status} right, left ${data} right'
global_var_dic = {'status': 'STATUS', 'data': 'DATA'}
resolved_str = resolve_global_var(pre_resolve_var=pre_resolve_var, global_var_dic=global_var_dic)
print(resolved_str)
Copy the code
Here is the console output:
left STATUS right, left DATA right
Process finished with exit code 0
Copy the code
You can see that the output is as expected and the global variables in the string are successfully parsed.
So much for sharing this time ~ see you later 🙂
Welcome to scan code to pay attention to my public number “intelligent automation testing”, reply: advanced test tutorial, you can get free advanced tutorial ~