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