Implementation method of interface data dependency for python interface automated testing

This article mainly introduces the implementation method of the interface data dependence of the python interface automation test. The editor thinks it is quite good. Now I will share it with you and give you a reference. Let's come and see with me

When doing automated testing, a whole set of business processes is often tested on a set of interfaces. At this time, there are often data dependencies between interfaces, so how to implement this dependency.

The idea is as follows:

  1. The return value of the interface before extraction is stored in the global variable dictionary.
  2. When initializing an interface request, parse and replace global variables in the request header, request parameters, and other information.
  3. Make a request.

Core code implementation:

The return value of the extracted interface is stored 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 代表全局变量的名字

      query = set_global_var.get('query') # query 代表全局变量的查询语句

      value = common.dict_get(response_json, query) # response_json 代表接口的响应数据

      self.global_vars[name] = value

Among them, 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 the specific implementation of the common.dict_get method, please move to the specific implementation

Parse the global variables in the string and replace them

import re



# 解析字符串中全局变量并进行替换

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: 准备进行解析的变量<str>

  :param global_var_dic: 全局变量字典<dict>

  :param global_var_regex: 识别全局变量正则表达式<str>

  :param match2key_sub_string_start_index: 全局变量表达式截取成全局变量字典key值字符串的开始索引<int>

  :param match2key_sub_string_end_index: 全局变量表达式截取成全局变量字典key值字符串的结束索引<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_var

Here, first create a regular rule to identify global variables, and then use the re.sub method to replace. Among them, the repl parameter in re.sub can accept functions as parameters. In the global_var_repl method, use the global_var_dic dictionary to get the matching value and return it.

In the default parameters, the global variable is identified as follows: ${GLOBALVAR_NAME}, when global_var_dic is used to find and replace the global variable, the default preset start and end index parameters are used. I feel a little strange about this way of writing, but I haven’t come up with a better way so far. If you have a better way to implement it, please discuss it :)

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)

Here is the console output:

left STATUS right, left DATA right
Process finished with exit code 0

It can be seen that the output is still in line with expectations, and the global variables in the string are successfully parsed.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you will support me a lot!

The following is the supporting information. For friends who do [software testing], it should be the most comprehensive and complete preparation warehouse. This warehouse also accompanied me through the most difficult journey. I hope it can help you too!

Software testing interview applet

The software test question bank maxed out by millions of people! ! ! Who is who knows! ! ! The most comprehensive quiz mini program on the whole network, you can use your mobile phone to do the quizzes, on the subway or on the bus, roll it up!

The following interview question sections are covered:

1. Basic theory of software testing, 2. web, app, interface function testing, 3. network, 4. database, 5. linux

6. web, app, interface automation, 7. performance testing, 8. programming basics, 9. hr interview questions, 10. open test questions, 11. security testing, 12. computer basics

Information acquisition method:

Guess you like

Origin blog.csdn.net/myh919/article/details/131868129