对公共模块进行封装 excel模块进行封装
#!usr/bin/env python3
#-*- coding:utf-8 -*-
# Auther : liu wei
import xlrd
class ExcelUtil():
def __init__(self,excelPath,sheetName):
self.data = xlrd.open_workbook(excelPath)
self.table = self.data.sheet_by_name(sheetName)
self.keys = self.table.row_values(0)
self.rowNum = self.table.nrows
self.colNum = self.table.ncols
def dict_data(self):
if self.rowNum<=1:
print("总行数小于1")
else:
r = []
j=1
for i in range(self.rowNum-1):
s={}
values = self.table.row_values(j)
for x in range(self.colNum):
s[self.keys[x]] = values[x]
r.append(s)
j+=1
return r
if __name__=='__main__':
filepath="E:\\Project\\python\\pinke\\first_project_demo\\data.xlsx"
sheetName = "Sheet1"
data = ExcelUtil(filepath,sheetName)
print(data.dict_data())
为了测试模块的数据驱动参数化做准备,公共模块封装起来,在测试脚本中直接调用,测试数据维护在excel中,减少脚本的代码,提高脚本的运行速度,对于数据后期的维护也起到方便的作用。