1. 程式人生 > 實用技巧 >介面測試(Python)之DDT

介面測試(Python)之DDT

本文是學習了小二哥很二的簡書,連結地址:https://www.jianshu.com/p/78998bcf3e05

一、安裝ddt模組

1 pip install ddt

ddt有四種模式:引入裝飾器@ddt;匯入資料@data;拆分資料@unpack;匯入外部資料@file_data

二、引數化實現的方式

1、讀取元組資料

 1 #一定要和單元測試框架一起用
 2 import unittest,os
 3 from ddt import ddt,data,unpack,file_data
 4 
 5 '''NO.1單組元素'''
 6 @ddt
 7 class Testwork(unittest.TestCase):
8 9 @data(1,2,3) 10 def test_01(self,value): #value用來接收data的資料 11 print(value) 12 if __name__ == '__main__': 13 unittest.main() 14 結果: 15 =>1 16 2 17 3 18 19 '''NO.2多組未分解元素''' 20 @ddt 21 class Testwork(unittest.TestCase): 22 23 @data((1,2,3),(4,5,6)) 24 def
test_01(self,value): 25 print(value) 26 27 if __name__ == '__main__': 28 unittest.main() 29 結果: 30 =>(1, 2, 3) 31 (4, 5, 6) 32 33 '''NO.3多組分解元素''' 34 @ddt 35 class Testwork(unittest.TestCase): 36 37 @data((1,2,3),(4,5,6)) 38 @unpack #拆分資料 39 def test_01(self,value1,value2,value3): #
每組資料有3個值,所以設定3個形參 40 print(value) 41 42 if __name__ == '__main__': 43 unittest.main() 44 結果: 45 =>1 2 3 46 4 5 6

2、讀取列表資料

 1 import unittest,os
 2 from ddt import ddt,data,unpack,file_data
 3 
 4 '''NO.1多組元素未分解'''
 5 @ddt
 6 class Testwork(unittest.TestCase):
 7 
 8     @data([{'name':'lili','age':12},{'sex':'male','job':'teacher'}])
 9     def test_01(self,a):
10         print(a)
11 
12 if __name__ == '__main__':
13     unittest.main()
14 結果:
15 =>[{'name': 'lili', 'age': 12}, {'sex': 'male', 'job': 'teacher'}]
16 ※上面結果可以看出:無法運用到requests資料請求中,所以不是很實用※
17 
18 
19 '''NO.2多組元素分解'''
20 @ddt
21 class Testwork(unittest.TestCase):
22 
23     @data([{'name':'lili','age':12},{'sex':'male','job':'teacher'}])
24     @unpack
25     def test_01(self,a,b):
26         print(a,b)
27 
28 if __name__ == '__main__':
29     unittest.main()
30 結果:
31 =>{'name': 'lili', 'age': 12} {'sex': 'male', 'job': 'teacher'}
32 ※拆分後的執行結果,不帶有[ ],拆分是將列表中的2個字典拆分,所以有2個數據※

3、讀取字典資料

 1 import unittest,os
 2 from ddt import ddt,data,unpack,file_data
 3 
 4 '''※字典的讀取比較特殊,因為在拆分的時候,形參和實參的key值要一致,否則就報錯※'''
 5 
 6 '''NO.1單組資料未分解'''
 7 @ddt
 8 class Testwork(unittest.TestCase):
 9 
10     @data({'name':'lili','age':'16'},{'sex':'female','job':'nurser'})
11     def test_01(self,a):
12         print(a)
13 
14 if __name__ == '__main__':
15     unittest.main()
16 結果:
17 =>{'name': 'lili', 'age': '16'}
18   {'sex': 'female', 'job': 'nurser'}
19 ※以上執行的結果資料,就可以用來作為requests的請求引數~!※
20 
21 
22 '''NO.2多資料拆分,重點來了'''
23 @ddt
24 class Testwork(unittest.TestCase):
25 
26     @data({'name':'lili','age':'16'},{'name':'female','age':'nurser'})
27     @unpack
28     def test_01(self,name,age):
29         print(name,age)
30 
31 if __name__ == '__main__':
32     unittest.main()
33 結果:
34 =>lili 16
35   female nurser
36 ※重點來了:首先結果展示的資料是字典裡的value,沒有列印key的值;其次@data裡的資料key值和def方法裡的形參名稱一定要一致,否則,列印的時候,就會報莫名的引數錯誤。※

4、讀取json檔案資料

 1 # data_json.json檔案
 2 {
 3     "test_case1": {
 4         "key": "value1",
 5         "status_code": 200
 6     },
 7     "test_case2": {
 8         "key": "value2",
 9         "status_code": 200
10     },
11     "test_case3": {
12         "key": "value3",
13         "status_code": 200
14     }
15 }
16 
17 
18 # python讀取json檔案
19 @ddt
20 class InterfaceTest(unittest.TestCase):
21 
22     def setUp(self):
23         self.url = "http://httpbin.org/get"
24 
25     def tearDown(self):
26         print(self.result)
27 
28     @file_data("data_json.json")  #如不在同一級目錄下需另做處理
29     def test_get_request(self, key, status_code):
30         r = requests.get(self.url, params={"key": key})
31         self.result = r.json()
32         self.assertEqual(r.status_code, status_code)
33 
34 if __name__ == '__main__':
35     unittest.main()