zoukankan      html  css  js  c++  java
  • python ddt 实现数据驱动一

    ddt 是第三方模块,需安装, pip install ddt

    DDT包含类的装饰器ddt和两个方法装饰器data(直接输入测试数据)

    通常情况下,data中的数据按照一个参数传递给测试用例,如果data中含有多个数据,以元组,列表,字典等数据,需要自行在脚本中对数据进行分解或者使用unpack分解数据。

    @data(a,b)

    那么a和b各运行一次用例

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

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

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

    具体看下面的例子:

    复制代码
    import unittest
    from ddt import ddt,data,unpack
    
    @ddt
    class MyTesting(unittest.TestCase):
        def setUp(self):
            print('this is the setUp')
        @data([1,2,3])
        def test_1(self,value):
            print(value)
    
        @data([3,2,1],[5,3,2],[10,4,6])
        @unpack
        def test_minus(self,a,b,expected):
            actual = int(a) - int(b)
            expected = int(expected)
            self.assertEqual(actual, expected)
    
        @data([2,3],[4,5])
        def test_compare(self,a,b):
            self.assertEqual(a,b)
    
        def tearDown(self):
            print('this is tearDown')
    
    if __name__ == '__main__':
        unittest.main(verbosity=2)
    复制代码

    结果分析:

    1. test_1的测试结果是ok的, 因为 [1,2,3] 作为一个整体传给value,所有value 打印的值是[1,2,3]

    test_1_1__1__2__3_ (__main__.MyTesting) ... ok
    test_compare_1__2__3_ (__main__.MyTesting) ... ERROR
    [1, 2, 3]

    2. test_minus的测试结果也是ok的,由于在@data(...)下加了@unpack, 代表会把数据分解,得到3组测试数据,分别为:

    1.[3,2,1]
    2.[5,3,2]
    3.[10,4,6]
    test_minus_1__3__2__1_ (__main__.MyTesting) ... ok
    test_minus_2__5__3__2_ (__main__.MyTesting) ... ok
    test_minus_3__10__4__6_ (__main__.MyTesting) ... ok

    3. test_compare的测试结果是fail的,由于没有加@unpack, 虽然还是会被理解成2组测试数据,但是[2,3]作为一个整体被传给了a, 因为b就没有值传入了,所以一执行后报了  TypeError: test_compare() missing 1 required positional argument: 'b'  这句错。

    test_compare_1__2__3_ (__main__.MyTesting) ... ERROR
    test_compare_2__4__5_ (__main__.MyTesting) ... ERROR
    复制代码
    this is the setUp
    ERROR: test_compare_1__2__3_ (__main__.MyTesting)
    this is tearDown
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "D:pythonlibsite-packagesddt.py", line 139, in wrapper
        return func(self, *args, **kwargs)
    TypeError: test_compare() missing 1 required positional argument: 'b'
    
    ======================================================================
    ERROR: test_compare_2__4__5_ (__main__.MyTesting)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "D:pythonlibsite-packagesddt.py", line 139, in wrapper
        return func(self, *args, **kwargs)
    TypeError: test_compare() missing 1 required positional argument: 'b'
    复制代码

    @data()里的数据组可以为元祖,list,字典

    复制代码
    @ddt
    class MyTest(unittest.TestCase):
    
        @data((8, 6), (4, 0), (15, 6))
        @unpack
        def test_tuples(self, first, second):
            self.assertTrue(first > second)
    
        @data([30, 29], [40, 30], [5, 3])
        @unpack
        def test_list(self, first, second):
            self.assertTrue(first > second)
      '''
       data()里的数组为为字典时,方法的参数名为字典的key值
      ''' @data({'first': 1, 'second': 3, 'third': 5}, {'first': 4, 'second': 7, 'third': 8}) @unpack def test_dicts(self, first, second, third): self.assertTrue(first < second < third) if __name__ == '__main__': unittest.main(verbosity=2)
    复制代码
    复制代码
    def get_Csv(filename):
        rows = []
        with open(filename,encoding='utf-8') as f:
            readers = csv.reader(f)
            for row in readers:
                rows.append(row)
        return rows
    
    @ddt
    class MyTest(unittest.TestCase):
    
        @data(*get_Csv('test_csv.csv'))
        @unpack
        def test_data_csv(self,v1,v2,v3):
            print(v1)
            print(v2)
            print(v3)
    复制代码

    以上就是ddt 的学习总结,ddt 还有file_data(可以从json或者yaml中获取测试数据)的驱动方式,下篇继续。

  • 相关阅读:
    CS231n assignment3 Q1 Image Captioning with Vanilla RNNs
    使用tensorflow预测函数的参数值(a simple task)
    CS231n assignment2 Q5 TensorFlow on CIFAR-10
    CS231n assignment2 Q4 Convolutional Networks
    HDU 1561 The more, The Better
    HDU4003 Find Metal Mineral
    poj 1947 Rebuilding Roads
    2090 背包
    poj 2408 Apple Tree
    奔跑的xiaodao
  • 原文地址:https://www.cnblogs.com/wsy0202/p/12845140.html
Copyright © 2011-2022 走看看