In some cases, you need to test the same behavior with different combinations of parameters, and you want to know what to test from the execution result of the test case, not just a big test case; Then one of them is hard to see in the test results except for the error. the crux of the problem is whether there is a way to generate the corresponding test case according to the different combinations of input parameters, for example, if you have 10 sets of data, you get 10 test case, of course, it is not necessary to write so many Test_ member functions in a purely manual way. One possible idea is not to use UnitTest. TestCase the method of Test_ member functions in this class box, instead of writing runtest This member function, there will be some extra work, and it doesn't seem so "smart". So how do you get the framework to call TestCase automatically? Our train of thought is:
- Using SetAttr to add member functions to an existing testcase class
- In order for this method to work, the class's static method is used to generate the member function of the decorate class, and the function returns a test function object out
- Register this call to add the test member function somewhere (just before you actually do it, you can do it automatically in the module or you can call it manually)
code example:
Import UnitTest
From test import Test_support
Class Mytestcase (UnitTest. TestCase):
def setUp (self):
#some Setup Code
Pass
def clear (self):
#some Cleanup Code
Pass
def action (self, Arg1, arg2):
Pass
@staticmethod
def gettestfunc (Arg1, arg2):
def func (self):
Self.action (Arg1, arg2)
return func
Def __generatetestcases ():
arglists = [(' Arg11 ', ' Arg12 '), (' Arg21 ', ' Arg22 '), (' arg31 ', ' arg32 ')]
For args in arglists:
SetAttr (mytestcase, ' test_func_%s_%s '% (Args[0], args[1]),
Mytestcase.gettestfunc (*args))
__generatetestcases ()
if __name__ = = ' __main__ ':
#test_support. Run_unittest (Mytestcase)
Unittest.main ()
The unittest test in Python generates a separate test case solution based on different parameter combinations