0

I need to test two almost identical functions, and don't see much point copy-pasting the tests that I have written for function1. Can I somehow pass the function name to a class derived from unittest.TestCase and set up the function that needs to be tested in setUp?

0

1 Answer 1

1

There's no trick to this. Functions are first-class objects in Python and can be passed around just like any other parameter. So this would work, assuming the functions under test are 'func_one' and 'func_two':

class MyTestCase(unittest.TestCase):

    def generic_test(self, func):
        result = func()
        ... assertions about result....

    def test_func_one(self):
        self.generic_test(func_one)

    def test_func_two(self):
        self.generic_test(func_two)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.