Search⌘ K
AI Features

Simple Examples of mock Module

Explore simple practical examples of Python's mock module to mimic class behavior and check method calls. Learn to use assertions like assert_called_once_with and assert_not_called to verify interactions, making your testing more reliable.

The Python mock class can mimic other Python class. This allows us to examine what methods were called on our mocked class and even what parameters were passed to them.

Simple examples of mock module

Let’s start by looking at a couple of simple examples that demonstrate how to use the mock module:

Python 3.5
from unittest.mock import Mock
my_mock = Mock()
my_mock.__str__ = Mock(return_value='Mocking')
print(str(my_mock))
#'Mocking'

In this ...