python interface to automate data-driven --ddt

First, install

pip install ddt

Second, the need to add the data driving @ ddt.ddt on the class name, plus @ ddt.data (* data) on the test case, data preparation for data

. 1  Import the unittest
 2  Import DDT
 . 3  
. 4  # test need data 
. 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()

 

Third, data-driven interface to log on

 

 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 is      @ ddt.data (* Data)
 . 19      DEF testlogin01 (Self, the Testdata):
 20 is          Print ( " The test data:% S " % the Testdata)
 21 is          RES = Login (self.s, the Testdata [ " User " ] , the Testdata [ " pwd " ])
 22 is          # actual result 
23 is          result = is_login_sucess (RES)
 24          # desired result 
25          expect the Testdata = [ " expect " ]
 26 is          # assertion, 
27         self.assertTrue(result==expect)
28 
29     def tearDown(self):
30         self.s.close()
31 
32 if __name__ == '__main__':
33     unittest.main()

 

Guess you like

Origin www.cnblogs.com/xiaoyujuan/p/11316513.html