2018-06-14   17:00:13

环境准备:

- Python 3.7

- requests库

- xlrd

1、创建Excel文件

2、读取Excel文件

  1. import xlrd
  2.  
  3. class readExcel(object):
  4. def __init__(self, path):
  5. self.path = path
  6.  
  7. @property
  8. def getSheet(self):
  9. # 获取索引
  10. xl = xlrd.open_workbook(self.path)
  11. sheet = xl.sheet_by_index(1)
  12. # print( xl.sheet_names() ) 打印所有sheet名字
  13. # print (sheet.cell_value( 2, 3 )) 打印第3行第4列
  14. return sheet
  15.  
  16. @property
  17. def getRows(self):
  18. # 获取行数
  19. row = self.getSheet.nrows
  20. return row
  21.  
  22. @property
  23. def getCol(self):
  24. # 获取列数
  25. col = self.getSheet.ncols
  26. return col
  27.  
  28. # 以下是分别获取每一列的数值
  29.  
  30. @property
  31. def getId(self):
  32. TestId = []
  33. for i in range( 1, self.getRows ):
  34. TestId.append( self.getSheet.cell_value( i, 0 ) )
  35. # print(TestName)
  36. return TestId
  37.  
  38. @property
  39. def getName(self):
  40. TestName = []
  41. for i in range(1, self.getRows):
  42. TestName.append(self.getSheet.cell_value(i, 1))
  43. # print(TestName)
  44. return TestName
  45.  
  46. @property
  47. def getData(self):
  48. TestData = []
  49. for i in range(1, self.getRows):
  50. TestData.append(self.getSheet.cell_value(i, 2))
  51. return TestData
  52.  
  53. @property
  54. def getUrl(self):
  55. TestUrl = []
  56. for i in range(1, self.getRows):
  57. TestUrl.append(self.getSheet.cell_value(i, 3))
  58. return TestUrl
  59.  
  60. @property
  61. def getMethod(self):
  62. TestMethod = []
  63. for i in range(1, self.getRows):
  64. TestMethod.append(self.getSheet.cell_value(i, 4))
  65. return TestMethod
  66.  
  67. @property
  68. def getStatusCode(self):
  69. TestUid = []
  70. for i in range(1, self.getRows):
  71. TestUid.append(self.getSheet.cell_value(i, 5))
  72. return TestUid
  73.  
  74. @property
  75. def getCode(self):
  76. TestCode = []
  77. for i in range(1, self.getRows):
  78. TestCode.append(self.getSheet.cell_value(i, 6))
  79. return TestCode

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

  1. import requests
  2. import json
  3. from baseData import readExcel
  4. from common import keywords
  5.  
  6. class testApi(object):
  7. def __init__(self, method, url, data):
  8. self.method = method
  9. self.url = url
  10. self.data = data
  11.  
  12. @property
  13. def headers(self):
  14. headers = {
  15. "Content-Type": "application/json"
  16. }
  17. return headers
  18.  
  19. @property
  20. def testApi(self):
  21. # 根据不同的访问方式来访问接口
  22. try:
  23. if self.method == 'post':
  24. r = requests.post(self.url, data=json.dumps(eval(self.data)), headers=self.headers)
  25. elif self.method == 'get':
  26. r = requests.get(self.url, params=self.data)
  27. return r
  28. except:
  29. print('失败')
  30.  
  31. def getCode(self):
  32. # 获取访问接口的状态码
  33. code = self.testApi.json()['code']
  34. return code
  35.  
  36. def getStatusCode(self):
  37. # 获取返回信息status_code
  38. status_code = self.testApi.json()['status_code']
  39. return status_code
  40.  
  41. def getJson(self):
  42. # 获取返回信息的json数据
  43. json_data = self.testApi.json()
  44. return json_data

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

  1. from baseData import readExcel
  2. from testApiWay import testApi
  3. from base_test import baseTest
  4. import unittest
  5. from HTMLTestRunner import HTMLTestRunner
  6. from common import keywords #一些参数封装成了关键字
  7.  
  8. class testLoginApi( baseTest ):
  9. def testLoginApi(self):
  10. '''测试登陆接口,登陆的几种情况。'''
  11. excel = readExcel( r'C:\Users\Jasmine\Desktop\data.xlsx' )
  12. name = excel.getName
  13. data = excel.getData
  14. url = excel.getUrl
  15. method = excel.getMethod
  16. id = excel.getId
  17. status_code = excel.getStatusCode
  18. code = excel.getCode
  19. row = excel.getRows
  20. # print(code)
  21. for i in range( 0, row - 1 ):
  22. api = testApi( method[i], url[i], data[i])
  23. # apicode = api.getCode()
  24. # print(apicode)
  25. apistatus = api.getStatusCode()
  26. print(apistatus)
  27. apijson = api.getJson()
  28. if apistatus == status_code[i]:
  29. print('{}.{}:测试成功。json数据为:{}'.format( id[i], name[i], apijson )) #i+1
  30. else:
  31. print('{}.{}:测试失败。json数据为:{}'.format( id[i], name[i], apijson ))
  32. # 生成测试报告
  33. def runAutomation():
  34. suite = unittest.TestLoader().loadTestsFromTestCase( testLoginApi )
  35. runner = HTMLTestRunner(
  36. stream=open(keywords.Time+ 'testReport.html', 'wb' ), #k.getNowTime()
  37. title=u'TestReport',
  38. description=u'测试报告详细信息'
  39. )
  40. runner.run( suite )
  41.  
  42. if __name__ == '__main__':
  43. # runAutomation()
  44.  
  45. 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. Some notes of An Insider's Guide to TOEFL iBT

    尽早把托福这个坑填上方是正道,在正式上托福课之前阅读了这本Guide,颇受启发——只要是考试,总是有固定的方法的= = An Insider's Guide to TOEFL iBT It is NO ...

  2. plsql之导入数据乱码

     问题现象: 首先是使用了plsql 8.0版本客户端导入 ANSI as UTF-8 的字符集 格式sql 文件进行数据的导入, 然后检查了所有的系统环境的字符集和plsql 的字符集 都OK 的但 ...

  3. 交互式数据可视化-D3.js(三)比例尺

    线性比例尺 线性比例尺是常用比例尺常用方法有: var linear = d3.scaleLinear() - 创建一个定量的线性比例尺. linear.domain([numbers]) - 定义或 ...

  4. 简单使用logback日志框架

    logback_config.xml 配置文件信息 <?xml version="1.0" encoding="UTF-8"?> <confi ...

  5. Django 文件配置、pycharm及django连接数据库、表的增删改查 总结

    静态文件配置 1.你在浏览器中输入网址能够有响应的资源返回给你 是因为后端已经提前给你开设该资源的接口,也就意味着你所能 访问到的资源 都是人家事先定义好的 2.django如何给用户开设资源接口呢? ...

  6. 前端接受base64的excel,点击立即下载

    由于使用框架的特殊性,后台返回的文件流进行了base64加密,所以返回的是base64格式,所以需要将其转变为blob,上代码

  7. LAMP 搭建,wordpress.xcache,powerdns及poweradmin

    一,概念 CGI: CGI全称是通用网关接口(Common Gateway Interface),是外部应用程序与与服务器之间的接口标准,是在CGI程序和web服务器之间传递信息的规程 CGI是一段程 ...

  8. C# AVI Image 转换

    AVI视频库 http://download.csdn.net/download/qc_id_01/9970151 Avi视频文件的编码有很多,这个库只支持部分Avi文件,有些Avi文件不支持,具体哪 ...

  9. 51 Nod 1085 01背包问题

    1085 背包问题  基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 在N件物品取出若干件放在容量为W的背包里,每件物品的体积为W1,W2--Wn(Wi为 ...

  10. 计蒜客 window画图

    在 Windows 的"画图"工具里,可以绘制各种各样的图案.可以把画图当做一个标准的二维平面,在其上先后绘制了 nn 条颜色互不相同的线段. 输出格式 输出 qq 行,每行一个整 ...