UnitTest 参数化ddt,paramunittest
ddt
import unittest
import ddt
# from ddt import ddt,data,unpack,file_data
@ddt.ddt
class Testwork(unittest.TestCase):
@ddt.data([1,2],{'username':'4','password':'6'})
# @ddt.data((1,2),(3,4))
# @ddt.file_data(*login_xls)
@ddt.unpack
def test_01(self,username,password):
print(username,'******',password)
def test_02(self):
print('02')
if __name__ == '__main__':
unittest.main()
# 执行结果为3条用例
paramunittest
import unittest
import paramunittest
# @paramunittest.parametrized(*login_xls)
@paramunittest.parametrized(('1',"2"),{'username':'4','password':'6'})
class Testwork(unittest.TestCase):
# 注意这里接受参数的时候,必须要定义setParameters这个方法,并且只能是这个名称。括号后面的参数分别接受传入的参数名称。前面定义的是字典,那参数就跟前面字典的key保持一致
def setParameters(self,username,password):
self.username = username
self.password = password
def test_01(self):
print(self.username,'******',self.password)
def test_02(self):
print('02')
if __name__ == '__main__':
unittest.main()
# 执行结果为4条用例