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. 【转载】C++ 11中的右值引用

    本篇随笔为转载,原博地址如下:http://www.cnblogs.com/TianFang/archive/2013/01/26/2878356.html 右值引用的功能 首先,我并不介绍什么是右值 ...

  2. ecshop新版不能在模板文件.dwt和.lbi中直接添加php代码的解决方法

    ecshop新版不能在模板文件.dwt和.lbi中直接添加php代码了,为什么呢? 因为直接在模板中加入php函数和代码,没有经过过滤,容易造成安全隐患.程序源码安全是非常重要的. 不过如果有朋友希望 ...

  3. java8学习之Stream源码分析

    上一次已经将Collectors类中的各种系统收集器的源代码进行了完整的学习,而在之前咱们已经花了大量的篇幅对其Stream进行了详细的示例学习,如: 那接下来则通过源代码的角度来对Stream的运作 ...

  4. JavaScript中数组元素删除的七大方法汇总

    原文链接:https://blog.csdn.net/u010323023/article/details/52700770 在JavaScript中,除了Object之外,Array类型恐怕就是最常 ...

  5. springmvc其他类获取request记得web.xml

    <listener> <listener-class>org.springframework.web.context.request.RequestContextListene ...

  6. 使用Vue自定义组件时,报did you register the component correctly? For recursive components, make sure to provide the "name" option.(未注册组件)的原因之一

    错误信息: [Vue warn]: Unknown custom element: <list> - did you register the component correctly? F ...

  7. 什么?studio3T试用期到了,还没有破解的办法?试制基于python的mongodb CRUD平台

    首先,安装python支持的mongodb库pip install pymongo from pymongo import MongoClient client = MongoClient('loca ...

  8. Acwing-98-分形之城(递推,数学)

    链接: https://www.acwing.com/problem/content/description/100/ 题意: 城市的规划在城市建设中是个大问题. 不幸的是,很多城市在开始建设的时候并 ...

  9. JAVA笔记6-继承和权限控制

    1. (1)类的成员的权限修饰符有public,protected,private或default,限定其他对象对该类对象成员的访问权限. (2)class的权限修饰符只可以是public或defau ...

  10. vue 绑定class、v-bind:style(对象语法、数组语法)

    绑定 HTML Class 我们可以传给 v-bind:class 一个对象,以动态地切换 class: 内联样式在模板里 <div id="div1" :class=&qu ...