Showing posts from December, 2015

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…

Extract more information from your Python unittests

Hi guys, I know you love to write your unittest with python unittest module since its so easy and fun .
But sometimes we need to have a little more information than usual. This article is for those who want to use the powers of unittest module to get better control. Have more decision variables to play with.

I am going to use the below test script for reference.

importunittestclassTestExample(unittest.TestCase): defsetUp(self): = 'Python'deftest_title(self): self.assertTrue(, 'Name not a title') deftest_string(self): self.assertTrue(isinstance(, str), ' variable is not a string') deftest_capital(self): self.assertEqual(,, 'Name not in capital')

Case 1:  Find number of test cases in your test script.

if __name__ == '__main__': # Creating a TestSuite object test_suite = unittest.TestLoader().loadTestsFromTestCase(TestExam…