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. eclipse+自己安装的maven不能run as 找不到包

    我本地环境eclipse自带maven但是默认指定的路径是c盘下,本着不想放c盘,就自己下了maven包集成到eclipse中,但是java类中main方法如果调用了maven中的包是找不到的.后类自 ...

  2. python3之selenium.webdriver 库练习自动化谷歌浏览器打开百度自动百度关键字

    import os,time,threading from selenium import webdriver from selenium.webdriver.common.keys import K ...

  3. XXL-JOB原理--任务调度中心任务管理

    XXL-JOB原理--任务调度中心任务管理 https://blog.csdn.net/qq924862077/article/details/82713758

  4. CAP 与 注册中心

    https://blog.csdn.net/fly910905/article/details/100023415 http://www.ruanyifeng.com/blog/2018/07/cap ...

  5. hive判断数据一个表是否在另一个表中(二)

    1.一个表中的数据不存在另一个表中 2.一个表中的数据 存在另一个表中:

  6. 直通BAT必考题系列:深入详解JVM内存模型与JVM参数详细配置

    VM基本是BAT面试必考的内容,今天我们先从JVM内存模型开启详解整个JVM系列,希望看完整个系列后,可以轻松通过BAT关于JVM的考核. BAT必考JVM系列专题 1.JVM内存模型 2.JVM垃圾 ...

  7. DEDE怎么让栏目列表以权重拍排列

    方法/步骤     进入后台到栏目里,新发布文章也行,编辑文章也行,你会看到有个权重值,不管你怎么改变保存后依然没有变化.   修改权重值,打开dede\album_edit.php文件,找到 UPD ...

  8. 创建本地repo源

    1,保留rpm包 yum 安装时保留包至指定目录 编辑/etc/yum.conf 将keepcache的值设置为1: 2,使用插件 1,yum-plugin-downloadonly插件 sudo y ...

  9. nginx命令和配置

    centos 6.8安装的nginx 1.12.2 1.nginx常用的命令 使用nginx命令前,进入到/usr/local/nginx/sbin/目录 1)查看nginx版本 进入到/usr/lo ...

  10. Mybatis笔记总结

    第一.Mybatis介绍 MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改 ...