python ddt 實現數據驅動一

ddt 是第三方模塊,需安裝, pip install ddtpython

DDT包含類的裝飾器ddt和兩個方法裝飾器data(直接輸入測試數據)json

一般狀況下,data中的數據按照一個參數傳遞給測試用例,若是data中含有多個數據,以元組,列表,字典等數據,須要自行在腳本中對數據進行分解或者使用unpack分解數據。app

@data(a,b)學習

那麼a和b各運行一次用例測試

 

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

若是沒有@unpack,那麼[a,b]當成一個參數傳入用例運行this

若是有@unpack,那麼[a,b]被分解開,按照用例中的兩個參數傳遞spa

 

具體看下面的例子:code

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)

結果分析:blog

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:\python\lib\site-packages\ddt.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:\python\lib\site-packages\ddt.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({'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中獲取測試數據)的驅動方式,下篇繼續啦。

相關文章
相關標籤/搜索