2018-06-14   17:00:13

环境准备:

- Python 3.7

- requests库

- xlrd

1、创建Excel文件

2、读取Excel文件

import xlrd

class readExcel(object):
def __init__(self, path):
self.path = path @property
def getSheet(self):
# 获取索引
xl = xlrd.open_workbook(self.path)
sheet = xl.sheet_by_index(1)
# print( xl.sheet_names() ) 打印所有sheet名字
# print (sheet.cell_value( 2, 3 )) 打印第3行第4列
return sheet @property
def getRows(self):
# 获取行数
row = self.getSheet.nrows
return row @property
def getCol(self):
# 获取列数
col = self.getSheet.ncols
return col # 以下是分别获取每一列的数值 @property
def getId(self):
TestId = []
for i in range( 1, self.getRows ):
TestId.append( self.getSheet.cell_value( i, 0 ) )
# print(TestName)
return TestId @property
def getName(self):
TestName = []
for i in range(1, self.getRows):
TestName.append(self.getSheet.cell_value(i, 1))
# print(TestName)
return TestName @property
def getData(self):
TestData = []
for i in range(1, self.getRows):
TestData.append(self.getSheet.cell_value(i, 2))
return TestData @property
def getUrl(self):
TestUrl = []
for i in range(1, self.getRows):
TestUrl.append(self.getSheet.cell_value(i, 3))
return TestUrl @property
def getMethod(self):
TestMethod = []
for i in range(1, self.getRows):
TestMethod.append(self.getSheet.cell_value(i, 4))
return TestMethod @property
def getStatusCode(self):
TestUid = []
for i in range(1, self.getRows):
TestUid.append(self.getSheet.cell_value(i, 5))
return TestUid @property
def getCode(self):
TestCode = []
for i in range(1, self.getRows):
TestCode.append(self.getSheet.cell_value(i, 6))
return TestCode

3、封装请求类型与返回的数据,此处只封装了get和post请求,还有delete、put、options、head等,有兴趣的可以自行添加

import requests
import json
from baseData import readExcel
from common import keywords class testApi(object):
def __init__(self, method, url, data):
self.method = method
self.url = url
self.data = data @property
def headers(self):
headers = {
"Content-Type": "application/json"
}
return headers @property
def testApi(self):
# 根据不同的访问方式来访问接口
try:
if self.method == 'post':
r = requests.post(self.url, data=json.dumps(eval(self.data)), headers=self.headers)
elif self.method == 'get':
r = requests.get(self.url, params=self.data)
return r
except:
print('失败') def getCode(self):
# 获取访问接口的状态码
code = self.testApi.json()['code']
return code def getStatusCode(self):
# 获取返回信息status_code
status_code = self.testApi.json()['status_code']
return status_code def getJson(self):
# 获取返回信息的json数据
json_data = self.testApi.json()
return json_data

4、通过unittest执行测试用例,用HTMLTestRunner生成测试报告

from baseData import readExcel
from testApiWay import testApi
from base_test import baseTest
import unittest
from HTMLTestRunner import HTMLTestRunner
from common import keywords #一些参数封装成了关键字 class testLoginApi( baseTest ):
def testLoginApi(self):
'''测试登陆接口,登陆的几种情况。'''
excel = readExcel( r'C:\Users\Jasmine\Desktop\data.xlsx' )
name = excel.getName
data = excel.getData
url = excel.getUrl
method = excel.getMethod
id = excel.getId
status_code = excel.getStatusCode
code = excel.getCode
row = excel.getRows
# print(code)
for i in range( 0, row - 1 ):
api = testApi( method[i], url[i], data[i])
# apicode = api.getCode()
# print(apicode)
apistatus = api.getStatusCode()
print(apistatus)
apijson = api.getJson()
if apistatus == status_code[i]:
print('{}.{}:测试成功。json数据为:{}'.format( id[i], name[i], apijson )) #i+1
else:
print('{}.{}:测试失败。json数据为:{}'.format( id[i], name[i], apijson ))
# 生成测试报告
def runAutomation():
suite = unittest.TestLoader().loadTestsFromTestCase( testLoginApi )
runner = HTMLTestRunner(
stream=open(keywords.Time+ 'testReport.html', 'wb' ), #k.getNowTime()
title=u'TestReport',
description=u'测试报告详细信息'
)
runner.run( suite ) if __name__ == '__main__':
# runAutomation() unittest.main( verbosity=2 )

ps:Python3中HTMLTestRunner生成的测试报告很简陋,需要自己手动修改HTMLTestRunner.py文件,由于我是初学者,所以还在研究用其他方法生成测试报告

