当前位置:网站首页>The interface automation test with a monthly salary of 12k+ takes you to get started in 3 minutes
The interface automation test with a monthly salary of 12k+ takes you to get started in 3 minutes
2022-07-26 07:16:00 【Peng Yuyan in the testing industry】

One 、 What is interface automation testing ?
A process in which a program or tool is used to test an interface instead of manual automation .
Two 、 Realization way :
Tools (Jmeter,postman)
Code (python-requests)
3、 ... and 、 Disadvantages of test tools :
The test data is not easy to control ( Cannot read or store directly json Format )
It is inconvenient to encrypt test data
Lack of scalability ( Complex business logic )
Four 、 The goal of this study is : be based on python combination requesrs The library realizes interface automatic test
5、 ... and 、Requests Library Introduction :
Use python Language writing
Using open source protocols , be based on urllib Secondary encapsulation of Library
Requests The corresponding interface test methods are encapsulated in the library
1、GET Method ( Response status code :200)
#1. Guide pack
import requests
#2. call get
url="http://127.0.0.1:8080/demo/cheliangxinxi/list/"
r=requests.get(url)
#3. Get request url Address
print("url by :",r.url)
#4. Get response status code
print(" Status code for :",r.status_code)
#5. Get response information , Text form
print(" The response information is :",r.text)
print(r.headers)
l=r.encoding='ASCI'
print(r.encoding)2、GET Method request with parameters
#1. Guide pack
import requests
#2. call get
url=" use Baidu Search , You will know "
# Parameters :params: Dictionary or string ( A dictionary is recommended )
# Dictionaries
params={"id":1001,"kw":" Beijing "} # %2c ASCI The value is comma
# character string
#r=requests.get(url,params="id=1001")
r=requests.get(url,params=params)
# Writing is not recommended
#url=" use Baidu Search , You will know "
#3. Get request url Address
print("url by :",r.url)
#4. Get response status code
print(" Status code for :",r.status_code)
#5. Get response information , Text form
print(" The response information is :",r.text)3、PUT Method ( Response status code :200 or 201)
import requests
url="http://127.0.0.1:8080/demo/cheliangxinxi/edit/180/"
headers={"Content-Type":"application/json"}
data={
"data": [{
"id": "180",
"chehao":"T01",
"pinzhong": "Test college update"
}]
}
r=requests.put(url,json=data,headers=headers)
print(r.json())
print(r.status_code)4、POST Method ( Response status code :201)
import requests
url="http://127.0.0.1:8080/demo/cheliangxinxi/add/"
headers={"Content-Type":"application/json"}
data={
"data": [{
"chehao":"T01",
"pinzhong": "Test college "
}]
}
r=requests.post(url,json=data,headers=headers)
print(r.json())
print(r.status_code)
print(r.text)5、 Pass parameters json And data The difference in format ?
data: A dictionary object
json:json character string
6、 How to convert dictionary objects to json character string ?
1. Import json package
2.json.dumps( A dictionary object )
7、 The response data json() And text difference ?
json(): Double quotes , Return type Dictionary , You can get the value of the response through the key name
text: Single quotation marks , The type returned is string , Unable to get the value of the response from the key name
8、DELETE Method ( Response status code :204)
import requests
url="http://127.0.0.1:8080/demo/cheliangxinxi/remove/190/"
r=requests.delete(url)
print(r.status_code)6、 ... and 、 Method of response object
1.encoding
View the default request encoding format : The response object .encoding
Set the response encoding format The response object .encoding=”utf-8”
2.headers Get the server response header
The response object .headers
3.cookies Get a response cokkies Information ( The server generates )
4.text: Parse in text form
5.content: Parse the response content in bytecode form
6.json() With json Parse the response content in string format
7、 ... and 、 Case study :
TPShop The verification code obtained by the mall , Landing successful , Get order information
import requests
import json
# Get verification code
url="http://demo6.tp-shop.cn/index.php?m=Home&c=User&a=verify"
r=requests.get(url)
# obtain cookie
r_cookie=r.cookies
print(r_cookie)
print(" Get... Alone cookie value :",r_cookie['PHPSESSID'])
# Set up cookie Variable
cookies={"PHPSESSID":r_cookie['PHPSESSID']}
url_login="http://demo6.tp-shop.cn/index.php?m=Home&c=User&a=do_login"
data={
"username": "15991986680",
"password": "Wxm131411",
"region": "8888"
}
r_login=requests.post(url=url_login,json=data,cookies=cookies)
print(r_login.json())
# Get order information
url_order="http://demo6.tp-shop.cn/index.php/Home/Order/order_list.html"
r=requests.get(url=url_order,cookies=cookies)
print(r.text)Session: Complete a session ( Start by creating a request connection between the client and the server , Disconnect from client and server... End )
session It can automatically save the data generated by the server cookies Information , And automatically attach... To the next request .
application : adopt session object . Method
session.get()
session.put()
session.delete()
Whether it's requests still session The results are all responsebody
8、 ... and 、TPShop Case study session edition
import requests
import json
# Get verification code
url="http://demo6.tp-shop.cn/index.php?m=Home&c=User&a=verify"
r=requests.get(url)
# obtain cookie
r_cookie=r.cookies
print(r_cookie)
print(" Get... Alone cookie value :",r_cookie['PHPSESSID'])
# Set up cookie Variable
cookies={"PHPSESSID":r_cookie['PHPSESSID']}
url_login="http://demo6.tp-shop.cn/index.php?m=Home&c=User&a=do_login"
data={
"username": "15991986680",
"password": "Wxm131411",
"region": "8888"
}
session=requests.session()
r_login=session.post(url=url_login,json=data)
print(r_login.json())
# Get order information
url_order="http://demo6.tp-shop.cn/index.php/Home/Order/order_list.html"
r=session.get(url=url_order)
print(r.text)Nine 、unittest And requests combination
Inherit unittest.TestCase, With test The first method is a use case
setup:
effect :test Before starting method execution , Will be executed first
teardown:
effect :test After starting method execution , Will be performed
test_login_success:
effect : Landing successful
test_username_not_exist
effect : Login failed , The username does not exist
test_password_error
effect : Login failed , Wrong password
import unittest
import requests
class TPShopLogin(unittest.TestCase):
def setUp(self):
# obtain session object
self.session=requests.session()
# land url
self.url_login="http://demo6.tp-shop.cn/index.php?m=Home&c=User&a=do_login"
# Verification Code url
self.url_verify="http://demo6.tp-shop.cn/index.php?m=Home&c=User&a=verify"
def tearDown(self):
self.session.close()
# close session
def test_login_sucess(self):
# Request verification code
self.session.get(self.url_verify)
data = {
"username": "15991986680",
"password": "Wxm131411",
"region": "8888"
}
# Request to log in
r=self.session.post(self.url_login,json=data)
# Assertion
try:
self.assertEqual(" Landing successful ",r.json()['msg'])
except AssertionError as e:
print(e)
def test_username_not_exist(self):
# Request verification code
self.session.get(self.url_verify)
data = {
"username": "159919866801",
"password": "Wxm131411",
"region": "8888"
}
# Request to log in
r = self.session.post(self.url_login, json=data)
# Assertion
try:
self.assertEqual(" The username does not exist ", r.json()['msg'])
except AssertionError as e:
print(e)
def test_password_error(self):
# Request verification code
self.session.get(self.url_verify)
data = {
"username": "15991986680",
"password": "Wxm13141111",
"region": "8888"
}
# Request to log in
r = self.session.post(self.url_login, json=data)
# Assertion
try:
self.assertEqual(" Wrong password ", r.json()['msg'])
except AssertionError as e:
print(e)
def test_verify_error(self):
# Request verification code
self.session.get(self.url_verify)
data = {
"username": "15991986680",
"password": "Wxm13141111",
"region": "8888"
}
# Request to log in
r = self.session.post(self.url_login, json=data)
# Assertion
try:
self.assertEqual(" Verification code error ", r.json()['msg'])
except AssertionError as e:
print(e)
if __name__=='__main__':
unittest.main()A little help
Finally, thank everyone who reads my article carefully , Watching the rise and attention of fans all the way , Reciprocity is always necessary , Although it's not very valuable , If you can use it, you can take it

