# 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 unittest
import nose_parameterized
from 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 res
class 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里面所有的测试用例