Explosive finishing, interface automated testing-data-driven framework packaging (practical)


Preface

Interface automation framework—data-driven

The input and output data of the test here are read from data files (data pool, ODBC source, CSV file, EXCEL file, Json file, Yaml file, ADO object, etc.) and generated by capture tool or manually generated code script. 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.

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

E1

2. Create a new ini_file_utils.py file in the common py folder
and write the code:

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:

E2

The value of token_value in the ini configuration file

E3

3. Rewrite the local_config.py file and encapsulate the value in the ini file.

E4

Write code:

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:

E5

4. Modify the common_function.py file and put the obtained token value into the ini file.
Write the code:

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

E5

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

E6

6. Modify the code of the use case layer and obtain the py file of the token.
Code script of the use case layer before modification VS after modification.

E7

After modification

E8

Get the py file of token:

E9

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

E10

View report

E11

The token value is generated in Run_api_tests.py and given to the ini configuration file. 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 token interface must be changed. Different accounts (appid and secret)

The following is the most comprehensive software testing engineer learning knowledge architecture system diagram in 2023 that I compiled.

1. Python programming from entry to proficiency

Please add image description

2. Practical implementation of interface automation projects

Please add image description

3. Web automation project actual combat

Please add image description

4. Practical implementation of App automation project

Please add image description

5. Resumes of first-tier manufacturers

Please add image description

6. Test and develop DevOps system

Please add image description

7. Commonly used automated testing tools

Please add image description

8. JMeter performance test

Please add image description

9. Summary (little surprise at the end)

Struggle is the foundation of life, and persistence is the password of success; pursue excellence, forge ahead, and compose brilliant music with sweat. Believe in your own potential and talents, constantly surpass yourself, and create your own magnificent life picture!

Actively embrace challenges and bravely enter the stage of life; hold dreams in mind and firmly pursue brilliant success. Don’t stop, don’t stop, forge ahead, let go of your passion; believe in your own strength, work hard, and write a brilliant chapter of life!

Confidence lights up the road ahead, sweat waters the brilliant flowers; hard work transcends limits, and dreams dance to the melody of youth. Stick to your passion, chase the distance, and create your own magnificent legend. Work hard, your talents will bloom, move forward bravely, and achieve a wonderful life!

Guess you like

Origin blog.csdn.net/m0_60054525/article/details/132024184