I need to test a function which uses feature toggles to turn some functionality on and off. Say, a function like this:
def func_to_test(hello_value: str):
if toggle_is_active('only_hello_and_hi'):
if hello_value not in ('hello', 'hi'):
return
print(hello_value)
And now I want to test this function for both feature toggle states. For the off toggle I would do something like this:
class InactiveToggleTestCase(unittest.TestCase):
hello_values = ['hello', 'hi', 'bonjour', 'sup']
def test_func(self):
for hello_value in self.hello_values:
with self.subTest(hello_value=hello_value):
func_to_test(hello_value)
# assert print was called with expected values
And for the active state I want to just inherit the class and patch the toggle state:
@patch('toggle_is_active', lambda x: True)
class ActiveToggleTestCase(InactiveToggleTestCase):
hello_values = ['hello', 'hi']
This way I don't need to rewrite the test itself. The thing is, the parent class also gets the patch from the child class and test case for inactive toggle state doesn't pass anymore.
How can I avoid this effect? I could just duplicate the test, of course, but this doesn't seem to be right and also if there is a plenty of tests the inheritance would be really helpful.