Posts

Showing posts with the label unit test

Art of Unittest writing : Auto-generation of unit test cases

How to auto create test cases with minimum coding  Lets say , you want to run a series of test cases that undergo similar kind of testing  . but you need to generate different test cases for each comparison (or whatever test procedure you are using).

So imagine. We want to test a function. A function that accepts two variables, and an operator for comparison. Here's our test function:

def _test_func(self, a, b, exp, operator):
        ans = 0
        exec("ans = %d %s %d" % (a, operator, b))
        self.assertEqual(ans, exp)

We want to test above function with various inputs....And...we want each scenario as a test case...You might need it for test report or something...


importunittestfromcollectionsimportnamedtupleclassTestAuto(unittest.TestCase):defsetUp(self):passdeftearDown(self):pass# Adder function to add test casedef_add_test(cls,generator):forfunc,name,doc,a,b,exp,operatoringenerator():test=lambdaself,a=a,b=b,expected=exp,oper=operator,func=func:func(self,a,b,expec…