Python mock patch a function called by another function

Python mock patch a function called by another function

When you want to mock or patch a function that is called by another function during testing in Python, you can use the unittest.mock library. Specifically, you can use the @patch decorator or the unittest.mock.patch context manager to temporarily replace the target function with a mock function. Here's how you can do it:

Suppose you have a module named my_module.py with the following functions:

# my_module.py def add(a, b): return a + b def calculate_sum(a, b): result = add(a, b) return result 

Now, let's say you want to mock the add function when testing the calculate_sum function. Here's an example using unittest.mock.patch as a context manager:

import unittest from unittest.mock import patch from my_module import calculate_sum class TestCalculateSum(unittest.TestCase): @patch('my_module.add') # Replace 'my_module.add' with the correct import path def test_calculate_sum(self, mock_add): # Configure the mock function mock_add.return_value = 42 # Call the function you want to test result = calculate_sum(10, 20) # Assert that the mock function was called with the correct arguments mock_add.assert_called_once_with(10, 20) # Assert the result self.assertEqual(result, 42) if __name__ == '__main__': unittest.main() 

In this example:

  1. We import the unittest library and the unittest.mock.patch function.

  2. We use the @patch decorator to mock the add function, specifying the import path to the function as a string.

  3. In the test method test_calculate_sum, we configure the mock_add mock function using mock_add.return_value.

  4. We call the calculate_sum function and assert that the mock function (add) was called with the expected arguments using mock_add.assert_called_once_with().

  5. Finally, we assert the result of the calculate_sum function.

Make sure to replace 'my_module.add' with the correct import path to the add function in your code.

This approach allows you to mock the add function specifically for testing the calculate_sum function while keeping the actual implementation of add intact.

Examples

  1. How to mock a function called by another function in Python?

    • Description: This query discusses how to mock a function that's called by another function, allowing control over its behavior in unit tests.
    from unittest import mock # Example outer function def outer_function(): return some_function() # This function will be mocked # Mock `some_function` using patch with mock.patch("__main__.some_function") as mocked_function: mocked_function.return_value = "Mocked Result" result = outer_function() # Calls `some_function` assert result == "Mocked Result" 
  2. How to patch a function called within a class method in Python?

    • Description: This query discusses patching a function that's called inside a class method.
    from unittest import mock class MyClass: def my_method(self): return external_function() # This function will be mocked # Patch `external_function` with mock.patch("__main__.external_function") as mocked_function: mocked_function.return_value = "Mocked" instance = MyClass() result = instance.my_method() assert result == "Mocked" 
  3. How to mock a function that's called within another module in Python?

    • Description: This query explores how to patch a function in one module that's called from another module.
    from unittest import mock from my_module import outer_function # Patch the function that `outer_function` calls with mock.patch("my_module.some_function") as mocked_function: mocked_function.return_value = "Mocked Result" result = outer_function() # Calls `some_function` assert result == "Mocked Result" 
  4. How to patch a function that's called within an if-statement in Python?

    • Description: This query discusses patching a function that's conditionally called within an if-statement.
    from unittest import mock def conditional_function(value): if value > 10: return check_high_value() # This function will be mocked return "Low Value" # Patch `check_high_value` with mock.patch("__main__.check_high_value") as mocked_function: mocked_function.return_value = "High Value" result = conditional_function(15) # Calls `check_high_value` assert result == "High Value" 
  5. How to mock a function with arguments that's called by another function in Python?

    • Description: This query explores patching a function with arguments that's called within another function.
    from unittest import mock def outer_function(x, y): return calculate_sum(x, y) # This function will be mocked # Patch `calculate_sum` with mock.patch("__main__.calculate_sum") as mocked_function: mocked_function.return_value = 10 result = outer_function(5, 5) # Calls `calculate_sum` assert result == 10 
  6. How to mock a function called within a loop in Python?

    • Description: This query discusses patching a function that's repeatedly called within a loop.
    from unittest import mock def loop_function(n): results = [] for _ in range(n): results.append(loop_iteration()) # This function will be mocked return results # Patch `loop_iteration` with mock.patch("__main__.loop_iteration") as mocked_function: mocked_function.side_effect = ["First", "Second", "Third"] result = loop_function(3) # Calls `loop_iteration` three times assert result == ["First", "Second", "Third"] 
  7. How to patch a function called by a method in a Python class?

    • Description: This query discusses patching a function that's called by a method in a Python class.
    from unittest import mock class MyClass: def method_with_external_call(self): return external_call() # This function will be mocked # Patch `external_call` with mock.patch("__main__.external_call") as mocked_function: mocked_function.return_value = "Patched" my_instance = MyClass() result = my_instance.method_with_external_call() # Calls `external_call` assert result == "Patched" 
  8. How to patch a function called by a decorator in Python?

    • Description: This query explores patching a function that's called as part of a decorator.
    from unittest import mock # A simple decorator that calls an external function def my_decorator(func): def wrapper(): before_call() result = func() after_call() # This function will be mocked return result return wrapper # Patch `after_call` with mock.patch("__main__.after_call") as mocked_function: mocked_function.return_value = "Mocked" @my_decorator def my_function(): return "Hello" result = my_function() # This calls `after_call` assert result == "Hello" 
  9. How to patch a function called by a utility function in Python?

    • Description: This query discusses patching a function called within a utility function.
    from unittest import mock def utility_function(data): return process_data(data) # This function will be mocked # Patch `process_data` with mock.patch("__main__.process_data") as mocked_function: mocked_function.return_value = "Processed" result = utility_function("Input Data") # Calls `process_data` assert result == "Processed" 

More Tags

polynomial-math google-app-engine string-aggregation encryption iequalitycomparer heap-memory emgucv language-agnostic spring-data-redis android-actionbar

More Python Questions

More Livestock Calculators

More Retirement Calculators

More Chemical thermodynamics Calculators

More Mortgage and Real Estate Calculators