Python ddt achieves data-driven, and pythonddt implements data
Ddt is a third-party module and must be installed. pip install ddt
DDT contains the class decorator ddt and two methods decorator data (directly input the test data)
Generally, data in data is transmitted to the test case according to one parameter. If data contains multiple data, such as tuples, lists, and dictionaries, data must be decomposed in the script or unpack.
@ Data (a, B)
Then use cases a and B run each time.
@ Data ([a, d], [c, d])
If no @ unpack exists, [a, B] is used as a parameter to pass in the use case for running.
If @ unpack exists, [a, B] is decomposed and passed according to the two parameters in the use case.
For more information, see the following example:
import unittestfrom ddt import ddt,data,unpack@ddtclass MyTesting(unittest.TestCase): def setUp(self): print('this is the setUp') @data([1,2,3]) def test_1(self,value): print(value) @data([3,2,1],[5,3,2],[10,4,6]) @unpack def test_minus(self,a,b,expected): actual = int(a) - int(b) expected = int(expected) self.assertEqual(actual, expected) @data([2,3],[4,5]) def test_compare(self,a,b): self.assertEqual(a,b) def tearDown(self): print('this is tearDown')if __name__ == '__main__': unittest.main(verbosity=2)
Result Analysis:
1. The test result of test_1 is OK, because [1, 2, 3] is passed as a whole to the value. The printed values of all values are [1, 2, 3].
test_1_1__1__2__3_ (__main__.MyTesting) ... oktest_compare_1__2__3_ (__main__.MyTesting) ... ERROR[1, 2, 3]
2. The test_minus test result is also OK. Because @ unpack is added under @ data (...), it means that the data is decomposed and three groups of test data are obtained:
1.[3,2,1]2.[5,3,2]3.[10,4,6]test_minus_1__3__2__1_ (__main__.MyTesting) ... oktest_minus_2__5__3__2_ (__main__.MyTesting) ... oktest_minus_3__10__4__6_ (__main__.MyTesting) ... ok
3. the test_compare test result is fail. Because no @ unpack is added, although it will still be understood as two groups of test data, [2, 3] is passed as a whole to, because B has no value passed in, a TypeError: test_compare () missing 1 required positional argument: 'B' is reported after execution.
test_compare_1__2__3_ (__main__.MyTesting) ... ERRORtest_compare_2__4__5_ (__main__.MyTesting) ... ERROR
this is the setUpERROR: test_compare_1__2__3_ (__main__.MyTesting)this is tearDown----------------------------------------------------------------------Traceback (most recent call last): File "D:\python\lib\site-packages\ddt.py", line 139, in wrapper return func(self, *args, **kwargs)TypeError: test_compare() missing 1 required positional argument: 'b'======================================================================ERROR: test_compare_2__4__5_ (__main__.MyTesting)----------------------------------------------------------------------Traceback (most recent call last): File "D:\python\lib\site-packages\ddt.py", line 139, in wrapper return func(self, *args, **kwargs)TypeError: test_compare() missing 1 required positional argument: 'b'
The above is the learning Summary of ddt, and the driving method of ddt and file_data (which can obtain test data from json or yaml) will continue in the next article.