In doing automated testing, often a whole set of business processes on a set of interfaces, this time there is often a data dependency between the interfaces, so specifically how to achieve this dependency.
The idea is as follows.
- Extracts the return value of the previous interface and stores 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.
- Send a request.
Core code implementation.
Extract the return value of the interface and store it in the global variable dictionary.
# Extract the return value of the interface and store it in the global variable dictionary if set_global_vars and isinstance(set_global_vars, list): for set_global_var in set_global_vars: if isinstance(set_global_var, dict): name = set_global_var.get('name') # name Represents the name of the global variable query = set_global_var.get('query') # query Query statement on behalf of a global variable value = common.dict_get(response_json, query) # response_json represents the response data of the interface self.global_vars[name] = value
where set_global_vars represents the list of global variable settings for the current test case, and self.global_vars represents the global variable dictionary for the test class instance; for the implementation of the common.dict_get method, go toMethod implementation
Parsing global variables in strings and replacing them
import re # Parsing global variables in strings and replacing them 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: The variable to prepare for parsing <str>. :param global_var_dic: Dictionary of global variables <dict>. :param global_var_regex: Global variable regular expression to recognize <str>; :param match2key_sub_string_start_index: The start index of the global variable expression intercepted into the global variable dictionary key value string <int>; :param match2key_sub_string_end_index: The end index of the global variable expression intercepted as a global variable dictionary key string <int>; :return: The parsed variable <int>. :return: Parsed variable <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 = (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 = (pattern=re_global_var, string=pre_resolve_var, repl=global_var_repl) return resolved_var
Here, the regular rules for recognizing global variables are first created and then the methods are applied to replace them. In this case, the repl parameter in can accept a function as an argument. global_var_repl method uses the global_var_dic dictionary to get the matching value and return it.
The default parameter identifies the global variable as: ${GLOBALVAR_NAME}, and when global_var_dic is used to find and replace the global variable, the default start/stop index parameter is used. This feels a bit strange to me, but I can't think of a better way to do it at the moment, so feel free to discuss it if you have a better implementation idea :)
best practice
Let's simulate the effect of a global variable substitution once: the
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)
The following is the console output.
left STATUS right, left DATA right
Process finished with exit code 0
You can see that the output is still as expected, parsing the global variables in the string successfully.
This is the whole content of this article.