一、介绍
requests是一个很实用的python http客户端库,编写爬虫和测试服务器响应数据时经常会用到,requests是python语言的第三方的库,专门用于发送http请求
二、前提
1
|
pip install requests
|
三、get的请求
3.1 get无参请求
1
|
r = requests.get( 'http://www.baidu.com' )
|
3.2 get传参
1
2
|
payload = { 'key1' : 'value1' , 'key2' : 'value2' , 'key3' : none}
r = requests.get( 'http://www.baidu.com ' , params = payload)
|
案例:测试聚合数据
代码
1
2
3
4
5
6
7
8
9
10
11
|
import requests
class userequestclass():
#get传参的第一种方式
def xwttmethod( self ):
r = requests.get( "http://v.juhe.cn/toutiao/index?type=guonei&key=4b72107de3a197b3bafd9adacf685790" )
print (r.text)
#get传参的第二种方式
def xwttmethod( self ):
params = { "type" : "guonei" , "key" : "4b72107de3a197b3bafd9adacf685790" }
r = requests.get( "http://v.juhe.cn/toutiao/index" ,params = params)
print (r.text)
|
四、post请求
类似python中的表单提交
1
2
|
payload = { 'key1' : 'value1' , 'key2' : 'value2' }
r = requests.post( "http://httpbin.org/post" , data = payload)
|
案例:测试聚合数据
代码
1
2
3
4
5
6
7
|
import requests
class userequestclass():
def xwttpostmethod( self ):
params = { "type" : "guonei" , "key" : "4b72107de3a197b3bafd9adacf685790" }
r = requests.post( "http://v.juhe.cn/toutiao/index" ,params = params)
#print(r.status_code)
return r.status_code
|
五、requests响应
1
2
3
4
5
6
|
r.status_code 响应状态码
r.heards 响应头
r.cookies 响应cookies
r.text 响应文本
r. encoding 当前编码
r. content 以字节形式(二进制)返回
|
最常用的是根据响应状态码判断接口是否连通,经常用于做接口中断言判断
六、request扩充
1
2
3
4
5
6
7
8
|
1. 添加等待时间
requests.get(url,timeout = 1 ) #超过等待时间则报错
2. 添加请求头信息
requests.get(url,headers = headers) #设置请求头
3. 添加文件
requests.post(url, files = files) #添加文件
|
文件传输
1
2
3
|
url = 'http://httpbin.org/post'
files = { 'file' : open ( 'report.xls' , 'rb' )}
r = requests.post(url, files = files)
|
七、requests+pytest+allure
7.1 流程如下
读取文件中的数据
requests拿到数据请求接口返回状态码
通过断言验证返回状态码和200对比
生成allure的测试报告
7.2 模块总览
1
2
3
|
datademo(存放数据)>> readdemo(读取数据)
userequests(发送请求)>>testdemo(生成报告)
|
7.3 读取csv文件流程
7.3.1 存储数据(csv)
通过excel另存为csv即可。
7.3.2 读取数据(readdemo)
代码展示
1
2
3
4
5
6
7
8
9
|
import csv
class readcsv():
def readcsv( self ):
item = []
rr = csv.reader( open ( "../datademo/123.csv" ))
for csv_i in rr:
item.append(csv_i)
item = item [ 1 :]
return item
|
7.3.3 request请求接口返回状态码
代码展示
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
import requests
from readdatademo.readcsv import readcsv
r = readcsv()
ee = r.readcsv()
# print(ee)
class requestcsv():
def requestscsv( self ):
item = []
for csv_i in ee:
if csv_i[ 2 ] = = "get" :
rr = requests.get(csv_i[ 0 ],params = csv_i[ 1 ])
item.append(rr.status_code)
else :
rr = requests.post(csv_i[ 0 ],data = csv_i[ 1 ])
item.append(rr.status_code)
return item
|
7.3.4 pytest断言设置并结合allure生成测试报告
代码展示
1
2
3
4
5
6
7
8
9
10
11
12
13
|
import pytest,os,allure
from userequests.userequestsdemo.requestscsv import requestcsv
r = requestcsv()
ee = r.requestscsv()
print (ee)
class testclass02():
def test001( self ):
for code in ee:
assert code = = 200
if __name__ = = '__main__' :
pytest.main([ '--alluredir' , 'report/result' , 'test_02csv.py' ])
split = 'allure ' + 'generate ' + './report/result ' + '-o ' + './report/html ' + '--clean'
os.system(split)
|
7.3.5 测试报告展示
7.4 读取excle文件流程
7.4.1 存储数据(xlsx)
7.4.2 读取数据(readdemo)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
from openpyxl import load_workbook
class readxcel():
def gettestexcel( self ):
# 打开表
workbook = load_workbook( "g:\python\pythonproject\pytest05a\\requestdemo\\a.xlsx" )
# 定位表单
sheet = workbook[ 'sheet1' ]
print (sheet.max_row) # 3 行
print (sheet.max_column) # 3 列
test_data = [] # 把所有行的数据放到列表中
for i in range ( 2 , sheet.max_row + 1 ):
sub_data = {} # 把每行的数据放到字典中
for j in range ( 1 , sheet.max_column + 1 ):
sub_data[sheet.cell( 1 , j).value] = sheet.cell(i, j).value
test_data.append(sub_data) # 拼接每行单元格的数据
return test_data
t = readxcel()
f = t.gettestexcel()
print (f)
|
7.4.3 request请求接口返回状态码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
import requests
from requestdemo.readexcel import readxcel
class getstatuscode():
def getstatuscode( self ):
t = readxcel()
f = t.gettestexcel()
item = []
for excel_i in f:
if excel_i[ "method" ] = = "get" :
rr = requests.get(excel_i[ "url" ], params = excel_i[ "params" ])
item.append(rr.status_code)
else :
rr = requests.post(excel_i[ "url" ], data = excel_i[ "params" ])
item.append(rr.status_code)
return item
print (getstatuscode().getstatuscode())
|
7.4.4 pytest断言设置并结合allure生成测试报告
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
import allure, pytest, os
from requestdemo.getstatuscode import getstatuscode
get = getstatuscode()
statuscodes = get.getstatuscode()
class testreadexcel():
def testreadexcel( self ):
for code in statuscodes:
assert code = = 200
if __name__ = = "__main__" :
# 生成测试报告json
pytest.main([ "-s" , "-q" , '--alluredir' , 'report/result' , 'testreadexcel.py' ])
# 将测试报告转为html格式
split = 'allure ' + 'generate ' + './report/result ' + '-o ' + './report/html ' + '--clean'
os.system(split)
|
7.4.5:测试报告展示
到此这篇关于python中requests做接口测试的方法的文章就介绍到这了,更多相关python requests接口测试内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/weixin_50829653/article/details/117374597