My language is python 3
and I am trying to understand how to use the same set of tests to test different implementations of the same algorithm.
So far I am looking at the built-in unittest
and my impression is that I have to make some sort of a class hierachy: a class that inherits from unittest.TestCase and implements all the actual tests and several descendants from its class that each test particular implementations with the tests from the parent.
However, this is just my idea of how it should look like. Could you please tell me how to actually use the same set of tests to test different functions that implement the same algorithm?
I've done this by writing tests in a class that does not inherit from TestCase
and then writing multiple test classes that do inherit from TestCase
and the tests, but either have a factory method or a class attribute of the class to instantiate:
class TestAnyImplementation:
def testThis(self):
obj = getImpl() # call to factory method
self.assertTrue(obj.isEmpty())
class TestFooImplementation(TestAnyImplementation,TestCase):
def getImpl(self):
return Foo()
class TestBarImplementation(TestAnyImplementation,TestCase):
def getImpl(self):
return Bar()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With