current position:Home>Python based interface automation test project, complete actual project, with source code sharing

Python based interface automation test project, complete actual project, with source code sharing

2022-01-31 10:48:35 Program yuanxia

One 、 Preface

This is a base python Interface automation test project , Use excel Manage test cases , Client operation , You don't need to pay attention to the code in the process of use . The design mainly follows two principles : Simple and easy to use 、 Cover common scenes .

Two 、 The basic structure

Project contains 3 Parts of , The configuration file 、 Use case files 、 client .

3、 ... and 、 The page display

1、 The configuration file

2、 Use case files The use case file is excel Format , Two formats of excel, But not recommended .xlsx Format , Because the program deals with .xlsx The format is openpyxl library , Poor performance . Simple and intuitive , Contains interface information , Fields to be verified , Query whether the data is warehoused ( use sql Statements for ), Fixed value , Check whether the data is stored in the warehouse , Response content , Response head , Status code , Data initialization , Data recovery and so on

part101part301 Is the data that needs to be verified section101section301 It's the expected result This part is divided into 3 There are two different verification methods : Part101~section101: Fixed value verification Part101 Fill in JSONPATH Such as ['sites'][0]['name'] Section101 Fill in a fixed expected result, such as :helloworld

Part201~section201: Database verification Part201 It's also JSONPATH, Such as ['sites'][0]['name'] Section201 At this time, a fixed value is no longer filled in , But the data queried from the database , It can be written like this :select 'helloworld' When you need attention , If you need to write in the use case sql sentence ( Not just here ), Then you need to select the database first

Part301~section301: Check whether the data is stored in the warehouse Sometimes this part of verification can be used to verify whether the data is actually stored after the interface request Part301 Fill in sql sentence Section301 Fill in the expected results

JSON analysis : If the interface response level is deep , have access to “JSON analysis ” The function parses the interface response into key-value form , Then directly copy what you want to use jsonpath that will do .

above part101~section301 Part is accurate verification

Fuzzy check : If you don't want to carry out accurate verification , Just want to see if some values are included in the interface response or response header , You can use the response assertion resText or resHeader.

expression : The above verification can usually meet most scenarios , But some scenes can't be well satisfied , Such as , I might want to check the length of a list in the response , Or whether a field is a mobile phone number , At this time, you need to use an expression to verify , The expression here is actually a sentence python Code , If I want to check the length of the list in the interface response, I can write this :len(r.json()['sites'])==3, Note here : The interface response object in the program is “r”.

Database initialization 、 Data recovery 、 Dynamic parameters : These three parts are sql sentence seeing the name of a thing one thinks of its function , Data initialization is the initialization of the database before the interface request , Data recovery refers to the recovery operation of the database after the interface request . Dynamic parameters were originally based on this consideration , The request parameters of some interfaces are not fixed , Then we need to get from the database in real time , If the parameter ID Since the increase , We query the latest information from the database in real time ID Assign to parameter list , It can be written like this :select ID from XX where XX, Using this query in the parameter list ID It needs to be written like this :{“ID”:”${ dyparam001 }”}, Of course, dynamic parameters are not only used for self increasing parameters , Any data extracted from the database can be used as dynamic parameters , If you use sql Random a mobile phone number or call md5 Function to encrypt a value .

${ Variable name },

This is the reference format of variables in this project , There is... In the project 3 Two variables , User variables , Dynamic parameters and interface variables , All use the same reference format .

User variables : User variables are set in the configuration file , Use as a global variable .

Interface variables : In many cases, there will be dependencies between interfaces , For example, other interfaces depend on the login interface , At this time, you need to set the interface variable . Such as B Interface dependency A One of the interfaces [‘token’] Field , Can be in A Interface key001 Fill in [‘token’] stay value001 Fill in token,key001 Is a dependent field ,value001 Is the variable name set for this field , stay B You can use... In the interface ${token} To call this variable .

Header Manager : The header manager can be implicitly called by subsequent interfaces , Or to AB Two interfaces, for example , If B The request header is required for interface request , Of course, it can be in B Of header Fill in , But if B Interface after CDEF…… All need to use the same request header , Then you can use the header Manager , Can be in A Fill in the header manager of {“token”:”${token}”}, If B The... Is not filled in header, that B Will be quoted A Header Manager , If B The request header is filled in , So the header manager is B It doesn't work in the interface .

database : The database information is configured in the configuration file , Support 3 Species database ,oracle,sqlserver.mysql Preset in the project 3 A database :DB1,DB2,DB3, You can also customize the database in the configuration file , Use ${} Just call the form .

The number of iterations : It should be noted that the number of iterations is 0 That is, the use case does not execute .

Many columns in the template can be added or deleted according to actual needs , Such as , I want to check 5 A field , So in part101 Just add a few columns later , At the same time in the corresponding section101 Add the same number of columns after ; If two dynamic parameters are used , It's just dyparam001 Just add a column after , Use ${dyparam002} quote .

3、 client Click the select button to select the use case file , Then select the corresponding interface module in the use case file first , Click Start , You can test the interface force , The result of interface success or error report appears in the window . The right side shows that the interface is successful 、 Failure 、 Abnormal data analysis .

analysis JSON Button implementation interface response is parsed into JSON Format , It is convenient to fill in the data verification in the use case file .

After the implementation of the interface use case is completed , Click on excel Report and html The report , You can view the report of this interface execution , Facilitate the analysis of later interface test .

After an error is reported on the execution interface , We can click the view log button , View the request of the executed interface , Respond to And then analyze the cause of interface error .

In view of the space limit , Unable to display documents and project source code , Those who need project practice can pay attention to it and click the link below to get it for free

Actual project entrance

copyright notice
author[Program yuanxia],Please bring the original link to reprint, thank you.

Random recommended