Python+requests+excel接口测试的更多相关文章

  1. python+requests+excel 接口测试

    1.EXCEL文件接口保存方式,如图. 2.然后就是读取EXCEL文件中的数据方法,如下: import xlrd class readExcel(object): def __init__(self ...

  2. python+requests+excel+unittest+ddt接口自动化数据驱动并生成html报告(二)

    可以参考 python+requests接口自动化完整项目设计源码(一)https://www.cnblogs.com/111testing/p/9612671.html 原文地址https://ww ...

  3. python+requests+json 接口测试思路示例

    实际项目中用python脚本实现接口测试的步骤: 1 发送请求,获取响应  >>2 提取响应里的数据,对数据进行必要的处理  >>3 断言响应数据是否与预期一致 以豆瓣接口为例 ...

  4. python+requests+excel+unittest+ddt接口自动化数据驱动并生成html报告

    1.环境准备: python3.6 requests xlrd openpyxl HTMLTestRunner_api 2.目前实现的功能: 封装requests请求方法 在excel填写接口请求参数 ...

  5. python+requests+excel+unittest+ddt接口自动化数据驱动并生成html报告(已弃用)

    前言 1.环境准备: python3.6 requests xlrd openpyxl HTMLTestRunner_api 2.目前实现的功能: 封装requests请求方法 在excel填写接口请 ...

  6. python+requests实现接口测试 - get与post请求使用(转载)

    转自:http://www.cnblogs.com/nizhihong/p/6567928.html 简介:Requests 是用Python语言编写,基于 urllib,采用 Apache2 Lic ...

  7. python+requests实现接口测试 - cookies的使用

    在很多时候,发送请求后,服务端会对发送请求方进行身份识别,如果请求中缺少识别信息或存在错误的识别信息, 会造成识别失败. 如一些需要用户登录以后才能访问的页面. import requests mya ...

  8. python+requests实现接口测试 - get与post请求使用

    简介:Requests 是用Python语言编写,基于 urllib,采用 Apache2 Licensed 开源协议的 HTTP 库.它比 urllib 更加方便,可以节约我们大量的工作,完全满足 ...

  9. python+requests之接口测试

    最近学习接口测试,测试工具玩的差不多了,想用代码来尝试一下. 发现一个简单的库,requests 一:安装 pip install requests 二:使用 import requests url ...

随机推荐

  1. Python内部执行过程

    一.编译过程概述 当我们执行Python代码的时候,在Python解释器用四个过程“拆解”我们的代码,最终被CPU执行返回给用户. 首先当用户键入代码交给Python处理的时候会先进行词法分析,例如用 ...

  2. UVA - 1649 Binomial coefficients (组合数+二分)

    题意:求使得C(n,k)=m的所有的n,k 根据杨辉三角可以看出,当k固定时,C(n,k)是相对于n递增的:当n固定且k<=n/2时,C(n,k)是相对于k递增的,因此可以枚举其中的一个,然后二 ...

  3. web前端:上传文件夹(需支持多浏览器)

    在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传和下载功能的实现. 先说下要求: PC端全平台支持,要求支持Windows,Mac,Linux 支持所 ...

  4. CF1043F Make It One 容斥+dp+组合

    考试的时候考的一道题,感觉挺神的. 我们发现将所有数去重后最多只会选不到 $7$ 后 $gcd$ 就会变成 $1$. 令 $f[i][k]$ 表示选 $i$ 个数后 $gcd$ 为 $k$ 的方案数. ...

  5. 分块查找(Blocking Search)

    1.定义 分块查找(Blocking Search)又称索引顺序查找.它是一种性能介于顺序查找和二分查找之间的查找方法. 2.基本思想 分块查找的基本思想是: (1)首先查找索引表 索引表是有序表,可 ...

  6. 小程序上传wx.uploadFile - 小程序请假-请求

    小程序上传wx.uploadFile UploadTask wx.uploadFile(Object object) 将本地资源上传到服务器.客户端发起一个 HTTPS POST 请求,其中 cont ...

  7. redis安装成功后get: command not found

    安装redis后客户端无法使用,即redis-cli执行后报找不到的错误. 这主要是安装redis的时候没有把客户端装上,在StackOverFlow上找到了一种只安装redis cli的方法. 安装 ...

  8. 兼容pc端和移动端的轮播图插件 swiper.js

    swiper.js是一款纯JavaScript打造的滑动特效插件,可以用来实现检点轮播图,tab触摸滑动切换等常用效果.下载地址:https://www.swiper.com.cn/download/ ...

  9. nginx负载均衡 理解与测试

    Nginx负载均衡概述 Web服务器,直接面向用户,往往要承载大量并发请求,单台服务器难以负荷,我使用多台WEB服务器组成集群,前端使用Nginx负载均衡,将请求分散的打到我们的后端服务器集群中,实现 ...

  10. 三、smarty--变量调节器(修改器)

    变量调节器(修改器) 作用: 1.  从PHP中分配个模板的变量 2.  需要模板中对变量在输出前进行处理 3.  处理方式就是使用“函数” 4.  在smarty3中可以直接调用到PHP的函数 5. ...