• 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数据看起来是最外层循环
  • 相关阅读:
    nyoj163 Phone List
    hdu1251统计难题
    hdu1754 I Hate It
    nyoj123 士兵杀敌(四)
    poj3468 A Simple Problem with Integers
    zoj1610 Count the Colors
    nyoj144 小珂的苦恼
    nyoj93 汉诺塔(三)
    poj2182 Lost Cows
    ASP.NET2.0中的Callback机制
  • 原文地址:https://www.cnblogs.com/lisi01/p/11157675.html
Copyright © 2020-2023  润新知