第一种:
import unittest from common import login from read_xl import read_excel from unittestreport import ddt, list_data data = read_excel('cases.xlsx', 'Sheet1') """ 参数化的具体用法: 1.unittestreport,ddt,list_data 2.在测试函数当中,加入参数row,row参数是可以自己定义的 """ @ddt class TestLogin(unittest.TestCase): @list_data(data) def test_login(self, row): # row 标识每次从data这个list中取出其中一个数据,{} # 代表代码一组测试数据相当于for row in data: # 源码:会自动生成一个新的test_login_01 函数 params_str = row['data'] params = eval(params_str) # 脱掉外层衣服,将字符串转为字典 username = params['username'] password = params['password'] expected = eval(row['expected']) actual = login(username, password) self.assertEqual(expexted, actual)
第二种:
import unittest from common import login from read_xl import read_excel from ddt import ddt, data xl = read_excel('cases.xlsx', 'Sheet1') @ddt class TestLogin(unittest.TestCase): @data(*xl) def test_login(self, row): params_str = row['data'] params = eval(params_str) # 脱掉外层衣服,将字符串转为字典 username = params['username'] password = params['password'] expected = eval(row['expected']) actual = login(username, password) self.assertEqual(expected, actual)