These materials , For those who want to advance 【 automated testing 】 For our friends, it should be the most comprehensive and complete war preparation warehouse , This warehouse also accompanied me through the most difficult journey , I hope it can help you ! Everything should be done as soon as possible , Especially in the technology industry , We must improve our technical skills . I hope that's helpful ….

边栏推荐
- Opencv learn read images videos and webcams
- Drools (4): drools basic syntax (2)
- 【一库】妙啊!这个库组织npm脚本简直爆炸!
- Precious metal knowledge: lethal short-term secret script
- NiO implementation
- Question: can't download sh shellcheck Please install it manually and some commands of shell script
- Curl post request on the server, using postman tool for parameter conversion
- How to expand and repartition the C disk?
- Opencv learning warp Perspective
- Opengauss simple version installation error
猜你喜欢

Wechat applet - from entry to penetration

Drools (3): drools basic syntax (1)

IDEA——使用@Slf4j打印日志

Opencv learning warp Perspective

Screen: frame paste, 0 fit, full fit

404 page best practices to improve user experience

Curl post request on the server, using postman tool for parameter conversion

Drools (2): drools quick start

RGB-T追踪——【数据集基准】GTOT / RGBT210 / RGBT234 / VOT-2019-2020 / LasHeR / VTUAV

常用的cmd指令
随机推荐
Drools (4): drools basic syntax (2)
[QT] how to obtain the number of rows and columns of qtableview and qtablewidget
Common programming shortcut keys of idea (take off after learning the operation)
Do you know what "parts" MySQL contains?
Precious metal knowledge: lethal short-term secret script
Flame diagram analysis Flink backpressure
Redis migrate tool migration error.
unity3d-对象池的用法
[yiku] wonderful! This library organization NPM script is simply explosive!
MySQL intent lock
Become an Apache contributor, so easy!
Opencv learn resize and crop
Apache dolphin scheduler & tidb joint meetup | focus on application development capabilities under the development of open source ecosystem
火焰图分析Flink反压
Countdown 2 days! Based on the cross development practice of Apache dolphin scheduler & tidb, you can greatly improve your efficiency from writing to scheduling
Common CMD instructions
Opencv learning basic functions
Heap parsing and heap sorting
金融任务实例实时、离线跑批Apache DolphinScheduler在新网银行的三大场景与五大优化
达人专栏 | 还不会用 Apache Dolphinscheduler?大佬用时一个月写出的最全入门教程【三】