Interface automation testing ideas and practical data-driven testing framework

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 code scripts generated by capture tools or manually generated by loaded into the variable. In this framework, variables are used not only to store input values ​​but also to store output validation values. Throughout the program, the test script reads the value file and records the test status and information. This is similar to table-driven testing, where test cases are contained in data files rather than scripts, and the script is merely a "driver", or delivery mechanism, for the data.

To make it more popular: the test data and code used in the test process are written separately and stored separately.

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

For example: the access_token in the project is a public requirement test data, it is valid for 7200 seconds once generated, and generally all interface tests can be executed in 2 hours.

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

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

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

 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, encapsulate and read 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)

Check the value of token_value in the ini file after execution:

The latest token value has been written

Step 5. Modify the run_api_tests.py file, 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

The code script of the use case layer before modification VS after modification

VS

after modification

Get the py file of the token:

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

view report

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

Remarks: The above is that each framework is different, from the linear script - - - modular framework - - - library framework - - - data-driven

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/130506469