How Can We Mock Functions That Are Returned by Higher-Order Functions in Python?

What will you learn?

In this comprehensive guide, you will delve into the world of mocking functions that are returned by higher-order functions in Python. By mastering this technique, you will enhance your unit testing capabilities and streamline your testing processes.

Introduction to the Problem and Solution

When working with higher-order functions in Python�functions that either take other functions as arguments or return them�it can sometimes be challenging to test the interactions between these nested or returned functions, especially when they have side effects or interact with external systems. The solution lies in mocking. By mocking the function returned by a higher-order function, you gain control over its behavior during tests, allowing you to focus on the logic of your main function without worrying about dependencies.

To achieve this, we will utilize the unittest.mock module provided by Python’s standard library. This module offers exceptional flexibility and control for creating mock objects, enabling us to simulate any object behavior necessary for our tests. Specifically, we’ll employ the patch decorator/function from this module to replace our target function with a mock version at runtime. This approach ensures that our tests remain concise and concentrate on verifying that our higher-order function behaves as expected under various conditions.


from unittest.mock import patch

def my_higher_order_function():
    def inner_function():
        # Original behavior of the inner function.
        return "original value"
    return inner_function

def test_my_higher_order_function(mock_inner_function):
    # Configure the mock to return a specific value.
    mock_inner_function.return_value = "mocked value"

    result = my_higher_order_function()()

    assert result == "mocked value", "The result should be mocked."

# Copyright PHD


In this example, we define my_higher_order_function, which returns an inner_function. The objective is to test how my_higher_order_function behaves when its returned inner_function is externally controlled (i.e., mocked).

We employ the @patch decorator from unittest.mock, specifying ‘__main__.my_higher_order_function.return_value’ as its argument. This instructs Python that whenever the return value of my_higher_order_function() (which actually calls inner_functions) is invoked within our test scope (test_my_higher_order_function), it should instead invoke our mocked version of that inner function.

By setting .return_value = “mocked value” on our mock object (mock_inner_fucntion), we dictate what data it should return when called; thereby simulating an alternative outcome of calling that inner function without modifying its actual implementation�an invaluable asset for easily testing diverse scenarios.

Subsequently, invoking result = my_higerhorderfunction()() executes our higher order’s logic (now returning and executing a mocked version of inner_fuction). We then verify whether this manipulation produces the expected outcomes (“mocked value”), ensuring seamless integration of high-level functionalities even with altered internal behaviors.

    What is mocking in software testing?

    Mocking involves replacing real objects or functions within your application code with simulated versions that mimic their behavior while allowing full control over their outputs during testing phases.

    Why do we need mocks?

    Mocks are crucial for isolating sections of code being tested from external dependencies or unpredictable behaviors�facilitating more reliable and deterministic unit tests.

    What�s a higher-order function?

    A higher-order function either takes one or more functions as arguments or returns another function altogether.

    Can I only mock functions using unittest.mock?

    No! The unittest.mock module allows you to mock not only functions but also objects’ methods and attributes among others�it�s quite versatile!

    Does patching work on instance methods too?

    Yes! Using patch.object(), you can replace instance methods just as easily as standalone functions.

    Is it possible �to stack multiple patches?

    Certainly! You can apply multiple @patch decorators atop your test functions if you need several mocks within one test case scenario.


    Embracing mocking techniques in unit testing provides a sophisticated approach to validating components� reliability across various scenarios without directly altering their source codes or heavily relying on external system responses every time. Particularly beneficial when dealing with intricate structures such as those involving higher-order functions where traditional direct influence seems less feasible�the approaches outlined above serve as practical and efficiency-driven assets toward achieving cleaner and more maintainable codebases moving forward.

    Leave a Comment