Brief introduction
ddt (data driven test) data-driven test: set by the external data driven test case, a method for testing the same, but the situation requires a large amount of change in the test data, test data and the purpose is to separate the steps of
Since there is no data-driven unittest module, the main use of this library ddt, following installation
pip install ddt
ddt ddt comprising decorator three classes of common methods and decorators data (direct input test data), file_data (test data may be obtained from the json or yaml), unpack (decomposed data)
use
1. Several individual time data
Import unittest Import ddt @ ddt.ddt # Before testing the use of the class definition: @ ddt.ddt class Mytest (unittest.TestCase): @ ddt.data ( 1,2,3,4) # use before the test case definition: @ ddt.data (test data) separated by commas test data DEF TEST_1 (Self, A): Print (A) IF the __name__ == " __main__ " : unittest.main ()
result
1 .2 .3 .4 . ---------------------------------------------------------------------- Ran 4 tests in 0.013s OK
2. When the data set is a list, split into individual elements
Import the unittest Import DDT value = [(1,2), (3,4-), (5,6 )] @ ddt.ddt # used in the test before the class definition: @ ddt.ddt class the Mytest (of unittest.TestCase): @ ddt.data ( * value) # before the test case definitions used: @ ddt.data (test data), the parameter * python is decomposed into a list element in turn passed DEF TEST_1 (Self, a): Print (A) IF __name__ == " __main__ " : unittest.main ()
result
(1, 2) .(3, 4) .(5, 6) . ---------------------------------------------------------------------- Ran 3 tests in 0.004s OK
If you want the list above which tuples decomposed into individual elements, used unpack
Import the unittest Import DDT value = [(1,2), (3,4-), (5,6 )] @ ddt.ddt # used in the test before the class definition: @ ddt.ddt class the Mytest (of unittest.TestCase): @ ddt.data ( * value) # before using the test case definition: @ ddt.data (test data), parameter * python is decomposed into a list of elements sequentially passed @ ddt.unpack # take separate value [1], is decomposed into 1,2 incoming DEF TEST_1 (Self, A, B): Print (A, B) IF the __name__ == " __main__ " : unittest.main ()
result
1 2 .3 4 .5 6 . ---------------------------------------------------------------------- Ran 3 tests in 0.010s OK
3. The data dictionary is a set time
import unittest import ddt value = {"a":1,"b":2} @ddt.ddt class Mytest(unittest.TestCase): @ddt.data(value) @ddt.unpack def test_1(self,a,b): print(a,b) if __name__ == "__main__": unittest.main()
result
1 2 . ---------------------------------------------------------------------- Ran 1 test in 0.004s OK
4. Use json file or yaml
json file
{ "test1":1, "test2":"abc", "test3":[1,2,3] }
Code
import unittest import ddt @ddt.ddt class Mytest(unittest.TestCase): @ddt.file_data("tmp.json") def test_1(self,a): print(a) if __name__ == "__main__": unittest.main()
result
1 .abc .[1, 2, 3] . ---------------------------------------------------------------------- Ran 3 tests in 0.014s OK