python3+requests接口自动化--ddt数据驱动

DDT包含类的装饰器ddt和两个方法装饰器data(直接输入测试数据),file_data(可以从json或者yaml中获取测试数据)

只有yaml和yml结尾的文件以yaml形式上传,其他情况下默认为json

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

@data(a,b)

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

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

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

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

@file_data(filename)

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

 1 import unittest
 2 from ddt import ddt,data,file_data,unpack
 3 
 4 @ddt
 5 class demotest(unittest.TestCase):
 6     def setup(self):
 7         print ("this is the setup")
 8 
 9     @data(2,3)
10     def testb(self,value):
11         print (value)
12         print ("this is test b")
13 
14     @data([2,3],[4,5])
15     def testa(self,value):
16         print (value)
17         print ("this is test a")
18 
19     @data([2, 3], [4, 5])
20     @unpack
21     def testc(self, first,second):
22         print (first)
23         print (second)
24         print ("this is test c")
25 
26     @file_data('d:/data_dic.json')
27     def test_dic(self,value):
28         print (value)
29         print ('this is dic')
30 
31     @file_data('d:/data.yml')
32     def test_yml(self, value):
33         print (value)
34         print ('this is yml')
35 
36     def teardown(self):
37         print ("this is the down")
38 
39 if __name__ == '__main__':
40     unittest.main()

 

posted @ 2018-11-07 10:07  jaysonzxk  阅读(797)  评论(0编辑  收藏  举报