一、安装
pip install ddt
二、数据驱动需在类名上加@ddt.ddt,在测试用例上加@ddt.data(*data) ,data为准备的数据
1 import unittest 2 import ddt 3 4 #测试需要准备的数据 5 data = [ 6 {"user":"admin1","pwd":"111111","expect":"True"}, 7 {"user":"admin2","pwd":"222222","expect":"True"}, 8 {"user":"admin3","pwd":"333333","expect":"False"}, 9 ] 10 11 @ddt.ddt 12 class Test01(unittest.TestCase): 13 14 @ddt.data(*data) 15 def test001(self,testdata): 16 print(testdata) 17 18 19 if __name__ == ‘__main__‘: 20 unittest.main()
三、数据驱动登录接口
1 import requests 2 import unittest 3 import ddt 4 from common.testLogin import login,is_login_sucess 5 6 7 data = [ 8 {"user":"admin","pwd":"e10adc3949ba59abbe56e057f20f883e","expect":True}, 9 {"user":"admin2","pwd":"222222","expect":False}, 10 {"user":"admin3","pwd":"333333","expect":False}, 11 ] 12 @ddt.ddt 13 class TestDdtCase(unittest.TestCase): 14 15 def setUp(self): 16 self.s = requests.session() 17 18 @ddt.data(*data) 19 def testlogin01(self,testdata): 20 print("本次测试数据为:%s" %testdata) 21 res = login(self.s,testdata["user"],testdata["pwd"]) 22 #实际结果 23 result = is_login_sucess(res) 24 #期望结果 25 expect = testdata["expect"] 26 #断言, 27 self.assertTrue(result==expect) 28 29 def tearDown(self): 30 self.s.close() 31 32 if __name__ == ‘__main__‘: 33 unittest.main()
原文地址:https://www.cnblogs.com/xiaoyujuan/p/11316513.html
时间: 2024-11-05 20:33:48