Skip to the content.

Patch

In simple words, patch() will temporarily change the object referenced with another one (default: MagicMock object).

Don’t know what a MagicMock or a mock object is? Come here for a brief explanation of the mock object.

Patch can be used in three ways:

Example

def foo():
    pass

Patch as context manager

from unittest.mock import patch

# __main__.foo is the reference of method foo
with patch('__main__.foo') as foo_mock:
    foo_mock.return_value = 3
    print(foo_mock())
    print(foo())

Patch as decorator of another function

from unittest.mock import patch

# __main__.foo is the reference of method foo
@patch('__main__.foo')
def fake_mock(foo_mock):
    foo_mock.return_value = 3
    print(foo_mock())
    print(foo())

fake_mock()

Straight-forward patch

from unittest.mock import patch

# __main__.foo is the reference of method foo
patcher = patch('__main__.foo')
foo_mock = patcher.start()
foo_mock.return_value = 3
print(foo_mock())
print(foo())
patcher.stop()

Parameter Target

It’s time to understand the __main__.foo of patch('__main__.foo') from the example above.

This is where the object is looked up. More details in the official doc on [where to patch][]

I believe this is enough for most of your tests. Wanna know more about patch? Come to the advanced patch tutorial

Be patient

Go to