Modular test script framework for interface automation test ideas and practice

Modular Test Scripting Framework

  It is necessary to create independently descriptive modules, program fragments, and scripts of the application under test. These small scripts are combined to form test case scripts that run specific tests independently.

scene one:

  The development changed the address of the access_token interface from /cgi-bin/token to /cgi-bin/get_token or modified parameters, etc. == "The development adjusted the commonly used interface information; this scenario will cause most parts of the test script to be maintained. And I am worried about future adjustments; therefore, a modular testing framework is used to solve it.

  Since the operation of obtaining token is very frequently used in the code, it is made public

Step 1. Create a new common_function.py file under the common folder to encapsulate the method

 Write code:

# encoding: utf-8
# @author: Jeffrey
# @file: commom_function.py
# @time: 2022/7/26 21:01
# @desc: 模块化框架
import jsonpath

def get_access_token_value(session_obj):
    """获取access_token的值"""
    url_params = {"grant_type": "client_credential",
                  "appid": "wxf14419077f707856",
                  "secret": "92a113bd4b5ffdc72144740dc7123c99"}
    response = session_obj.get(url="https://api.weixin.qq.com/cgi-bin/token",
                                params=url_params)
    # 获取响应json中的access_token的值
    token_value = jsonpath.jsonpath(response.json(), "$.access_token")[0]
    return token_value

Step 2. Import the public module in the test_create_user_tag_api.py file, and delete the original redundant code; as shown below

The benefits of the above code: 1. Reduce code redundancy; 2. Convenient maintenance (the scenario described above)

Scene two:

The company's test environment address changes, such as switching the domain name from api.weixin.qq.com to 192.168.1.12

Step 3. Create a new local_config.py file under the common folder

Step 4. Put the values ​​in the local_config.py file into the use case layer, as shown in the figure below

View execution results

Note: When executing through the above use case layer, each use case needs to obtain an access_token once. In fact, the access_token is valid for 2 hours after each acquisition, and it will be resolved later.

Practical case

Optical theory is useless, you have to learn to follow along, and you have to do it yourself, so that you can apply what you have learned to practice. At this time, you can learn from some actual combat cases.

If it is helpful to you, please like and collect it to give the author an encouragement. It is also convenient for you to quickly find it next time.

If you don’t understand, please consult the small card below. The blogger also hopes to learn and progress with like-minded testers

At the right age, choose the right position, and try to give full play to your own advantages.

My road of automated test development is inseparable from the plan of each stage along the way, because I like planning and summarizing,

Test and develop video tutorials, study notes and receive portals! ! !

Guess you like

Origin blog.csdn.net/Liuyanan990830/article/details/130473687