python数据驱动模块ddt
一、安装 pip install ddt
传入一个参数
import unittest
from ddt import ddt,data,unpack
@ddt
class MyTestCase1(unittest.TestCase):
@data(6)
def test_normal(self,value):
print(value)
if __name__=='__main__':
unittest.main()
传入多个参数
import unittest
from ddt import ddt,data,unpack
@ddt
class MyTestCase2(unittest.TestCase):
@data((1,2))
@unpack
def test_tuple(self,value1,value2):
print(value1,value2)
@data([1,2])
@unpack
def test_list(self,value1,value2):
print(value1,value2)
@data({'value1':1,'value2':2})
@unpack
def test_dict(self,value1,value2):
print(value1,value2)
if __name__=='__main__':
unittest.main()
一、安装 pip install parameterized
import unittest
from nose_parameterized import parameterized, param
class TestAdd(unittest.TestCase):
@parameterized.expand([param("10", 16, base=16),])
def test_int(self, str_val, expected, base=10):
print(str_val,expected,base)
self.assertEqual(int(str_val, base), expected)
if __name__ == '__main__':
unittest.main()