day12_unittest参数化文件

import unittest
from nose_parameterized import parameterized

def readCvs(file, sep = ','): # sep是表示以,为分隔符
lis = []
with open(file, 'rb') as f: # rb是什么格式都可以兼容,不会报错
for line in f:
sub_str = line.decode().strip().split(sep)
lis.append(sub_str)
return lis

def login(username,passwd):
if username == 'ssj' and passwd == '123456':
return True
return False

class My(unittest.TestCase):
@parameterized.expand(readCvs('userinfo123.txt')) # 通过上面的函数把参数化文件转成二维数组,这样就不用写N行数据
def testa(self, a, b, hope):
res = login(a, b)
self.assertEqual(bool(hope), res) # bool('True')变成True,可以和res进行比较

if __name__ == '__main__':
unittest.main()
posted @ 2018-03-20 11:33  laosun0204  阅读(101)  评论(0编辑  收藏  举报