DDT of interface test, pure code actual combat, learn

DDT, or data-driven testing, is simply understood as the change of data to drive the execution of automated tests, and ultimately cause changes in test results. Use external data sources to realize the parameterization of input and output and expected values, avoid using hard-coded data in the test.

One, install the ddt module

pip install ddt

ddt has four modes: import decorator @ddt; import data @data; split data @unpack; import external data @file_data

Second, the way of parameterization

1. Read the tuple data


#Be sure to use with the unit test framework 
import unittest,os from ddt import ddt,data,unpack,file_data 

''' NO.1Single set of elements''' 
@ddt 
class Testwork(unittest.TestCase): 

    @data(1, 2,3) 
    def test_01(self,value): #value is used to receive data 
        print(value) 
if __name__ =='__main__': 
    unittest.main() 
result: 
=>1 
  2 
  3 

'''NO.2 Multiple groups of undecomposed elements''' 
@ddt 
class Testwork(unittest.TestCase): 

    @data((1,2,3),(4,5,6)) 
    def test_01(self,value): 
        print(value) 

if __name__ =='__main__': 
    unittest.main() 
result: 
=>(1, 2, 3) 
  (4, 5, 6)

'''NO.3 Multi-group decomposition elements''' 
@ddt 
class Testwork(unittest.TestCase): 

    @data((1,2,3),(4,5,6)) 
    @unpack #Split data 
    def test_01 (self,value1,value2,value3): 
#Each set of data has 3 values, so set 3 formal parameters 
        print(value) 
if __name__ =='__main__': 
    unittest.main() 
result: 
=>1 2 3 
  4 5 6

2. Read list data

import unittest,os 
from ddt import ddt,data,unpack,file_data 

''' NO.1Multiple groups of elements are not decomposed''' 
@ddt 
class Testwork(unittest.TestCase): 

    @data([{'name':'lili' ,'age':12},{'sex':'male','job':'teacher'}]) 
    def test_01(self,a): 
        print(a) 

if __name__ =='__main__': 
    unittest.main () 
Result: 
=>[{'name':'lili','age': 12}, {'sex':'male','job':'teacher'}] 
※The above result can be seen: cannot be used To the requests data request, so it is not very practical ※ 


'''NO.2 Multi-group element decomposition''' 
@ddt 
class Testwork(unittest.TestCase): 

    @data([{'name':'lili','age' :12},{'sex':'male','job':'teacher'}])
    @unpack
    def test_01(self,a,b):
        print(a,b)

if __name__ =='__main__': 
    unittest.main() 
result: 
=>{'name':'lili','age': 12} {'sex':'male','job':'teacher'} 
※ The running result after splitting, without [], splitting is to split the 2 dictionaries in the list, so there are 2 data※

3. Read dictionary data

import unittest,os 
from ddt import ddt,data,unpack,file_data 

'''※The reading of the dictionary is special, because when splitting, the key value of the formal parameter and the actual parameter must be the same, otherwise an error will be reported ※''' 

'''NO.1 A single set of data has not been decomposed''' 
@ddt 
class Testwork(unittest.TestCase): 

    @data(('name':'lili','age':'16'),('sex': 'female','job':'nurser'}) 
    def test_01(self,a): 
        print(a) 

if __name__ =='__main__': 
    unittest.main() 
Result: 
=>{'name':'lili' ,'age': '16'} 
  {'sex':'female','job':'nurser'} 
※The result data of the above operation can be used as the request parameter of requests~! 


'''NO.2 Multi-data split, the focus is coming'''

    def test_01(self,name,age): 
        print(name,age) 

if __name__ =='__main__': 
    unittest.main() 
Result: 
=>lili 16 
  female nurser 
※The point is here: first the data displayed in the result is in the dictionary The value of the key is not printed; secondly, the data key value in @data must be consistent with the formal parameter name in the def method, otherwise, when printing, an inexplicable parameter error will be reported. 

4. Read json file data

# data_json.json文件
{
    "test_case1": {
        "key": "value1",
        "status_code": 200
    },
    "test_case2": {
        "key": "value2",
        "status_code": 200
    },
    "test_case3": {
        "key": "value3",
        "status_code": 200
    }
}


# python读取json文件
@ddt
class InterfaceTest(unittest.TestCase):

    def setUp(self):
        self.url = "http://httpbin.org/get"

    def tearDown(self):
        print(self.result)

    @file_data("data_json.json") 
    def test_get_request(self, key, status_code):#If
        r = requests.get(self.url, params={"key": key})#If
        self.result = r.json()
        self.assertEqual(r.status_code, status_code)

if __name__ == '__main__':
    unittest.main()


Guess you like

Origin blog.51cto.com/xqtesting/2561699