Use of DDT
DDT contains a class of adorners DDT and two method adorner data (direct input test data), File_data (can get test data from JSON or YAML)
Only files ending in Yaml and YML are formatted in Yaml, and in other cases JSON is the default
Typically, data is passed to the test case by one parameter, and if data contains multiple data, tuples, lists, dictionaries, and so on, it is necessary to decompose the data in the script or use unpack to decompose the data.
@data (A, B)
Then A and B each run the use case
@data ([a,d],[c,d])
If there is no unpack, then [A, b] is passed into the use case as a parameter run
If there is a unpack, then [A, b] is decomposed and passed according to two parameters in the use case
@file_data (filename)
For JSON files, each JSON element runs according to a use case, and can be broken down into the way that Python decomposes tuples, lists, or dictionaries.
Importddt,requestsImportUnittest,beautifulreport#parameterize, execute multiple use cases at once, pass in different values@ddt. DDTclassmycase (unittest. TestCase):#@ddt. Data (three-way) # 1, the function parameter is one and executes 3 use cases at the same time #def Testa (self,num): #self.asserttrue (num>2)@ddt. Data ([8,2],[1,3],[5,4],[6,4])#2, the function parameter is multiple, simultaneously executes 3 use cases@ddt. Unpack#multiple parameters, with unpack,[8,2] passed as two parameters defTestb (self,num,num2): self.asserttrue (num>num2)#def TESTC (self,num): #不加unpack时, [8,2] not decomposed, passed as a parameter #print (num) #print (' W ') #@ddt. File_data (' login.yml ') #@ddt. Unpack #def test_run (Self,**kwargs): #返回 file_data The value in the corresponding document, in the form of a list, lisg the value of each element is a dictionary #print (' This is Kwargs .... ', Kwargs) #" " #{' method ': ' Post ', ' data ': {' passwd ': ' aA123456 ', ' username ': '}, ' check ': [' sign ', ' userId '], #' url ': ', ' header ': {' content_type ': ' Application/json '}} #" " #method = Kwargs.get (' method ') #url = kwargs.get (' url ') #data = kwargs.get (' data ', {}) #Header = Kwargs.get (' header ', {}) #如果没有的话, default {} #Is_json = kwargs.get (' Is_json ', 0) #如果没有的话, default 0 #cookie = kwargs.get (' cookie ', {}) #如果没有的话, default {} #check = kwargs.get (' Check ', {}) #如果没有的话, default {} #if method = = ' Post ': #if Is_json: #r = requests.post (url,json= data,headers =header,cookies= cookie) #Else: #r = requests.post (url,data= data,headers =header,cookies= cookie) #print (R.text) #Else: #requests.get (url,params=data,headers=header,cookies= cookie) #params把url拼到请求里面 #self.assertequal (Check.get (' Error_code '), R.json (). Get (' Error_code ')) #只判断error_code ## for C in check: #第二种判断结果的方法, check the results for parameter values ## Print (c) ## Self.assertin (c,r.text) # ## for C in check: #第三种判断方式 to determine whether the value of each key in the request result equals the value of each key in the check ## for K, V in C.items (): ## self.assertequal (R.json (). Get (k), V)if __name__=='__main__': Unittest.main ()#Suite = UnitTest. TestSuite () #suite.addtests (Unittest.makesuite (mycase)) #result_html = Beautifulreport.beautifulreport (suite) #result_html.report (filename= ' Test report ', description= ' description ', log_path= '. ')
"Python" Learning note 10-DDT Data driven