import ddt
import unittest
"""
ddt模块包含了一个类的装饰器ddt和两个方法的装饰器:
data:包含多个你想要传给测试用例的参数;
file_data:会从json或yaml中加载数据;
通常data中包含的每一个值都会作为一个单独的参数传给测试方法,如果这些值是用元组或者列表传进来的,可以用unpack方法将其自动分解成多个参数。
"""
test_data2 = [{"a": "1", "pwd": "123456"},
{"b": "2", "pwd": "123456"}]
@ddt.ddt
class Test(unittest.TestCase):
def setUp(self):
print("Start!")
def tearDown(self):
print("end!")
@ddt.file_data('url.yaml')
def test_ddt1(self, **data):
print(data)
"""
执行2次,yaml文件中是一个含有2个元素的list
Start!
{'url': 'http://feature.v3.0.1.xxxx.com/albums',
'headers': {'Authorization': 'x3JpemF0aW9ucyIsIxxwz8V1gn80yE-b8',
'Accept': 'application/vnd.xxx.admin+json'},
'Body': {'url': 'http://list.youku.com/show/ixxxbf.html?spm=a2h0j.11185381.bpmodule-playpage-righttitle.5~H2~A'},
'code': {'status_code': 200, 'message': 'success'}}
end!
Start!
{'url': 'http://feature.v3.0.1.xxxx.com/albums',
'headers': {'Authorization': 'x3JpemF0aW9ucyIsIxxwz8V1gn80yE-b8',
'Accept': 'application/vnd.xxx.admin+json'},
'Body': {'url': 'http://list.youku.com/show/ixxxbf.html?spm=a2h0j.11185381.bpmodule-playpage-righttitle.5~H2~A'},
'code': {'status_code': 200, 'message': 'success'}}
end!
"""
@ddt.data(*test_data2)
def test_ddt2(self, data):
print(data)
return data
"""
Start!
{'a': '1', 'pwd': '123456'}
end!
Start!
{'b': '2', 'pwd': '123456'}
end!
"""
# suit = unittest.TestSuite()
# suit.addTest(Test('test_ddt1'))
# suit.addTest(unittest.makeSuite(Test))
# unittest.TextTestRunner().run(suit)
if __name__ == "__main__":
unittest.main()