当前位置:网站首页>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 )
边栏推荐
- 2022-07-06 Unity核心9——3D动画
- DRF authentication, permissions, and flow restrictions (only for views in DRF)
- Cesium load vector data
- What is the rating of Huishang futures company? Is it safe to open an account? I want to open an account, OK?
- Cmake command line use
- 【Istio Network CRD VirtualService、Envoyfilter】
- Goldbach conjecture C language
- What is the use of PMP certificate?
- [chaosblade: node CPU load, node network delay, node network packet loss, node domain name access exception]
- Serial port experiment - simple data sending and receiving
猜你喜欢
Locust performance test 3 (high concurrency, parameter correlation, assembly point)
2022-07-06 Unity核心9——3D动画
Hard core sharing: a common toolkit for hardware engineers
Locust performance test 4 (custom load Policy)
UnityShader入门精要个人总结--基础篇(一)
C language pointer (Part 2)
2022-07-06 unity core 9 - 3D animation
Mysql数据库-锁-学习笔记
【Istio Network CRD VirtualService、Envoyfilter】
Confitest of fixture py
随机推荐
【istio简介、架构、组件】
How does the project manager write the weekly summary and weekly plan?
Port occupation troubleshooting
STM32串口寄存器库函数配置方法
C language pointer (exercises)
Pytest+request+allure+excel interface automatic construction from 0 to 1 [familiar with framework structure]
Postman interface debugging method
2022-07-06 unity core 9 - 3D animation
Leetcode刷题记录(数组)组合总和、组合总和 II
【Istio Network CRD VirtualService、Envoyfilter】
Serializer & modelserializer of DRF serialization and deserialization
Locust performance test 4 (custom load Policy)
Cesium does not support 4490 problem solution and cesium modified source code packaging scheme
Do you have any certificates with high gold content?
Data association between two interfaces of postman
Error: selenium common. exceptions. WebDriverException: Messag‘geckodriver‘ execute
【ChaosBlade:节点 CPU 负载、节点网络延迟、节点网络丢包、节点域名访问异常】
DRF defines views and routes
Simulation volume leetcode [general] 1706 Where does the ball meet
OpenGL 3D graphics rendering