目录结构:
测试用例:
数据类case_info.py:
用于存放测试数据的对象类
import os
class CaseInfo:
def __init__(self,case_number,case_name,case_mode,case_pri,front_data,case_step,exp_results,act_results):
self.case_number = case_number
self.case_name = case_name
self.case_mode = case_mode
self.case_pri = case_pri
self.front_data = front_data
self.case_step = case_step
self.exp_results = exp_results
self.act_results = act_results
if __name__ =='__main__':
cerrunt_path = os.path.dirname(__file__)
file_path = cerrunt_path + '/../test_case/test_case.xls'
read_excel01.py:
测试用例转成字典、列表、列表+字典:
import os
import xlrd
class ReadExcel:
def __init__(self,file_path):
self.file_path=file_path
def reade_in_list(self): #把测试数据做成列表
try:
excel_file = xlrd.open_workbook(self.file_path)
sheet = excel_file.sheet_by_index(0)
all_case_infos = []
for i in range(1, sheet.nrows):
case_info = []
for j in range(sheet.ncols):
case_info.append(sheet.cell_value(i, j))
all_case_infos.append(case_info)
except FileNotFoundError as e:
print(e)
except Exception as e:
print(e)
else:
return all_case_infos
#字典格式
#{'case01':{...},'case02':{}}
def reade_in_dict(self):
try:
excel_file = xlrd.open_workbook(self.file_path)
sheet = excel_file.sheet_by_index(0)
all_case_infos = {}
for i in range(1, sheet.nrows):
case_info = []
case_key = ''
for j in range(sheet.ncols):
if j == 0:
case_key = sheet.cell_value(i,j)
else:
case_info.append(sheet.cell_value(i, j))
all_case_infos[case_key] = case_info
except FileNotFoundError as e:
print(e)
except Exception as e:
print(e)
else:
return all_case_infos
def reade_in_list_dict(self):
try:
excel_file = xlrd.open_workbook(self.file_path)
sheet = excel_file.sheet_by_index(0)
all_case_infos = []
for i in range(1, sheet.nrows):
case_info = {}
for j in range(sheet.ncols):
case_info[sheet.cell_value(0, j)] = sheet.cell_value(i, j)
all_case_infos.append(case_info)
except FileNotFoundError as e:
print(e)
except Exception as e:
print(e)
else:
return all_case_infos
if __name__ == '__main__':
cerrunt_path = os.path.dirname(__file__)
file_path = cerrunt_path + '/../test_case/test_case.xls'
list1 = ReadExcel(file_path)
case1 = list1.reade_in_list()
print(case1)
read_excel02.py:
调用数据类,存放excel数据到数据类中
import os
import xlrd
from case_info import CaseInfo
class ExcelDataInof:
def __init__(self,file_path):
self.file_path = file_path
def read_data_in_class(self):
all_case_info = []
try:
excel_file = xlrd.open_workbook(self.file_path)
sheet = excel_file.sheet_by_index(0)
for i in range(1,sheet.nrows):
case_info = CaseInfo(sheet.cell_value(i,0),
sheet.cell_value(i,1),
sheet.cell_value(i,2),
sheet.cell_value(i,3),
sheet.cell_value(i,4),
sheet.cell_value(i,5),
sheet.cell_value(i,6),
sheet.cell_value(i,7))
all_case_info.append(case_info)
except FileNotFoundError as e:
print(e)
except Exception as e:
print(e)
else:
return all_case_info
if __name__ == '__main__':
cerrunt_path = os.path.dirname(__file__)
file_path = cerrunt_path + '/../test_case/test_c1ase.xls'
try:
Excel_data = ExcelDataInof(file_path)
str1 = Excel_data.read_data_in_class()
print(str1[0].act_results)
except Exception as e:
print(e)
read_excel03.py:
调用read_excel01或者read_excel02验证:
import os
from read_excel01 import ReadExcel
if __name__ == '__main__':
cerrunt_path = os.path.dirname(__file__)
file_path = cerrunt_path + '/../test_case/test_case.xls'
try:
list1 = ReadExcel(file_path)
case1 = list1.reade_in_dict()
print(case1)
except Exception as e:
print(e)
执行结果:
{‘case_01’: [‘登录功能是否正确实现’, ‘登录’, 1.0, ‘admin’, ‘使用admin’, ‘登录成功’, ‘登录成功’], ‘case_02’: [‘添加购物车正确实现’, ‘购物车’, 2.0, 10000.0, ‘添加购物车’, ‘添加成功’, ‘添加成功’], ‘case_03’: [‘退出功能正确实现’, ‘登录’, 1.0, ‘#@%#?’, ‘退出’, ‘退出成功’, ‘退出成功’], ‘case_04’: [‘用户注册正确实现’, ‘注册’, 1.0, ‘admin001’, ‘注册’, ‘注册成功’, ‘注册成功’]}