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 ...
随机推荐
- php 正则表达式四,例子
PHP常用正则表达式汇总:http://bbs.php100.com/read-htm-tid-83266.html 1.非空匹配: .+ 2.浮点数匹配: ^\d+.\d{2}$ 3.手机号匹配: ...
- Python菜鸟之路:Django 序列化数据
类型一:对于表单数据进行序列化 这时需要用到ErrorDict. ret['errors'] = obj.errors.as_data() result = json.dumps(ret, cls=J ...
- [转载]js复制内容加版权声明代码
转自:https://www.cnblogs.com/zdz8207/p/js-oncopy.html var ua = navigator.userAgent.toLowerCase(); if( ...
- 【我的Android进阶之旅】Android调用JNI出错 java.lang.UnsatisfiedLinkError: No implementation found for的解决方法
错误描述 今天使用第三方的so库时候,调用JNI方法时出现了错误.报错如下所示: 11-01 16:39:20.979 4669-4669/com.netease.xtc.cloudmusic E/a ...
- Leetcode 之 Combination Sum系列
39. Combination Sum 1.Problem Find all possible combinations of k numbers that add up to a number n, ...
- Leetcode 之 Valid Triangle Number
611. Valid Triangle Number 1.Problem Given an array consists of non-negative integers, your task is ...
- C#实现对图片文件的压缩、裁剪操作实例
本文实例讲述了C#对图片文件的压缩.裁剪操作方法,在C#项目开发中非常有实用价值.分享给大家供大家参考.具体如下: 一般在做项目时,对图片的处理,以前都采用在上传时,限制其大小的方式,这样带来诸多不便 ...
- Python之初识函数(Day11)
一.函数的定义与调用 总结一: 定义:def 关键词开头,空格之后接函数名称和圆括号(),最后还有一个":". def 是固定的,不能变,必须是连续的def三个字母,不能分开... ...
- beego——静态文件
Go 语言内部其实已经提供了 http.ServeFile,通过这个函数可以实现静态文件的服务. beego 针对这个功能进行了一层封装,通过下面的方式进行静态文件注册: beego.SetStati ...
- python中的pass语句是什么
当用python写代码时,有时可能还没想好函数怎么写,只写了函数声明,但为了保证语法正确,必须输入一些东西,这种情况下,我们会使用pass语句 def func(*args): pass break语 ...