abstract test case using python unittest

I didn’t quite understand what do you plan to do —
the rule of thumb is “not to be smart with tests” –
just have them there, plain written.

But to achieve what you want, if you inherit from unittest.TestCase, whenever you call unittest.main() your “abstract” class will be executed – I think this is the situation you want to avoid.

Just do this:
Create your “abstract” class inheriting from “object”, not from TestCase.
And for the actual “concrete” implementations, just use multiple inheritance:
inherit from both unittest.TestCase and from your abstract class.

import unittest

class Abstract(object):
    def test_a(self):
        print "Running for class", self.__class__

class Test(Abstract, unittest.TestCase):
    pass

unittest.main()

update: reversed the inheritance order – Abstract first so that its defintions are not overriden by TestCase defaults, as well pointed in the comments bellow.

Leave a Comment