In Python, UnitTest uses different combinations of parameters to produce a separate test case

Source: Internet
Author: User

When we use Python's unittest for automation or unit testing, sometimes a test case needs to be executed several times depending on the combination of input and output, but a use case in UnitTest can only have one set of parameter combinations, and if you use a circular approach, In the generated test report there will be only one test case statistics, the fact that we need a set of parameters to be counted as a test case, or the use case copy N (this way who can meet the demand, the single actually maintenance too cumbersome, if there is a change, need to copy n copies), So what should we do to achieve our needs? After looking for information and Baidu, we can use the Python setattr function at run time to produce test_ at the beginning of the test case method, the script is as follows:

Import unittestfrom Test import Test_supportclass mytestcase (unittest. TestCase):    def setUp (self):        pass    def Clear (self):        pass    def gettest (self, arg1, arg2): #定义的函数, Method of execution of the resulting test case        pass    @staticmethod       def gettestfunc (Arg1, arg2):        def func (self):            self.gettest ( Arg1, arg2)        return func        def __generatetestcases ():    arglists = [(' Arg11 ', ' Arg12 '), (' Arg21 ', ' Arg22 '), ( ' Arg31 ', ' arg32 ')]    for the args in arglists:        setattr (mytestcase, ' test_func_%s_%s '% (Args[0], args[1]), Mytestcase.gettestfunc (*args)) #通过setattr自动为TestCase类添加成员方法, the method begins with "Test_func_"
__generatetestcases () if __name__ = = ' __main__ ': Unittest.main ()

To this, we have completed the desired, on this basis can be increased parameterization, etc., to achieve the requirements of automated testing.

In Python, UnitTest uses different combinations of parameters to produce a separate test case

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.