Python Learning note 10--unittest parameterization

Source: Internet
Author: User

When we write case, if the operation of use cases is the same, the parameters are different, for example, to test a login interface, to test the normal landing, the blacklist user login, account password error, and so on, in the UnitTest will write a number of case to test.

This is the case just call the interface when the parameters are not the same, and then write more than one case is a bit redundant, how to do it, you have to write these parameters to a list inside, and then loop to execute the case. This saves you from having to write more than one case.

Of course, there is a third-party module directly has such a function, do not need us to write the loop ourselves. That is nose-parameterized, direct PIP installation can be.

Pip Install nose-parameterized

Here is the code

Import Unittestfrom nose_parameterized Imports parameterized# This module class My (UnitTest. TestCase):     @parameterized. Expand ([(+), (), (), (            1,2,4)         ]    ) # Decorate this function with the adorner provided by it, put the 4 data we wrote into this list    def test1 (self,a,b,c):        self.assertequal (a+b,c) if __name__== ' __main_ _ ':    unittest.main ()

Here is the result of the operation, we only run it once, we can find that it automatically helped us to run 4 times.

  

  

Python Learning note 10--unittest parameterization

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.