当前位置:网站首页>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 ….

边栏推荐
- WPS or office compression of ppt
- RGB-T追踪——【数据集基准】GTOT / RGBT210 / RGBT234 / VOT-2019-2020 / LasHeR / VTUAV
- Business secret series -- Talking about the evaluation of commercial passwords from the perspective of Party A and Party B (I)
- Relevant configurations of pychart: change font style and size, change picture background, and change the font color of console output
- 从XSS Playload 学习浏览器解码
- Hands on practice - teach you how to make an intelligent fish tank with STM32
- Opencv learning color detection
- “蔚来杯“2022牛客暑期多校训练营1补题记录(ACDGIJ)
- To do list application vikunja
- PG operation and maintenance -- logical backup and physical backup practice
猜你喜欢

配置Flask

"Wei Lai Cup" 2022 Niuke summer multi school training camp 1 supplementary question record (acdgij)

Differences in the use of function call pointer parameters *p, * & P
![[yiku] wonderful! This library organization NPM script is simply explosive!](/img/84/e118c11f27aaf26e52059cb7ebc27a.png)
[yiku] wonderful! This library organization NPM script is simply explosive!

Leetcode 1184: distance between bus stops
![Rgb-t tracking - [dataset benchmark] gtot / rgbt210 / rgbt234 / vot-2019-2020 / laser / VTUAV](/img/10/40d02da10a6f6779635dc820c074c6.png)
Rgb-t tracking - [dataset benchmark] gtot / rgbt210 / rgbt234 / vot-2019-2020 / laser / VTUAV

PR字幕制作

Apache DolphinScheduler&TiDB联合Meetup | 聚焦开源生态发展下的应用开发能力

How can man Bang write a new footnote to the value of the industry's first social responsibility report?

Redis migrate tool migration error.
随机推荐
Drools (3): drools basic syntax (1)
PR字幕制作
Drools(2):Drools快速入门
【一库】妙啊!这个库组织npm脚本简直爆炸!
MySQL execution plan
With Huawei cloud welink, you can connect to the world even in the countryside
Learn browser decoding from XSS payload
Yolov5 improvements: add attention mechanism (video tutorial)
unity3d-对象池的用法
Difference between shape and size ()
Become an Apache contributor, so easy!
Manifest merger failed with multiple errors, see logs
Screen: frame paste, 0 fit, full fit
Introduction to C language -- summary table of operator priority sorting
Queue assistant | product update log in June 2022
Image annotation software reference
Apache dolphin scheduler version 3.0.0-beta-1 was released, and flinksql and Zeppelin task types were added
如何对C盘进行扩容重新分区?
20220725 compensator in automatic control principle
Leetcode 1184: distance between bus stops