Search code examples
pythonunit-testingtesting

Python unit test with base and sub class


I currently have a few unit tests which share a common set of tests. Here's an example:

import unittest

class BaseTest(unittest.TestCase):

    def testCommon(self):
        print 'Calling BaseTest:testCommon'
        value = 5
        self.assertEquals(value, 5)

class SubTest1(BaseTest):

    def testSub1(self):
        print 'Calling SubTest1:testSub1'
        sub = 3
        self.assertEquals(sub, 3)


class SubTest2(BaseTest):

    def testSub2(self):
        print 'Calling SubTest2:testSub2'
        sub = 4
        self.assertEquals(sub, 4)

if __name__ == '__main__':
    unittest.main()

The output of the above is:

Calling BaseTest:testCommon
.Calling BaseTest:testCommon
.Calling SubTest1:testSub1
.Calling BaseTest:testCommon
.Calling SubTest2:testSub2
.
----------------------------------------------------------------------
Ran 5 tests in 0.000s

OK

Is there a way to rewrite the above so that the very first testCommon is not called?

EDIT: Instead of running 5 tests above, I want it to run only 4 tests, 2 from the SubTest1 and another 2 from SubTest2. It seems that Python unittest is running the original BaseTest on its own and I need a mechanism to prevent that from happening.


Solution

  • Use multiple inheritance, so your class with common tests doesn't itself inherit from TestCase.

    import unittest
    
    class CommonTests(object):
        def testCommon(self):
            print 'Calling BaseTest:testCommon'
            value = 5
            self.assertEquals(value, 5)
    
    class SubTest1(unittest.TestCase, CommonTests):
    
        def testSub1(self):
            print 'Calling SubTest1:testSub1'
            sub = 3
            self.assertEquals(sub, 3)
    
    
    class SubTest2(unittest.TestCase, CommonTests):
    
        def testSub2(self):
            print 'Calling SubTest2:testSub2'
            sub = 4
            self.assertEquals(sub, 4)
    
    if __name__ == '__main__':
        unittest.main()