Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reuse the same tests to test different implementations?

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?

like image 510
alisianoi Avatar asked Apr 29 '15 09:04

alisianoi


Video Answer


1 Answers

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()
like image 124
quamrana Avatar answered Oct 29 '22 18:10

quamrana