The unittest test in Python generates a separate test case solution based on different parameter combinations

Source: Internet
Author: User

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

Related 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.