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. detect 导图

    https://www.zhihu.com/question/356551927/answer/919612766 知乎上看到的,不错

  2. local_time

    time_t time(time_t *tloc); 功能:获取纪元1970-01-01 00:00:00以来所经历的秒数 参数: tloc:用来存储返回时间 返回值:成功:返回秒数, 失败:-1 - ...

  3. 第10课:[实战] Redis 网络通信模块源码分析(3)

    redis-server 接收到客户端的第一条命令 redis-cli 给 redis-server 发送的第一条数据是 *1\r\n\$7\r\nCOMMAND\r\n .我们来看下对于这条数据如何 ...

  4. alembic在tornado项目中的应用

    在项目中引用alembic 协助tornado项目生成数据表结构 alembic revision --autogenerate -m "create tables" 第二步执行 ...

  5. 手机端 设置html上font-size的值 使用rem

    在head标签上加入: (function() { var b = navigator.userAgent; ipad = b.match(/(iPad).*OS\s([\d_]+)/) ? true ...

  6. JS实现 Tab栏切换案例

    要求:当鼠标点击上面相应的选项卡(tab),下面页面的内容也随之而改变. 结构分析: 全部的内容都放到一个大的盒子里面,盒子里面又可以分为上面和下面两个盒子. 上面的盒子放了 5个li,装着5个小的选 ...

  7. OpenCV笔记(6)(harris角点检测、背景建模)

    一.Harris角点 如上图所示,红色框AB都是平面,蓝色框CD都是边缘,而绿色框EF就是角点. 平面:框往X或Y抽移动,变化都很小. 边缘:框沿X或Y轴移动,其中一个变化很小,而另外一个变化比较大. ...

  8. 什么是ASCII码?

    ㈠定义 ASCII ((American Standard Code for Information Interchange): 美国信息交换标准代码)是基于拉丁字母的一套电脑编码系统,主要用于显示现 ...

  9. sh_01_列表基本使用

    sh_01_列表基本使用 name_list = ["zhangsan", "lisi", "wangwu"] # 1. 取值和取索引 # ...

  10. make文件基础用法

    参照:https://www.jianshu.com/p/0b2a7cb9a469 创建工作目录,包含一下文件 main.c person.c b.h c.h /*** c.h ***/ //this ...