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()
原文地址:https://www.cnblogs.com/laosun0204/p/8608268.html