今天写了个测试的代码,结果在执行test_register.py文件在调用readexcle.py的时候一直报错TypeError: 'ExcelData' object is not iterable,最后发现是test_register.py模块调用readexcle.py模块时,忘记调用方法了,导致返回值有误,折腾了半天,是个教训。
下面是readexcle.py模块(读取excle内的数据并返回读取到的数据):
1 import xlrd 2 3 class ExcelData(): 4 def __init__(self,data_path,sheetname): 5 self.data_path = data_path # excle表格路径 6 self.sheetname = sheetname # excle表格内sheet 7 self.data = xlrd.open_workbook(self.data_path) # 打开excle表格 8 self.table = self.data.sheet_by_name(self.sheetname) # 切换到相应sheet 9 self.keys = self.table.row_values(0) # 第一行作为key值 10 self.rowNum = self.table.nrows # 获取表格行数 11 self.colNum = self.table.ncols # 获取表格列数 12 # print(self.rowNum) 13 # print(self.colNum) 14 15 def readExcle(self): 16 if self.rowNum<2: 17 print("excle内数据行数小于2") 18 else: 19 L = [] 20 for i in range(1,self.rowNum): 21 sheet_data = {} 22 for j in range(self.colNum): 23 sheet_data[self.keys[j]] = self.table.row_values(i)[j] 24 L.append(sheet_data) 25 #print(type(L)) 26 return L 27 28 if __name__ == '__main__': 29 data_path = "F:\\KEJINSUO_interface\\Case\\interface_data.xlsx" 30 sheetname = "注册" 31 get_data = ExcelData(data_path,sheetname) 32 print(get_data.readExcle())
下面是test_register.py模块(调用readexcle.py模块,使用readexcle.py返回值做参数):
1 import requests 2 import json 3 import unittest 4 from readexcle import ExcelData 5 6 class Regist(unittest.TestCase): 7 def setUp(self): 8 self.headers = {"Content-Type":"application/json"} 9 self.data_path = "F:\\KEJINSUO_interface\\Case\\interface_data.xlsx" 10 self.sheetname = "注册" 11 self.shuju = ExcelData(self.data_path,self.sheetname).readExcle() #错误在此行,未调用readExcle方法 12 13 def test_regist(self): 14 for a in self.shuju: 15 self.url = "http://test.jkkjs.cn:44444/api/register/createUser" 16 self.pars = {"data": { 17 "mobile":a['mobile'], 18 "password": a['password'], 19 "verifyCode":a['verifyCode'] 20 } 21 } 22 self.par_json = json.dumps(self.pars) 23 res = requests.post(self.url,data=self.par_json,headers=self.headers) 24 #根据返回数据判断登录结果 25 if "注册成功" in res.text: 26 print("注册成功") 27 elif "手机号码已注册" in res.text: 28 print("手机号码已注册") 29 elif "参数错误" in res.text: 30 print("提交参数错误")