I need to test the combination of two parameters, each having only a few possibilities. So I want to have a class hierarchy, where each subclass has its own fixed parameter A, while Parameter B can be tested based on that common A. I want to polymorph the test methods in the test case in terms of a shifting Parameter A, so that I don't have to rewrite the test methods for each subclass I test, but still can have variations in the subclasses' tests.
However, I found that if I declare the attribute in setUp(self). the attribute self.A does not get subclassed. Meaning that all my subclasses all have the same self.A value from the first test case (parent class). Also all my testSomething(self) methods are not virtual! Meaning with the following hierarchy:
class baseTest(unittest.TestCase):
def setUp(self):
print('base setup')
self.A = 100
def testSomething(self):
print('base test')
# Do something with self.A
class subTest(baseTest):
def setUp(self):
print('sub setup')
self.A = 999
def testSomething(self):
print('sub test')
# Do something with self.A
EDIT: I put both classes in one file, and relied on unittest.main() launched in if __name__ == "__main__"
Running this file gives me:
base setup
base test
base setup
base test
I thought all python methods are virtual, but this does not seem the case in unittest.
What should I do?