I have been trying to test a function that calls another function with some parameters. I am trying to mock the latest so that it won't actually run and instead executes a mock function that returns some mock values.
What I have -simplified- looks like that:
def function_to_test():
a = 2
b = 3
c = 4
results = second_function(a, b, c)
return results
Then the function that I am trying to mock looks like that:
def second_function(a, b , c):
a = b + c
return a
Both function_to_test
and second_function
belong to the class Example
.
I am using unittest
for my tests and I cannot switch to pytest unfortunatelly, so no pytest options are helpful.
What I have managed to do so far with the test is:
@patch('rootfolder.subfolder.filename.Example.second_function', autospec=True)
def test_function_to_test(self, get_content_mock):
get_content_mock.return_value = mocked_second_function()
res = function_to_test()
self.assertEqual(res, 10)
As you can see I am trying to use a mocked function instead the actual second_function
that looks like that:
def mocked_second_function(a, b, c):
# using a, b, c for other actions
# for the question I will just print them but they are actually needed
print(f"{a}, {b}, {c}")
return 10
The problem is that when I set the get_content_mock.return_value = mocked_second_function()
.
I am required to pass the parameters, but in my actual problem, these parameters are being generated at the function_to_test
so I have no way of knowing them beforehand.
I read many related questions and documentation but I cannot seem to find something that helps my problem. Any help or even a different approach would be helpful.