Talking about the construction of Python+requests+pytest interface automated testing framework

Framework Design Ideas

First of all, it is necessary to clarify the steps required for interface automation, as shown in the following figure:

​Then disassemble the work that needs to be done step by step:

1) Understand the analysis requirements: understand the functions to be realized by the interface

2) Data preparation: Determine the basic situation of the interface according to the development document, know the url, request method, input parameters and other information of the interface, and then predict the output of the interface according to the business logic and input parameters

  • A configuration file is required to store some basic information of the interface;
  • There needs to be a way to read configuration files;
  • An excel or yaml format file is required to store test data;
  • There needs to be a method to read test data;
  • There needs to be a method to assemble the read configuration file and test data into the parameterized data required for test execution;

If the expected result needs to be obtained by entering the reference database, a method that can execute SQL is also required

3) Design & Execute Test: Generate a test file, which can be used to obtain interface output and then verify by calling the interface according to the input parameters

  • According to the characteristics of pytest, the work of preparing parameterized data can be placed under conftest.py;
  • There needs to be a call interface and a test file for assertion verification, and the naming rule is xxx_test.py or test_xxx.py;
  • In order to better verify the output and expected output, prepare to encapsulate the assertions that come with the system;
  • In order to better perform the test, encapsulate the requests module and formulate a unified input and output standard;
  • Record execution error information into the logging module;

4) View the results: Determine whether the interface meets the requirements according to the assertion results. The final execution sequence of the framework is shown in the figure below

Basic Structure of the Framework

​Then introduce what is in each directory and what work is done respectively

1. Each file here corresponds to the test data required by an interface. The naming rule is nameA.xlsx or nameA.yaml (nameA refers to the name of the interface to be tested). The file can store the parameter name, parameter value, Required cookies, expected output

2、assertion.py

--Encapsulated assertion method, used to verify whether the return value of the interface is consistent with the expected result

config.py

--Encapsulate the method of reading the configuration file

get_caseparams.py

-- Process the test data into the format required by the interface

log.py

--Encapsulated logging method

request.py

--Encapsulated https request method, mainly post and get

selectDB.py

--Query sql and process the result into [dic1,dic2,dic3....] format, each dic is a piece of data

testcasetemplate.py

--This is a script that automatically generates test files in the testcase folder based on the test data in the caseparams folder

3. There are two configuration files stored in it, base.ini stores the configuration of database connection interface.ini is the configuration required by all test interfaces.

4. Record the test log file, there is nothing to say, log.log is the file of the current day, and the others are the historical logs under the corresponding date.

5. A file with a fixed name of conftest.py is placed in the same folder as the tested interface script. The function here is to provide parameterization support for each tested script. The test_XXX.py file is the test file of the interface. XXX is the name of the interface under test.

6. run.py --- The main entry point for test execution. If you put the interface automation test framework on jenkins for continuous integration, you can replace this entry point by configuring jenkins .

Instructions for use

When we need to have an interface (assumed to be named abc) that needs to use this framework for automated testing, the steps are as follows:

1. Add a node named abc under the interface.ini file, and then configure the interface parameter file format, http request method, interface url, and interface wiki under this node.

2. Create a use case data file named abc.xlsx under the casefparams folder.

3. Execute the testcasetemplate.py under the common folder. The result of the execution is that the script file test_abc.py will be automatically generated under the casefile folder according to the content configured in the previous two steps.

4. Edit test_abc.py to add the required assertions to verify the accuracy of the interface.

5. Execute run.py to complete the test.

The test execution details are run.py to specify the path of the test file to be tested in the testcase directory. According to the operating mechanism of pytest, the conftest.py in the testcase directory will be called first. The function of this file here is to traverse the testcase directory. The test file starting with test_, then call get_caseparams.py that reads the configuration file config.py, reads and assembles the parameters, parameterizes the test case data and passes it to the test file starting with test_, and executes the test file starting with test_ When it is done, the parameters passed in the previous steps will be split, and then request.py will be called to send an http request to obtain the return result in json format of the interface, and then the return result of the interface will be verified in the following way.

1) According to the product business logic, when it is easy to get the expected result when the input parameters are known, you can directly put the expected results into the excel or yaml test data file together with the input parameters. When executing the test, these parameters It will also be passed to the test file, just take it out and verify it with the interface output.

2) In the case of needing to check the database for verification, you need to write the sql as the condition of the input parameter of the interface as sql, then call selectDB.py to get the query result, and then use the result returned by sql and the interface output for verification. 3) There are also some complicated scenarios It is possible to enter parameters according to the interface, write scripts according to the business logic to obtain the expected results, and then verify with the interface output.

Finally: In order to give back to the die-hard fans, I have compiled a complete software testing video learning tutorial for you. If you need it, you can get it for free【保证100%免费】

Software Testing Interview Documentation

We must study to find a high-paying job. The following interview questions are the latest interview materials from first-tier Internet companies such as Ali, Tencent, and Byte, and some Byte bosses have given authoritative answers. Finish this set Interview information I believe everyone can find a satisfactory job

Guess you like

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