1. 操作csv,使用python的csv模块
操作如下数据表csv格式
# -*- coding: utf-8 -*-
""" Created on Sun May 20 13:04:27 2018 @author: spfhy """
import csv
class OpTestConfig:
def __init__(self,ops,case_name,param,run_mode,run_type):
self.ops = ops
self.case_name = case_name
self.param = param
self.run_mode = run_mode
self.run_type = run_type
def opTestGenerateCmd(self):
cmd = self.ops + ' ' + self.case_name + ' '\
+ self.run_mode + ' ' + self.param + ' ' + self.run_type
return cmd
#解析config配置文件
binFilePath = ''
run_mode = 'IPU'
run_type = '3'
with open("config.csv", "r", encoding = "utf-8") as f:
reader = csv.reader(f)
for row in reader:
if row[0] == 'filepath':
binFilePath = row[1]
print('filepath : '+ binFilePath)
elif row[0] == 'run_mode':
run_mode = row[1]
print('run_mode : '+run_mode)
elif row[0] == 'run_type':
run_type = row[1]
print('run_type : ' + run_type)
f.close()
#解析test_case列表
OpTestlist = []
with open("test_xlwt.csv", "r", encoding = "utf-8") as f:
reader = csv.DictReader(f)
for row in reader:
if row['select'] == 'yes':
#print(row)
Optest = OpTestConfig(row['ops'],row['case_name'],row['param'],run_mode,run_type)
OpTestlist.append(Optest)
f.close()
test_case_num = len(OpTestlist)
if test_case_num > 0:
print('case num is :' + str(test_case_num))
for i in range(test_case_num):
print(str(i) + ': '+ OpTestlist[i].opTestGenerateCmd())
else:
print('no case to run')
2. 操作xsl,使用python的csv模块
使用python的第三方库xlwt,xlrd操作excel文件
# -*- coding: utf-8 -*-
""" Created on Sat May 19 10:54:25 2018 @author: spfhy """
''' 写xml import xlwt workbook = xlwt.Workbook(encoding='utf-8') booksheet = workbook.add_sheet('test case', cell_overwrite_ok=True) #存第一行cell(1,1)和cell(1,2) booksheet.write(0,0,'序号') booksheet.write(0,1,'算子') booksheet.write(0,3,'testcast') booksheet.write(0,4,'run_type') #存第二行cell(2,1)和cell(2,2) booksheet.write(1,0,36) booksheet.write(1,1,39) #存一行数据 rowdata = [43,56] for i in range(len(rowdata)): booksheet.write(2,i,rowdata[i]) workbook.save('test_xlwt.xls') '''
''' 读xml '''
import xlrd
class OpTestConfig:
def __init__(self,ops,case_name,param,run_mode,run_type):
self.ops = ops
self.case_name = case_name
self.param = param
self.run_mode = run_mode
self.run_type = run_type
def opTestGenerateCmd(self):
cmd = self.ops + ' ' + self.case_name + ' '\
+ self.run_mode + ' ' + self.param + ' ' + self.run_type
return cmd
#解析config配置文件
binFilePath = ''
run_mode = ''
run_type = ''
booksheet = xlrd.open_workbook('test_xlwt.xls')
table = booksheet.sheet_by_name('config')
nrows = table.nrows
for row in range(nrows):
if table.cell(row,0).value == 'bin_so_filepath':
binFilePath = table.cell(row,1).value
elif table.cell(row,0).value == 'run_mode':
run_mode = table.cell(row,1).value
elif table.cell(row,0).value == 'run_type':
run_type = str(table.cell(row,1).value)
#解析test_case列表
table = booksheet.sheet_by_name('test_case')
ncols = table.ncols
chpice_col = 0
ops_col = 0
case_col = 0
run_type_col = 0
for col in range(ncols):
cellValue = table.cell(0,col).value
print(cellValue)
if(cellValue == 'select'):
chpice_col = col
elif(cellValue == 'ops'):
ops_col = col
elif(cellValue == 'case_name'):
case_col = col
elif(cellValue == 'param'):
param_col = col
OpTestlist = []
for i in range(table.nrows):
if(table.cell(i,chpice_col).value == 'yes'):
ops = table.cell(i,ops_col).value
case_name = table.cell(i,case_col).value
param = table.cell(i,param_col).value
Optest = OpTestConfig(ops,case_name,param,run_mode,run_type)
OpTestlist.append(Optest)
oplist_len = len(OpTestlist)
if oplist_len > 0:
print(str(oplist_len) + ' testcase need to run')
for i in range(oplist_len):
print(str(i)+ ': ' + OpTestlist[i].opTestGenerateCmd())
else:
print('no testcase need to run')