准备先利用之前整理的python自带的unittest框架

整合excel 实现接口自动化测试功能

先看看excel表格设置:

下来是对excel获取的代码:

 #!/usr/bin/env python
# -*- coding: utf_8 -*- import xlrd
import json class Create_excel:
def open_excel(self,path):
workbook = xlrd.open_workbook(path)
table = workbook.sheets()[0]
return table
#获取sheet def get_nrows(self,table):
nrows = table.nrows
return nrows
#获取行号 def testname(self,table,nrows):
TestName = []
for i in range(1,nrows):
TestName.append(table.cell(i,0).value)
return TestName
#获取用例name def testdata(self,table,nrows):
TestData = []
for i in range(1,nrows):
data = json.loads(table.cell(i,1).value)
TestData.append(data)
return TestData
#获取data接口参数 def testurl(self,table,nrows):
TestUrl = []
for i in range(1,nrows):
TestUrl.append(table.cell(i,2).value)
return TestUrl
#获取接口测试url def testpattern(self,table,nrows):
TestPattern = []
for i in range(1,nrows):
TestPattern.append(table.cell(i,3).value)
return TestPattern
#获取接口期望响应结果 def testreport(self,table,nrows):
TestReport = []
for i in range(1,nrows):
TestReport.append(table.cell(i,4).value)
return TestReport
#获取用例期望的运行结果 if __name__ == "__main__":
Create_excel()

之后是unittest框架

 #!/usr/bin/env python
# -*- coding: utf_8 -*- import requests
import re
import unittest
from myexcel import Create_excel class Testapi():
def testapi(self,url,data):
results = requests.post(url,data)
return results class Testcase(unittest.TestCase):
def setUp(self):
print "接口测试开始" def tearDown(self):
print "接口测试结束" def test_post(self):
global report
api = Testapi()
excel = Create_excel()
testpath = "testcase.xls"
testtable = excel.open_excel(testpath)
testnrows = excel.get_nrows(testtable)
for i in range(0,testnrows-1):
testname = excel.testname(testtable,testnrows)[i]
testdata = excel.testdata(testtable,testnrows)[i]
testurl = excel.testurl(testtable,testnrows)[i]
testpattern = excel.testpattern(testtable,testnrows)[i]
testreport = excel.testreport(testtable,testnrows)[i]
testresults = api.testapi(testurl,testdata)
pattern = re.compile(testpattern)
match = pattern.search(testresults.url)
try:
if testresults.status_code == 200:
if match.group() == testpattern:
report = "pass"
else:
print "测试请求失败"
except AttributeError:
report = "no"
if report == testreport:
print "用例名称:",testname,"测试结果:测试通过"
else:
print "用例名称:",testname,"测试结果:测试失败" if __name__ == "__main__":
unittest.main()

利用循环执行所有用例

现在只要在excel里添加接口测试用例

运行脚本 即可

python学习笔记(excel+unittest)的更多相关文章

  1. python学习笔记之——unittest框架

    unittest是python自带的单元测试框架,尽管其主要是为单元测试服务的,但我们也可以用它来做UI自动化测试和接口的自动化测试. unittest框架为我们编写用例提供了如下的能力 定义用例的能 ...

  2. python学习笔记(unittest)

    刚刚放假回来我想很多人都还没有缓过来吧 这次介绍一个python自带的测试框架 unitest #!/usr/bin/env python # -*- coding: utf_8 -*- import ...

  3. python学习笔记:unittest单元测试

    单元测试:开发自测时写的代码 unittest基本原理: ♦整个平台的搭建使用的是python的unittest测试框架,这里简单介绍下unittest模块的简单应用. ♦unittest是pytho ...

  4. Python学习笔记-EXCEL操作

    环境Python3 创建EXCEL,覆盖性创建 #conding=utf-8 import xlwt def BuildExcel(ExcelName,SheetName,TitleList,Data ...

  5. Python学习笔记之unittest测试类

    11-3 雇员:编写一个名为Employee 的类,其方法__init__()接受名.姓和年薪,并将它们都存储在属性中.编写一个名为give_raise()的方法,它默认将年薪增加5000美元,但也能 ...

  6. Python学习笔记:Unittest框架了解

    Unittest单元测试框架不仅可以适用于单元测试,还可以适用于自动化测试用来的开发与执行,该测试框架可执行测试用例,并提供丰富的断言方法,最终生成测试报告. 一.Unittest常用方法 1.Tes ...

  7. python学习笔记整理——字典

    python学习笔记整理 数据结构--字典 无序的 {键:值} 对集合 用于查询的方法 len(d) Return the number of items in the dictionary d. 返 ...

  8. VS2013中Python学习笔记[Django Web的第一个网页]

    前言 前面我简单介绍了Python的Hello World.看到有人问我搞搞Python的Web,一时兴起,就来试试看. 第一篇 VS2013中Python学习笔记[环境搭建] 简单介绍Python环 ...

  9. python学习笔记之module && package

    个人总结: import module,module就是文件名,导入那个python文件 import package,package就是一个文件夹,导入的文件夹下有一个__init__.py的文件, ...

  10. python学习笔记(六)文件夹遍历,异常处理

    python学习笔记(六) 文件夹遍历 1.递归遍历 import os allfile = [] def dirList(path): filelist = os.listdir(path) for ...

随机推荐

  1. 墨菲定律(Murphy's Law)

    https://baike.baidu.com/item/墨菲定律/746284?fr=aladdin 墨菲定律是一种心理学效应,是由 爱德华·墨菲(Edward A. Murphy)提出的. 主要内 ...

  2. XP系统中IIS访问无法显示网页,目前访问网站的用户过多。终极解决办法

    无法显示网页 目前访问网站的用户过多. -------------------------------------------------------------------------------- ...

  3. [转载]Css设置table网格线(无重复)

    原文地址:Css设置table网格线(无重复)作者:依然贰零零柒 效果图: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0Transition ...

  4. 【JUnit】junit4的几个assert方法

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/zhtao01/article/details/27858225 在静态类junit.framewor ...

  5. 010-shiro与spring web项目整合【四】缓存Ehcache

    一.Ehcache shiro每次授权都会通过realm获取权限信息,为了提高访问速度需要添加缓存,第一次从realm中读取权限数据,之后不再读取,这里Shiro和Ehcache整合. 1.添加Ehc ...

  6. C# 调用win api获取chrome浏览器中地址

    //FindWindow 查找窗口 //FindWindowEx查找子窗口 //EnumWindows列举屏幕上的所有顶层窗口,如果回调函数成功则返回非零,失败则返回零 //GetWindowText ...

  7. MySQL读写分离之amoeba

    MySQL读写分离之amoeba主从复制的搭建环境参考:http://www.cnblogs.com/fansik/p/5270334.htmlamoeba依赖于jdk环境:jdk环境搭建参考:htt ...

  8. Flash本地共享对象 SharedObject

    以下内容是对网上一些资料的总结 Flex SharedObject 介绍(转自http://www.eb163.com/club/thread-3235-1-1.html): Flash的本地共享对象 ...

  9. VS2015配置安卓Android和iOS开发环境

    http://www.cjjjs.cn/paper/gzsh/627201502818357.aspx [摘要] 本文按照步骤一步步的介绍要下载安装的东西,都提供了下载地址.最后将所有需要的程序都打包 ...

  10. 转:.NET特性与反射

    .NET编译器的任务之一是为所有定义和引用的类型生产元数据描述.除了程序集中标准的元数据外,.NET平台允许程序员使用特性(attribute)把更多的元数据嵌入到程序集中.简而言之,特性就是用于类型 ...