I have a program with multiple very similar classes:
class BlackBox1():
def calc(self, a, b):
return a + b
class BlackBox2():
def calc(self, a, b):
return a * b
...
Now I want to write unittests for all those classes. Of course I could write separate tests for each Blackbox. Anyway, since each blackbox has exactly the same method calc(a, b)
to be tested, I wonder, if there is something like a “best practise”, to automatically give classes and expected results to a abstract test framework, something like
import unittest
class TestAbstractBox(unittest.TestCase):
def setUp(self):
self.box = blackbox()
self.param_a = a
self.param_b = b
self.expected_result = result
def test_calc_method(self):
real_result = self.box.calc(self.param_a, self.param_b)
self.assertEqual(real_result, self.expected_result,
"{0} gives wrong result".format(self.box.__class__))
TAbstractTest = unittest.defaultTestLoader.loadTestsFromTestCase(TestAbstractBox)
Is there a way to pass {"blackbox": Blackbox1, "a": 3, "b": 5, "result": 8}
and {"blackbox": Blackbox2, "a": 4, "b": 7, "result": 28}
to the TestAbstractBox class to not have multiple times the same code, but have a easy way to test new Blackboxes?