Python Test inheritance with multiple subclasses
Question: I would like to write a Python test suite in a way that allows me to inherit
from a single TestBaseClass and subclass it multiple times, everytime changing
some small detail in its member variables.

Something like:

    
    
    import unittest
    
    class TestBaseClass(unittest.TestCase):
    
      def setUp(self):
        self.var1 = "exampleone"
    
    class DetailedTestOne(TestBaseClass):
      def setUp(self):
        self.var2 = "exampletwo"
    
      def runTest(self):
        self.assertEqual(self.var1, "exampleone")
        self.assertEqual(self.var2, "exampletwo")
    
    class DetailedTestOneA(DetailedTestOne):
      def setUp(self):
        self.var3 = "examplethree"
    
      def runTest(self):
        self.assertEqual(self.var1, "exampleone")
        self.assertEqual(self.var2, "exampletwo")
        self.assertEqual(self.var3, "examplethree")
    
    ... continue to subclass at wish ...
    

In this example, DetailedTestOne inherits from TestBaseClass and
DetailedTestOneA inherits from DetailedTestOne.

With the code above, I get:

    
    
    AttributeError: 'DetailedTestOne' object has no attribute 'var1'
    

for DetailedTestOne and:

    
    
    AttributeError: 'DetailedTestOneA' object has no attribute 'var1'
    

for DetailedTestOneA

Of course, var1, var2, var3 could be some members of a same variable declared
in first instance in the TestBaseClass.

Any ideas on how to achieve such behaviour?


Answer: You need to call the superclass implementation in your subclasses by doing,
e.g., `super(DetailedTestOne, self).setUp()` from inside your
`DetailedTestOne.setUp` method.

