python ddt处理如何传入 list, dict ,str数据

前尘篇
# coding=utf-8
import ddt
import unittest
import time
test_data =[('test_name','post','https://www.douban.com','{"key": "value"}','{"Content-Type": "text/plain;charset=UTF-8"}','{"projectName": "pipe", "jobId": "70558866DFF6567A"}','data'),('test_name2','get','https://www.douban.com','{"key": "value"}','{"Content-Type": "text/plain;charset=UTF-8"}','{"projectName": "pipe", "jobId": "60558866DFF6567B"}','json')]
test_tuple=[('str1','type1'),('str2', 'type2')]
test_list=["var1", "var2","var3"]
test_dict= [{"username": "zhangsan", "pwd": "zhangsan"},
             {"username": "lisi", "pwd": "lisi"},
             {"username": "wangwu", "pwd": "wangwu"},
             ]

@ddt.ddt
class  Test(unittest.TestCase):
    @ddt.data(*test_data)
    @ddt.unpack
    def test_data(self,test_name,method,url,dict,header,param,type):
        print test_name,method,url,eval(dict),eval(header),eval(param),type,time.asctime()

    @ddt.data(*test_list)
    def test_list(self,var):
        print var

    @ddt.unpack
    @ddt.data(*test_tuple)
    def test_tuple(self,tuple_1,tuple_2):
        print tuple_1,tuple_2

    @ddt.data(*test_dict)
    def test_dict(self,dict):
        print dict.get('username',None)
if __name__ == "__main__":
    unittest.main(verbosity=1)
..........
test_name post https://www.douban.com {'key': 'value'} {'Content-Type': 'text/plain;charset=UTF-8'} {'projectName': 'pipe', 'jobId': '70558866DFF6567A'} data Thu Nov 22 23:34:32 2018
test_name2 get https://www.douban.com {'key': 'value'} {'Content-Type': 'text/plain;charset=UTF-8'} {'projectName': 'pipe', 'jobId': '60558866DFF6567B'} json Thu Nov 22 23:34:32 2018
zhangsan
lisi
wangwu
var1
var2
var3
str1 type1
str2 type2

往事篇
基础单个处理

import ddt
import unittest

test_dict=[{"account": "zs","pwd": "123456"},{"account": "test","pwd": "86556"}]
test_list=['str1','str2','str3']
test_tuple=[(1,2),(0,3),(4,5)]

@ddt.ddt
class  Test(unittest.TestCase):
    @ddt.data(*test_dict)
    def test_dict(self,dict):
        print dict.get('account',None),dict.get('pwd', None)

    @ddt.data(*test_list)
    def test_list(self,test_list):
        print test_list

    @ddt.unpack
    @ddt.data(*test_tuple)
    def test_tuple(self,tuple0,tuple1):
        print tuple0,tuple1
if __name__ == "__main__":
    unittest.main(verbosity=2)


zs 123456
test_dict_1 (__main__.Test)
test 86556
dict() -> new empty dictionary ... ok
str1
test_dict_2 (__main__.Test)
str2
str3
1 2
0 3
4 5
dict() -> new empty dictionary ... ok
test_list_1_str1 (__main__.Test)
str(object='') -> string ... ok
test_list_2_str2 (__main__.Test)
str(object='') -> string ... ok
test_list_3_str3 (__main__.Test)
str(object='') -> string ... ok
test_tuple_1__1__2_ (__main__.Test)
tuple() -> empty tuple ... ok
test_tuple_2__0__3_ (__main__.Test)
tuple() -> empty tuple ... ok
test_tuple_3__4__5_ (__main__.Test)
tuple() -> empty tuple ... ok

----------------------------------------------------------------------
Ran 8 tests in 0.000s

OK

 

你可能感兴趣的:(接口测试)