python学习笔记(excel+unittest)
准备先利用之前整理的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)的更多相关文章
- python学习笔记之——unittest框架
unittest是python自带的单元测试框架,尽管其主要是为单元测试服务的,但我们也可以用它来做UI自动化测试和接口的自动化测试. unittest框架为我们编写用例提供了如下的能力 定义用例的能 ...
- python学习笔记(unittest)
刚刚放假回来我想很多人都还没有缓过来吧 这次介绍一个python自带的测试框架 unitest #!/usr/bin/env python # -*- coding: utf_8 -*- import ...
- python学习笔记:unittest单元测试
单元测试:开发自测时写的代码 unittest基本原理: ♦整个平台的搭建使用的是python的unittest测试框架,这里简单介绍下unittest模块的简单应用. ♦unittest是pytho ...
- Python学习笔记-EXCEL操作
环境Python3 创建EXCEL,覆盖性创建 #conding=utf-8 import xlwt def BuildExcel(ExcelName,SheetName,TitleList,Data ...
- Python学习笔记之unittest测试类
11-3 雇员:编写一个名为Employee 的类,其方法__init__()接受名.姓和年薪,并将它们都存储在属性中.编写一个名为give_raise()的方法,它默认将年薪增加5000美元,但也能 ...
- Python学习笔记:Unittest框架了解
Unittest单元测试框架不仅可以适用于单元测试,还可以适用于自动化测试用来的开发与执行,该测试框架可执行测试用例,并提供丰富的断言方法,最终生成测试报告. 一.Unittest常用方法 1.Tes ...
- python学习笔记整理——字典
python学习笔记整理 数据结构--字典 无序的 {键:值} 对集合 用于查询的方法 len(d) Return the number of items in the dictionary d. 返 ...
- VS2013中Python学习笔记[Django Web的第一个网页]
前言 前面我简单介绍了Python的Hello World.看到有人问我搞搞Python的Web,一时兴起,就来试试看. 第一篇 VS2013中Python学习笔记[环境搭建] 简单介绍Python环 ...
- python学习笔记之module && package
个人总结: import module,module就是文件名,导入那个python文件 import package,package就是一个文件夹,导入的文件夹下有一个__init__.py的文件, ...
- python学习笔记(六)文件夹遍历,异常处理
python学习笔记(六) 文件夹遍历 1.递归遍历 import os allfile = [] def dirList(path): filelist = os.listdir(path) for ...
随机推荐
- Create a Group Policy Central Store
一.How to create a Group Policy Central Store You have downloaded or created your own Group Policy Ad ...
- Python 模块之Logging——常用handlers的使用
一.StreamHandler 流handler——包含在logging模块中的三个handler之一. 能够将日志信息输出到sys.stdout, sys.stderr 或者类文件对象(更确切点,就 ...
- 002-es6字符串扩展
1.字符串扩展 参考地址:http://es6.ruanyifeng.com/#docs/string 1.1.codePointAt() JavaScript 内部,字符以 UTF-16 的格式储存 ...
- R中的data.table 快速上手入门
data.table包提供了一个非常简洁的通用格式:DT[i,j,by]. 可以理解为:对于数据集DT,选取子集行i,通过by分组计算j. 对比与dplyr等包,data.table的运行速度更快. ...
- 转:zero length array问题
单看这文章的标题,你可能会觉得好像没什么意思.你先别下这个结论,相信这篇文章会对你理解C语言有帮助.这篇文章产生的背景是在微博上,看到@Laruence同学出了一个关于C语言的题,微博链接.微博截图如 ...
- image_Magic
http://www.charry.org/docs/linux/ImageMagick/ImageMagick.html mogrify -sample 25% *.jpg 批量处理图片 conv ...
- Rest_framework-2
一 版本 二 解析器 三 序列化 四 请求数据验证 一 版本 作用:应用程序的更新迭代(丰富或添加功能),可以通过版本来实现. 1 .没用rest_framework之前,我们可以通过以下方式来获取 ...
- Team Foundation 中的错误和事件消息
Visual Studio Team System Team Foundation 中的错误和事件消息 Team Foundation 通过显示错误消息和事件消息来通知您操作成功以及操作失败.一部分错 ...
- CodeMatic动软自动生成Nhibernate
前两天调查了下自动生成工具MyGeneration和codesmith前一个版本已经不更新了后面一个太高级生成 的代码包含了太多东西,没整明白.不过生成的xmlmapping很强大.所以干脆整合一下c ...
- 微信小程序组件swiper
视图容器swiper:官方文档 Demo Code Page({ data:{ imgUrls: [ 'http://img02.tooopen.com/images/20150928/tooopen ...