Python unittest excel数据驱动
安装xlrd
下载地址:https://pypi.python.org/pypi/xlrd
安装ddt
下载地址:https://pypi.python.org/pypi/ddt/1.1.0
class ExcelUtil(object):
def __init__(self, excelPath, sheetName):
try:
self.data = xlrd.open_workbook(excelPath)
except Exception as e:
print(str(e))
self.table = self.data.sheet_by_name(sheetName) # get titles
self.row = self.table.row_values(0) # get rows number
self.rowNum = self.table.nrows # get columns number
self.colNum = self.table.ncols # the current column
self.curRowNo = 1 def next(self):
r = []
while self.hasNext():
s = {}
col = self.table.row_values(self.curRowNo)
i = self.colNum
for x in range(i):
s[self.row[x]] = col[x]
r.append(s)
self.curRowNo += 1
return r def hasNext(self):
if self.rowNum == 0 or self.rowNum <= self.curRowNo:
return False
else:
return True
在unittest框架中使用ddt进行迭代,@ddt.data接收一个可迭代的类型,来判断执行的次数,excel.next()返回的是一个数组
import unittest
import ddt
import ExcelUtil
excel = ExcelUtil("excelPath", 'Sheet1') @ddt.ddt
class DataTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
print('start') @classmethod
def tearDownClass(cls):
print('stop') @ddt.data(*excel.next())
def testLogin(self, data):
print(data['username'], data['password'])
if __name__ == "__main__":
suite = unittest.TestLoader().loadTestsFromTestCase(DataTest)
unittest.TextTestRunner(verbosity=2).run(suite)
excel表格如下:

最后的输出结果为:
admin 123456.0
hjh jpmark961203
Python unittest excel数据驱动的更多相关文章
- Python unittest excel数据驱动 写入
之前写过一篇关于获取excel数据进行迭代的方法,今天补充上写入的方法.由于我用的是Python3,不兼容xlutils,所以无法使用copy excel的方式来写入.这里使用xlwt3创建excel ...
- python Unittest+excel+ddt数据驱动测试
#!user/bin/env python # coding=utf- # @Author : Dang # @Time : // : # @Email : @qq.com # @File : # @ ...
- Python Unittest与数据驱动
python中有一个装饰器类DDT,通过它我们可以复用代码,达到数据驱动测试的目的,该类的官方介绍可以参考 http://ddt.readthedocs.io/en/latest/index.html ...
- python+unittest+ddt数据驱动进行接口自动化测试
所谓数据驱动测试,简单的理解为数据的改变从而驱动自动化测试的执行,最终引起测试结果的改变.通过使用数据驱动测试的方法,可以在需要验证多组数据测试场景中,使用外部数据源实现对输入输出与期望值的参数化,避 ...
- Python+unittest+excel
接口测试设计思想: 框架结构如下: 目录如下: readme: config下的run_case_config.ini 文件说明: run_mode: 0:获取所有sheet页 1: if case_ ...
- python+requests+excel+unittest+ddt接口自动化数据驱动并生成html报告
1.环境准备: python3.6 requests xlrd openpyxl HTMLTestRunner_api 2.目前实现的功能: 封装requests请求方法 在excel填写接口请求参数 ...
- python+requests+excel+unittest+ddt接口自动化数据驱动并生成html报告(二)
可以参考 python+requests接口自动化完整项目设计源码(一)https://www.cnblogs.com/111testing/p/9612671.html 原文地址https://ww ...
- python+requests+excel+unittest+ddt接口自动化数据驱动并生成html报告(已弃用)
前言 1.环境准备: python3.6 requests xlrd openpyxl HTMLTestRunner_api 2.目前实现的功能: 封装requests请求方法 在excel填写接口请 ...
- Python Selenium 之数据驱动测试
数据驱动模式的测试好处相比普通模式的测试就显而易见了吧!使用数据驱动的模式,可以根据业务分解测试数据,只需定义变量,使用外部或者自定义的数据使其参数化,从而避免了使用之前测试脚本中固定的数据.可以将测 ...
随机推荐
- (GoRails)ActiveRecord --explain方法:(优化你的查询)
https://gorails.com/episodes/activerecord-explain?autoplay=1 比如没有加index的查询和加了index的查询,调用数据库的速度就差5倍. ...
- 阿里云负载均衡配置https记录
配置前端协议是443,后端是80 问题1记录: 例如访问https://www.xxx.com,在后端服务器上面获取是http还是https请求协议实际上是http: 因为我们先请求负载均衡,负载均衡 ...
- Qt 线程基础
(转自:http://my.oschina.net/laopiao/blog/88158) 何谓线程? 线程与并行处理任务息息相关,就像进程一样.那么,线程与进程有什么区别呢?当你在电子表格上进行数据 ...
- hdu2883
题解: 网络流 用一个离散化 代码: #include<cstdio> #include<cstring> #include<algorithm> using na ...
- 爬虫框架:scrapy
一 介绍 Scrapy一个开源和协作的框架,其最初是为了页面抓取 (更确切来说, 网络抓取 )所设计的,使用它可以以快速.简单.可扩展的方式从网站中提取所需的数据.但目前Scrapy的用途十分广泛,可 ...
- Javascript几种创建对象的方法
1.工厂方法 demo.js 1 2 3 4 5 6 7 8 9 10 11 function createPerson(name, age) { var person = new Object(); ...
- 关于面向对象和String类型的 09,10
package test.面试题; public class Test9 { public static void main(String[] args){ Outer.Inner in=new Ou ...
- mysql数据添加时如果这条数据存在进行修改
1.建表 CREATE TABLE vipMovie( id INT PRIMARY KEY AUTO_INCREMENT, md_name VARCHAR(255) NOT NULL UNIQUE, ...
- springboot---没有配置数据库启动报错
If you want an embedded database please put a supported one on the classpath. If you have database s ...
- CentOS怎样安装Python3.6
yum install -y openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel安装可能用到的依赖 ...