Python ddt achieves data-driven, and pythonddt implements data

Source: Internet
Author: User
Tags unpack

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.