当前位置:网站首页>Interface test API case, data and interface separation
Interface test API case, data and interface separation
2022-07-07 09:16:00 【Is winter coming】
Purpose : Complete interface automation test , The most important one is use cases 、 data 、 Separation of interfaces
Now simulate the test of a login interface ,
Original request :
import pytest,json,requests
def test_login():
url = "https://blog.csdn.net/"
payload = json.dumps({
"tel": "123456",
"password": "123456"
})
headers = {
'Content-Type': 'application/json'
}
response = requests.request(method="POST", url=url,headers=headers ,data=payload)
print(response.json)problem :
Multiple use cases for this interface , You have to write many times , The code repetition rate is too high
Method :
Use cases and interfaces 、 Data separation
Interface file :get_token.py
import json,requests
# api Each method in represents the request method of an interface
class GET_Token():
def get_login(self):
url = "https://blog.csdn.net/"
payload = json.dumps({
"tel": "123456",
"password": "123456"
})
headers = {
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
return response.json()
if __name__ == '__main__':
t = GET_Token()
t.get_login()Use case files :test_demo.py
# testcase It's a test case , One method Corresponding to one case, Use cases are separated from interfaces
import pytest
from get_token import GET_Token
class Test_token():
def setup(self):
self.gettoken = GET_Token()
# Determine whether the login is successful , Compare the response value returned by the interface
def test_token(self):
print(self.gettoken.get_login())
assert self.gettoken.get_login()['code'] == 0Now the interface file is still confused , You can sort out the data format , Adopt unified format , such as JSON
After finishing get_token.py
import json,requests
# api Each method in represents the request method of an interface
class GET_Token():
def get_login(self):
# Transform the request information into a standard dictionary structure
req = {
"method": "POST",
"url": "https://blog.csdn.net/",
"headers": {
'Content-Type': 'application/json'
},
"data": json.dumps({
"tel": "1234456",
"password": "1234456"
})
}
# unpacking
rs = requests.request(**req)
print(rs.text)
return rs
if __name__ == '__main__':
t = GET_Token()
t.get_login()Now use cases and interfaces 、 The data was successfully separated , If the use case or interface changes, you can directly modify the corresponding file , One use case is for multiple pieces of data , Directly prepare multiple data replacements req that will do ( Put it in the data file to read , Traverse )
边栏推荐
- How to count the number of project code lines
- Led analog and digital dimming
- Port multiplexing and re imaging
- Simulation volume leetcode [general] 1567 Length of the longest subarray whose product is a positive number
- 数据在内存中的存储
- Simulation volume leetcode [general] 1706 Where does the ball meet
- Common operating commands of Linux
- 5A summary: seven stages of PMP learning
- H3C VXLAN配置
- External interrupt to realize key experiment
猜你喜欢

External interrupt to realize key experiment

Mysql database transaction learning notes

Storage of data in memory

Postman interface test (II. Set global variables \ sets)

串口实验——简单数据收发

Implementation of corner badge of Youmeng message push

Calf problem

STM32 serial port register library function configuration method

MySQL common statements
![[SVN] what is SVN? How do you use it?](/img/45/a7df8989f18f0a6185582389398d1a.png)
[SVN] what is SVN? How do you use it?
随机推荐
Storage of data in memory
5A summary: seven stages of PMP learning
Original collection of hardware bear (updated on June 2022)
Detailed learning notes of JVM memory structure (I)
Run can start normally, and debug doesn't start or report an error, which seems to be stuck
systemd
Register address name mapping
DRF defines views and routes
C语言指针(中篇)
How can I apply for a PMP certificate?
What is the rating of Huishang futures company? Is it safe to open an account? I want to open an account, OK?
Port occupation troubleshooting
Chaosblade: introduction to chaos Engineering (I)
Reading notes of pyramid principle
Postman interface test (I. installation and use)
Simulation volume leetcode [general] 1567 Length of the longest subarray whose product is a positive number
What is the value of getting a PMP certificate?
Implementation of corner badge of Youmeng message push
Skill review of test engineer before interview
Pycharm create a new file and add author information