I am trying to mock a python function similar to below. I am not doing anything with the mocked function except for the fact that it is used to return the mocked data in the called function. Is it possible for me to avoid passing in the variable (sum, in this case) to the test function?
# test_calculator.py
from unittest import TestCase
from unittest.mock import patch
class TestCalculator(TestCase):
@patch('calculator.Calculator.sum', return_value=9)
def test_sum(self, sum):
self.assertEqual(sum(2,3), 9)
patchit seems to expect classes as inputs, not functions. Am I missing something?9. That is, you've written a more verbose form ofself.assertEqual(9, 9).newargument, then you have to do it as above. If you specify thenewargument (presumably pointing to another class or function) then you can skip the second argument in the decorated function signature. I do not know if you can specify such a new function on the spot via a lambda though.