0

I have two functions: function1 and function2 Is there a way to add them as a parameter in parameterized list and then mock somehow? What I want to do is to run test 'name1' with mocked function1 and test 'name2' with mocked function2 instead. Or the only way to write two separate tests?

    @parameterized.expand([('name1', 1, 2),
                           ('name2', 2, 2)])
    @patch('functions.function1')
    def test_data(self, _, input, expected_output, mock_func):
        # given
        mock_func.return_value = input
        # when
        output = do_something()
        # then
        self.assertEqual(output.count(), expected_output)
herder
  • 412
  • 2
  • 5
  • 16
  • That would be a misuse of parameterised tests even if you could do it (e.g. by moving the mocking _inside_ the test case). Those are two different contexts, they should be two different groups of tests. It's unclear what the context is, but probably you shouldn't be alternating the test doubles like that anyway - either they're collaborators, and their implementation is irrelevant in the current test scope, or they're not. – jonrsharpe Dec 30 '21 at 10:47
  • The function differ but output format is the same. And both tests (if will be written separately) will differ only by this one line in which it mock the different functions. Anyway @jonrsharpe, thanks for answer :) – herder Dec 30 '21 at 10:55

0 Answers0