Open In App
Related Articles

Python | Unit Test Objects Patching | Set-1

Improve Article
Save Article
Like Article

The problem is writing unit tests and need to apply patches to selected objects in order to make assertions about how they were used in the test (e.g., assertions about being called with certain parameters, access to selected attributes, etc.).

To do so, the unittest.mock.patch() function can be used to help with this problem. It’s a little unusual, but patch() can be used as a decorator, a context manager, or stand-alone.

Code #1: Using unittest.mock.patch as a decorator

from unittest.mock import patch
import example
def test1(x, mock_func):
    # Uses patched example.func

Code #2: Using unittest.mock.patch as a decorator

with patch('example.func') as mock_func:

Code #3: Using unittest.mock.patch to patch things manually.

p = patch('example.func')
mock_func = p.start()

Code #4: Stacking decorators and context managers to patch multiple objects

def test1(mock1, mock2, mock3):
def test2():
    with patch('example.patch1') as mock1, \
    patch('example.patch2') as mock2, \
    patch('example.patch3') as mock3:

patch() works by taking an existing object with the fully qualified name that you provide and replacing it with a new value. The original value is then restored after the completion of the decorated function or context manager. By default, values are replaced with MagicMock instances.
Code #5 : Example

x = 42
with patch('__main__.x'):
print (x)

Output :

<MagicMock name='x' id='4314230032'>

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 12 Jun, 2019
Like Article
Save Article
Similar Reads
Complete Tutorials