# file_to_list
import os
import xlrd
import json
class DataToParam(object):
@classmethod
def text(cls, file_name, seq=','):
cls.file_exist(file_name)
with open(file_name, encoding='utf-8') as f:
res = []
for line in f:
res.append(line.strip().split(seq))
return res
@classmethod
def excel(cls, file_name):
cls.file_exist(file_name)
book = xlrd.open_workbook(file_name) # 打開excel
sheet = book.sheet_by_index(0) # 獲取sheet頁
res = []
for row in range(sheet.nrows):
line_list = sheet.row_values(row) # 取excel裏面的每一行數據,返回是一個list
res.append(line_list)
return res
@classmethod
def file_exist(cls, file_name):
if os.path.isfile(file_name):
return True
raise Exception('參數化文件不存在!')
# print(json.dumps(DataToParam.excel('data.xlsx'), indent=4))
import unittestimport nose_parameterizedfrom file_to_list import DataToParam# case_data = [# [1, 1, 1.0],# [1, 2, 0.5],# [0.5, 1, 0.5]# ]def calc(a, b): a = int(a) b = int(b) res = round(a / b, 2) return resclass MyTest(unittest.TestCase): @nose_parameterized.parameterized.expand(DataToParam.excel('data.xlsx')) def test_func(self, a, b, e): res = calc(a, b) self.assertEqual(res, float(e))if __name__ == '__main__': unittest.main() # 他會幫你運行當前這個python裏面全部的測試用例