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 ...
随机推荐
- 对opencv.hpp头文件的认识
OpenCV学习笔记(二):对opencv.hpp头文件的认识 - 安东的技术博客 - CSDN博客 https://blog.csdn.net/xidiancoder/article/details ...
- 第16章—日志(slf4j&logback)
spring boot 系列学习记录:http://www.cnblogs.com/jinxiaohang/p/8111057.html 码云源码地址:https://gitee.com/jinxia ...
- Django之html-模板继承
在编程的过程中,我们经常会重复性的写了很多的代码,比如一个页面的框架部分,这样我又多少个页面就得写上多少次,这样既不好维护,也不够高效,所以我们引出了html的模板继承部分. 1.写好一个html文件 ...
- MySQL数据库(5)- pymysql的使用、索引
一.pymysql模块的使用 1.pymysql的下载和使用 之前我们都是通过MySQL自带的命令行客户端工具mysql来操作数据库,那如何在python程序中操作数据库呢?这就需要用到pymysql ...
- PHP 数组教程 定义数组
数组array是一组有序的变量,其中每个变量被叫做一个元素. 一.定义数组 可以用 array() 语言结构来新建一个数组.它接受一定数量用逗号分隔的 key => value 参数对. a ...
- Behind The Cloud--浅析分布式系统背后的基础设施
http://blog.csdn.net/it_yuan/article/details/8617127 Behind The Cloud--浅析分布式系统背后的基础设施 分类: 系统架构2013- ...
- 大家一起来学 NHibernate+NUnit (VS2012+SQL Server2008)
大家一起来学 NHibernate+NUnit (VS2012+SQL Server2008) 分类: C#2013-08-10 18:47 1589人阅读 评论(5) 收藏 举报 NHibernat ...
- Visual Studio2012打不开MVC2.0以及1.0项目如何处理
/*打开扩展名为csproj的工程文件*/ <ProjectTypeGuids> {F85E285D-A4E0---AB1D724D3325};{349c5851-65df-11da--0 ...
- LeetCode:二叉搜索树中的搜索【700】
LeetCode:二叉搜索树中的搜索[700] 题目描述 给定二叉搜索树(BST)的根节点和一个值. 你需要在BST中找到节点值等于给定值的节点. 返回以该节点为根的子树. 如果节点不存在,则返回 N ...
- 《Spring Boot 实战》随记
第一部分 Spring 4.x 1. Spring基础 略过 2. Spring常用配置 2.1 Bean的scope 使用@Scope注解配置scope.默认signleton,原型模式protot ...