Interface automated testing ideas and practice (4): Data-driven testing framework

Table of contents

Data driven testing framework

Step 1. Create a new conf folder in the project root directory and create a new config.ini file below.

Step 2. Create a new ini_file_utils.py file in the common py folder.

Step 3, rewrite the local_config.py file to encapsulate the value in the ini file.

Step 4. Modify the common_function.py file and put the obtained token value into the ini file.

Step 5. Modify the run_api_tests.py file and first write the token value into the ini file.

Step 6. Modify the code of the use case layer and obtain the py file of the token

Step 7. Execute the run_api_tests.py file and view the execution results;


Data driven testing framework

  The input and output data tested here are read from data files (data pools, ODBC sources, CSV files, EXCEL files, Json files, Yaml files, ADO objects, etc.) and generated by capture tools or manually generated code scripts. Load into variables. In this framework, variables are used not only to store input values ​​but also to store output verification values. Throughout the program, the test script reads the numerical file and records the test status and information. This is similar to table-driven testing, in which the test cases are contained in the data file rather than in the script. The script is just a "driver", or a delivery mechanism, for the data.

To put it more simply: the test data and code used in the testing process are written separately and stored separately.

For example: the data appid-sercet and expected results used in testing the token interface are put into a data file in advance.

For example: the access_token in the project is a publicly required test data. It is generated once and has a validity period of 7200 seconds. Generally, all interface tests can be executed in 2 hours.

Achieve one-time acquisition of the token value, save it to a file, and then use the token value in the file to complete the test.

Step 1. Create a new conf folder in the project root directory and create a new config.ini file below.

Step 2. Create a new ini_file_utils.py file in the common py folder.

 Write code:

# encoding: utf-8
# @author: Jeffrey
# @file: ini_file_utils.py
# @time: 2022/7/31 16:22
# @desc: 读取、写入ini文件
import os
import configparser


class IniFileUtils:  #和框架业务无关的底层代码==》公共底层代码

    def __init__(self,file_path):
        self.ini_file_path = file_path
        self.conf_obj = configparser.ConfigParser()
        self.conf_obj.read(self.ini_file_path, encoding='utf-8')

    def get_config_value(self,section, key):
        value = self.conf_obj.get(section, key)
        return value

    def set_config_value(self,section, key, value):
        self.conf_obj.set(section, key, value)
        config_file_obj = open(self.ini_file_path, 'w')
        self.conf_obj.write(config_file_obj)
        config_file_obj.flush()
        config_file_obj.close()

if __name__ == '__main__':
    current_path = os.path.dirname(__file__)
    config_file_path = os.path.join(current_path, '../conf/config.ini')
    ini_file = IniFileUtils(config_file_path)
    print(ini_file.get_config_value('default', 'HOSTS'))
    ini_file.set_config_value('default','TOKEN_VALUE', 'SSS9090')

Execute to view the results:

The value of token_value in the ini configuration file

Step 3, rewrite the local_config.py file to encapsulate the value in the ini file.

 Write code:

# encoding: utf-8
# @author: Jeffrey
# @file: local_config.py
# @time: 2022/7/26 21:25
# @desc:  封装读取ini文件中的值
import os
from common.ini_file_utils import IniFileUtils


current_path = os.path.dirname(os.path.abspath(__file__))
config_file_path = os.path.join(current_path, '../conf/config.ini')


class LocalConfig():  # #和框架业务有关系的底层代码

    def __init__(self,file_path = config_file_path):
        self.ini_file_obj = IniFileUtils(file_path)
    @property
    def get_hosts(self):
        '''获取ini文件中的hosts值'''
        hosts_value = self.ini_file_obj.get_config_value('default', 'hosts')
        return hosts_value
    @property
    def get_token_value(self):
        '''获取ini文件中的token_value值'''
        token_value = self.ini_file_obj.get_config_value('default','token_value')
        return token_value

local_config = LocalConfig()

if __name__ == '__main__':
    print(local_config.get_hosts)
    print(local_config.get_token_value)

View execution results:

Step 4. Modify the common_function.py file and put the obtained token value into the ini file.

 Write code:

# encoding: utf-8
# @author: Jeffrey
# @file: common_function.py
# @time: 2022/7/26 21:01
# @desc: 模块化框架
import os
import jsonpath
from common.local_config import local_config
from common.common_api_info import CommonApiInfo
from common.ini_file_utils import IniFileUtils


current_path = os.path.dirname(os.path.abspath(__file__))
config_file_path = os.path.join(current_path, '../conf/config.ini')

def save_access_token_value_info_ini_file(session_obj,hosts):
    """获取access_token的值并写入到ini文件中"""
    response = CommonApiInfo(session_obj,hosts).get_access_token_api('client_credential',
                                                          'wxf14419077f707856',
                                                          '92a113bd4b5ffdc72144740dc7123c99')
    # 获取响应json中的access_token的值
    token_value = jsonpath.jsonpath(response.json(), "$.access_token")[0]
    # 把获取到的access_token值写入到ini文件中
    IniFileUtils(config_file_path).set_config_value('default', 'token_value', token_value)
    # return token_value

if __name__ == '__main__':
    import requests
    save_access_token_value_info_ini_file(requests.session(), local_config.get_hosts)

After execution, check the value of token_value in the ini file:

The latest token value has been written

Step 5. Modify the run_api_tests.py file and first write the token value into the ini file.

Step 6. Modify the code of the use case layer and obtain the py file of the token

Use case layer code script before modification VS after modification

VS

After modification

Get the py file of token:

Step 7. Execute the run_api_tests.py file and view the execution results;

View report

In Run_api_tests.py, the token value is generated for the ini configuration file. Then when the test gets the token interface, the generated token value is called. In order to prevent the later generated value from overwriting the previous value and causing the ini configuration file to become invalid, the test to get the token interface needs to be changed. Different accounts (appid and secret)

Note: The above is the difference between each framework. It talks about linear script·--·modular framework·--·library framework·--·data-driven


 The following are relatively good learning tutorial resources that I have collected. Although they are not very valuable, if you happen to need them, you can leave a message in the comment area [777] and just take them away.

Friends who want to get information, please like + comment + collect , three times in a row!

After three consecutive rounds , I will send you private messages one by one in the comment area~

Guess you like

Origin blog.csdn.net/m0_70618214/article/details/133310843