Python unittest TestCase with inheritance

To make this work as expected, you minimally need to:

  • Make sure the init method of your subclasses test cases match that of TestCase, i.e. __init__(self, methodName="runTest")
  • Add a super call in the init method of your subclasses e.g. super(TestMyClass, self).__init__(methodName)
  • Add a self argument to test_get_result, i.e. def test_get_result(self):

As for whether it's good design, remember, your tests act in part as documentation for how your code is meant to work. If you've got all the work hidden away in TestCase instance state, what it does will not be as obvious. You might be better off, say, writing a mixin class that has custom assertions that take inputs and expected outputs.


The design is (more or less) fine -- the one "hiccup" is that when unittest looks at all TestCase classes and runs the methods that start with "test" on them. You have a few options at this point.

One approach would be to specify the class under test and values as attributes on the class. Here, if possible, you'll want the values to be immutable...

class TestBase(unittest.TestCase):

    def check(self, input, expected_output):
        obj = self.class_under_test(input)
        actual_output = obj.get_result()
        self.assertEqual(actual_output, expected_output)

    def check_all(self):
        for value in self.values:
            self.check(value[0], value[1])

class TestMyClass1(TestBase):
    values = ((1, 2), (3, 4))
    class_under_test = MyClass1

    def test_it(self):
        self.check_all()

class TestMyClass2(TestBase):
    values = (('a', 'b'), ('d', 'e'))
    class_under_test = MyClass2

    def test_it(self):
        self.check_all()

A little late here but recently came into the need to have unit test inheritence

The most elegant solution that I could find is this:

First - you need a base test class

class MyBaseUnitTest(unittest.TestCase):
    __test__ = False
    def test_someting(self):
        ...

    def test_something_else(self):
        ...

then to inherit that class and run tests:

class TestA(MyBaseUnitTest):
    __test__ = True

    def test_feature(self):
        pass
    def test_feature2(self):
        pass

This is the best, and easiset way to have a single viewset inheritence.

The issue I found with multiple inheritance is that when you try invoke methods like setUp() it will not be called on the base test class, so you have to call it in each class you write that extends the base class.

I hope that this will help somebody with this somewhere in the future.

BTW: This was done in python3 - I do not know how it will react in python2

UPDATE:

This is probably better and more pythonic

class MyBaseUnitTest(object):
    def test_someting(self):
        ...

    def test_something_else(self):
        ...

class TestA(MyBaseUnitTest, unittest.TestCase):

    def test_feature(self):
        pass
    def test_feature2(self):
        pass

So long as the base test class does not extend "unittest.TestCase", the test runner will not resolve these tests and they will not run in the suite. They will only run where the base class extends them.