Currently, I have many similar tests for testing. Each TestCase contains both data (input values ββ+ expected output values) and logic (call SUT and compare the actual output with the expected output).
I would like to separate the data from the logic. So I want a base class that contains only logic and a derived class that contains only data. I came up with this so far:
import unittest class MyClass(): def __init__(self, input): self.input = input def get_result(self): return self.input * 2 class TestBase(unittest.TestCase): def check(self, input, expected_output): obj = self.class_under_test(input) actual_output = obj.get_result() self.assertEqual(actual_output, expected_output) def test_get_result(self): for value in self.values: self.check(value[0], value[1]) class TestMyClass(TestBase): def __init__(self, methodName='runTest'): unittest.TestCase.__init__(self, methodName) self.class_under_test = MyClass self.values = [(1, 2), (3, 6)] unittest.main(exit = False)
But this fails with the following error:
AttributeError: 'TestBase' object has no attribute 'values'
Two questions:
- Is my βdesignβ good?
- What else is needed to make it work?
source share