当前位置:网站首页>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'] == 0
Now 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 )
边栏推荐
- Port occupation troubleshooting
- Detailed learning notes of JVM memory structure (I)
- Count the number of words in the string c language
- PMP Exam Preparation experience systematically improve project management knowledge through learning
- Self awakening from a 30-year-old female programmer
- Pycharm importing third-party libraries
- JVM garbage collection detailed learning notes (II)
- How long does the PMP usually need to prepare for the exam in advance?
- What are the suggestions for PMP candidates?
- NVIC中断优先级管理
猜你喜欢
PMP Exam Preparation experience systematically improve project management knowledge through learning
C language pointer (Part 1)
数据在内存中的存储
C语言指针(中篇)
串口实验——简单数据收发
Upgrade Alibaba cloud RDS (relational database service) instance to com mysql. jdbc. exceptions. Troubleshooting of jdbc4.communicationsexception
[SVN] what is SVN? How do you use it?
On December 8th, 2020, the memory of marketing MRC application suddenly increased, resulting in system oom
Mysql数据库-锁-学习笔记
Common short chain design methods
随机推荐
Reflections on the way of enterprise IT architecture transformation (Alibaba's China Taiwan strategic thought and architecture practice)
Entity of cesium data visualization (Part 1)
[chaosblade: node disk filling, killing the specified process on the node, suspending the specified process on the node]
Self awakening from a 30-year-old female programmer
What is the use of PMP certificate?
Calf problem
On December 8th, 2020, the memory of marketing MRC application suddenly increased, resulting in system oom
LED模拟与数字调光
Error: selenium common. exceptions. WebDriverException: Messag‘geckodriver‘ execute
Postman interface debugging method
H3C vxlan configuration
Storage of data in memory
个人力扣题目分类记录
OpenGL三维图形绘制
RuntimeError: Calculated padded input size per channel: (1 x 1). Kernel size: (5 x 5). Kernel size c
OpenGL 3D graphics rendering
5A summary: seven stages of PMP learning
C语言指针(下篇)
Leetcode daily questions (2316. count unreachable pairs of nodes in an undirected graph)
JWT certification used in DRF