1.简介
requests是一个很实用的Python HTTP客户端库,编写爬虫和测试服务器响应数据时经常会用到,Requests是Python语言的第三方的库,专门用于发送HTTP请求
2.下载
pip install requests
3.引用
import requests
4..请求方式get传参
无参数
r = requests.get('http://www.baidu.com')
有参数
方式1:
r = requests.get(url="url",params="参数")
方式2:
no1="url"
no2="参数"
r=requests.get(no1,params=no2)
5..请求方式post传参
r = requests.get(url="url",data="参数")
6.常见响应
r.status_code 响应状态码
r.heards 响应头
r.cookies 响应cookies
r.text 响应文本
r.json() 响应的字典类型数据
r. encoding 当前编码
r. content 以字节形式(二进制)返回
1:添加等待时间
requests.get(url,timeout=1) #超过等待时间则报错
2:添加请求头信息
requests.get(url,headers=headers) #设置请求头
3:添加文件
requests.post(url, files=files) #添加文件
7.接口测试流程
读取文件中的数据
requests拿到数据请求接口返回状态码
通过断言验证返回的数据和预期结果
生成allure的测试报告
dataDemo(存放数据)>> readDemo(读取数据)>> restDemo(发送请求生成报告)
数据建立
csv
elsx
读取csv文件流程
8.进行单元测试
csv
xlsx
打印报告
csv
xlsx
代码取用
csv
import csv
class ReadCsv():
def readcsv(self):
li=[]
rr=csv.reader(open("../datatest0826/testdata0826.csv"))
li=[i for i in rr]
li=li[1::]
return li
r=ReadCsv()
print(r.readcsv())
import requests,unittest
from readdata.reaffatacsv import ReadCsv
rc=ReadCsv()
li=rc.readcsv()
class ApiClassCsv(unittest.TestCase):
def test01(self):
for i in li:
cs=eval(i[1])
if i[2]=="get":
objs=requests.get(url=i[0],params=cs)
self.assertEqual(objs.json()['error_code'],int(i[3]))
else:
objs=requests.post(url=i[0],data=cs)
self.assertEqual(objs.json()['error_code'],int(i[3]))
if __name__ == '__main__':
unittest.main()
from common.HTMLTestRunner import HTMLTestRunner
from requesttest0826.requestunintestcsv import ApiClassCsv
import unittest
class Htmlclass():
def htmlmethod(self):
suite=unittest.TestSuite()
lis=['test01']
for i in lis:
suite.addTest(ApiClassCsv(i))
with open('../reports1.html','wb')as s:
HTMLTestRunner(
stream=s,
title="接口01",
description='一次',
verbosity=2
).run(suite)
hc=Htmlclass()
hc.htmlmethod()
xlsx
from openpyxl import load_workbook
class ReadXlsl():
def readxlsl(self):
getxlsx=load_workbook("../datatest0826/testdata0826.xlsx")
Sheet=getxlsx['Sheet1']
xlsldata=[]
for i in range(2,Sheet.max_row+1):
getdata={}
for j in range(1,Sheet.max_column+1):
getdata[Sheet.cell(1, j).value] = Sheet.cell(i, j).value
xlsldata.append(getdata)
return xlsldata
import requests,unittest
from readdata.readdataxlsx import ReadXlsl
rc=ReadXlsl()
li=rc.readxlsl()
class ApiClassXlsx(unittest.TestCase):
def test01(self):
for i in li:
cs=eval(i['params'])
if i['method'] == "get":
objs = requests.get(url=i['url'], params=cs)
self.assertEqual(objs.json()['error_code'], int(i['dy']))
else:
objs = requests.post(url=i['url'], data=cs)
self.assertEqual(objs.json()['error_code'], int(i['dy']))
if __name__ == '__main__':
unittest.main()
from common.HTMLTestRunner import HTMLTestRunner
from requesttest0826.requestunittestxlsl import ApiClassXlsx
import unittest
class HtmlClass():
def htmlmethod(self):
suite = unittest.TestSuite()
lis = ['test01']
for i in lis:
suite.addTest(ApiClassXlsx(i))
with open('../reports2.html', 'wb') as s:
HTMLTestRunner(
stream=s,
title="接口02",
description='二次',
verbosity=2
).run(suite)
hc = HtmlClass()
hc.htmlmethod()