I am developing multiple functions that answer a same problem but using different algorithm.
So the same input for all functions should generate the same output, that's why I wnted to use the same unit tests instead of having to create multiple tests with the same logic.
I was using the Python unittest framework, and I wanted to use an abstract test class to have the generic tests defined with a function variable so that I could just instantiate that generic function with the one I want to test in another normal test class. But it seems I can't instantiate the function variable in the child class.
So here is an example abstract class with generic tests for multiple functions.
class AbstractTestCase():
def test_generic_input_one(self):
result = self.function("input 1")
self.assertFalse(result)
def test_generic_input_two(self):
result = self.function("input 2")
self.assertTrue(result)
And here you would have a specific test class for the function_a that inherits the generic tests from the AbstractTestCase class and that implements its own.
class TestsFunctionA(AbstractTestCase, unittest.TestCase):
def setUp(self):
self.function = function_a
def test_specific_input(self):
result = self.assertTrue(self.function("specific input"))
self.assertTrue(result)
I am pretty sure it can be done, but I can't seem to find an example to see how to implement it. I would like to avoid code duplication.
What should be the simplest and best way to do it ?