ddt学习笔记

ddt安装
Pip install ddt

ddt的使用
导入
from ddt import ddt,data,file_data

说明:
@data(a,b)

表示a和b各运行一次用例

@data([a,d],[c,d])

如果没有unpack,那么[a,b]当成一个参数传入用例运行

如果有unpack,那么[a,b]被分解开,按照用例中的两个参数传递

@file_data(filename)

对于json的文件,每一个json元素按照一个用例运行,可以依照python分解元组,列表或者字典的方式分解传入支持yaml

例子
import unittest
from ddt import ddt,data,file_data,unpack

@ddt
class demotest(unittest.TestCase):
def setup(self):
print "this is the setup"

@data(2,3)
def testb(self,value):
print value
print "this is test b"

@data([2,3],[4,5])
def testa(self,value):
print value
print "this is test a"

@data([2, 3], [4, 5])
@unpack
def testc(self, first,second):
print first
print second
print "this is test c"

@file_data('d:/data_dic.json')
def test_dic(self,value):
print value
print 'this is dic'

@file_data('d:/data.yml')
def test_yml(self, value):
print value
print 'this is yml'

def teardown(self):
print "this is the down"

if __name__ == '__main__':
unittest.main()
#suite=unittest.TestLoader.getTestCaseNames(demotest)
#suite = unittest.TestLoader().loadTestsFromTestCase(demotest)
#unittest.TextTestRunner(verbosity=2).run(suite)

ps 使用发现方法中有循环时ddt数据看起来是最外层循环
posted @ 2019-07-09 15:28  walle_ok  阅读(283)  评论(0编辑  收藏  举报