When we want to use python
Send a http
On request , have access to requests
library ; When we want to test an interface, we can also pass requests
Library to request interface , Here is an example to illustrate :
for example , We need to access an interface http://localhost:8080/login
, This is a login interface , The request method is post
, The request parameter is username
and password
, Header information is "Content-Type": "application/json"
, use requests The request code is as follows :
import requests data = { "username": "admin", "password": "123456" } header = { "Content-Type": "application/json", } res = requests.post(url="http://localhost:8080/login", json=data, headers=header) print(res.text)
From the code above, we can see our data data
、 Request header
、 And How to send the request
All mixed together , If it is a single interface, it is OK to say , But there will be dozens in our actual scene 、 Hundreds of interfaces , If you want to write like this, it will be difficult to maintain , At this time, we need some special treatment :
1、 Data separation :
Put the data ( Request parameters ) Put forward separately , Manage alone
2、requests Library secondary encapsulation :
requests Modeling , So that there is no python Basic testers can also write use cases
3、 Public method encapsulation :
For example, mail sending method 、 Database connection method .....
4、 Checkpoint encapsulation :
Second encapsulation assertion method
5、 Use case execution :
Single or batch execution of use cases
6、 The report :
Send test report or nail group report
A set of interface automation is basically this idea , There may be some differences , But the general direction is similar