Adventures in Machine Learning

Python Mocking: The Ultimate Unit Testing Solution

Python Mocking: Your Ultimate Guide to Effortless Unit Testing

Are you tired of writing unit tests that take hours of your precious time and tons of energy? Do you want to replicate libraries easily without actually using them?

Python Mocking is the answer to all your testing woes!

Python Mocking is one of the most powerful and widely used testing frameworks in Python. It is a way of testing code without relying on external components, thereby making testing faster and easier.

The primary purpose of Python Mocking is to test a unit of code in isolation by simulating its external dependencies. In this article, we’ll dive into Python Mocking and explore its benefits, key objects in the unittest.mock class for mocking, and finally, the Mock() object and its dynamic nature.

Benefits of Using Python Mocking

Python Mocking makes testing a breeze by providing numerous benefits. Let’s take a look at some of them:

Time-saving: Traditional testing frameworks require you to create an environment that mimics the library you’re testing.

Python Mocking simplifies this process by allowing you to create a simulated environment for the tested code. Energy-saving: With Python Mocking, you can avoid repetitive and mundane tasks of setting up data and testing multiple scenarios.

This allows you to focus on your code and reduce the risk of human errors. Library Replication: With Python Mocking, you can simulate the behavior of libraries without actually using them.

This functionality makes it easier to isolate and test specific parts of your code and increase the speed and reliability of your tests. Key Objects in Unittest.Mock Class for Mocking

Python Mocking is powered by the unittest.mock class, which contains various objects used for mocking.

The three primary objects are the Mock(), MagicMock(), and patch() objects.

Mock() Object

The Mock() object is the most basic object in the unittest.mock class that allows you to create mocks of your own. It imitates the behavior of objects and can return any value you set it to.

You can add any attribute you want to the object, and it will work as if it’s the real object. The Mock() object has a dynamic nature, meaning it can be used in multiple ways.

It comes with different default arguments such as spec, spec_set, return_value, unsafe, side_effect, wraps, and name, which can be modified based on the functionalities you want to achieve. Use of

Mock() Object as a Library Substitute

Mock() object comes in handy when you want to replicate the behavior of libraries without having to use them.

For instance, if you’re testing a function that calculates the number of weekends in a given month using the datetime library, you can use the Mock() object to replicate the behavior of datetime, thereby making testing faster and more efficient. Demonstration of

Mock() Object with the datetime Library

Let’s take a deeper look at the example above to understand how the Mock() object can be used to test code without relying on external dependencies.

Suppose we have a function that calculates the number of weekends in a given month using the datetime library. def weekends_in_month(year, month):

date =, month, 1)

_, days_in_month = calendar.monthrange(year, month)

weekends = 0

for i in range(days_in_month):

if date.weekday() == 5 or date.weekday() == 6:

weekends += 1

date += datetime.timedelta(days=1)

return weekends

We can use the Mock() object to simulate the behavior of datetime by creating an instance of the datetime class and set it as the return_value of our Mock() object.

Here’s how it works:

import unittest

from unittest.mock import Mock

import datetime

class TestWeekendsInMonth(unittest.TestCase):

def test_weekends_in_month(self):

datetime = Mock() =, 12, 18)

weekend_counter = 0

year, month = 2022, 12 = [4] * 4 + [5] * 8 + [6] * 7

self.assertEqual(weekends_in_month(year, month), 7)

The assert keyword in Testing

Finally, in testing, you need to compare the expected output of your code with the actual output. Using the assert keyword, we can compare the two inputs and ensure that they’re equal.

If the test fails, the assert statement will raise an AssertionError, letting us know that we need to fix something.


In conclusion, Python Mocking is a powerful testing framework that saves users time and energy when running tests. It provides numerous benefits, such as library replication, and key objects in the unittest.mock class for mocking, such as Mock(), MagicMock(), and patch() objects.

The Mock() object allows you to create mocks of your objects, replicate libraries without using them, and demonstrate behavior with different scenario inputs. When testing with Python Mocking, use the assert keyword to ensure the expected output is equal to the actual output.

3) Side Effect in Mock() object

Python Mocking is incredibly powerful, allowing you to create objects that simulate the behavior of other objects dynamically. One of the essential functionalities of the Mock() object is the ability to create side effects.

These effects mimic various situations, such as exceptions, iterable values, function calls, and more. Let’s take a comprehensive look at the side effects in the Mock() object and how they can influence your testing:

Types of Side Effects in

Mock() Object

In Python Mocking, side effects refer to the outcome of a function’s execution when called for a specific time.

The Mock() object in unittest.mock provides several types of side effects:

An Exception: When a Mock() object is called, raising an exception-based on specific parameters. An Iterable: When a Mock() object behaves like iterable returns based on specific parameters.

A Function: The Mock() object will call and return based on specific parameters. Demonstration of Side Effects in

Mock() Object

To show how the side effect in

Mock() Objects work, let’s create two functions that use the Mock() object to simulate the behavior of external libraries:

TypeError Example

def plusone(x):

if type(x) != int:

raise TypeError(“Please enter an integer!!”)

return x + 1

def test_plusone():

mymock = Mock(side_effect = TypeError(“Bad South!!”))

with unittest.mock.patch(‘__main__.plusone’, new=mymock):

self.assertRaises(TypeError, plusone, “7”)

In the above function, we’re using the Mock() object to show that the plusone function raises a TypeError if x is not an integer. We set the side effect of the function to be a TypeError in case the input argument is not an integer.

Keypairs() Example

def keypairs(count):

key_values = []

for i in range(0, count):

key_value = (str(i), i * i)


return dict(key_values)

def test_keypairs():

mock_obj = Mock(side_effect=lambda x: str(x))

with unittest.mock.patch(‘__main__.keypairs’, new=mock_obj):

self.assertRaises(TypeError, keypairs, 3)

In the above example, we’re using the Mock() object to simulate the behavior of the key-value pair. Here, we’re returning a string in case of an exception.

4) Specifications in

Mock() Object

Another key feature of the Mock() object in Python Mocking is its capability to take a specification. A specification is an object that defines the attributes, methods, and properties of a class.

The spec attribute specifies the class whose behavior the Mock() object should mimic. Let’s take a closer look at this feature:

Description and Importance of Spec Attribute in

Mock() Object


Mock() Object, the spec attribute is a crucial feature that sets its behaviors according to an already existing object.

When providing a class’ specification, the Mock() object takes on similar behaviors and attributes as the object.

Explanation and Demonstration of Using Spec Attribute with a Class

To use the spec attribute in Python Mocking, let’s define a class with two methods:

class Dog():

def __init__(self, breed, bark):

self.breed = breed

self.bark = bark

def fetch(self):

return “Fetching..”

def sit(self):

return “Sitting..”

We can now build a Mock() object with this class’s specification, as shown below:

def test_mock_specification():

dog_mock = Mock(spec=Dog)

dog_mock.sit.return_value = “Mock Sitting..”

dog = Dog(“Labrador”, “Bark!”)

with unittest.mock.patch(‘__main__.Dog’, return_value=dog_mock):

self.assertEqual(dog_mock.sit(), “Mock Sitting..”)

In the above code, we’re using Mock() to create an instance of the Dog class and provide its behavior using the spec attribute. Here, we create a Mock() object with the class Dog specification and set the sit method to return the string ‘Mock Sitting..’.


Python Mocking is a powerful testing framework that allows developers to test code in isolation and simulate external dependencies. The Mock() object in Python Mocking is used to create mock versions of libraries and objects.

It also comes with useful features such as side effects and specifications that allow developers to simulate various behaviors. Understanding these features can help developers to create better and more reliable unit tests.



In this article, we’ve examined Python Mocking – a powerful unit testing framework that enables developers to test their code in isolation and simulate external dependencies. We’ve explored the benefits of using Python Mocking in unit testing and detailed the key objects in the unittest.mock class for mocking.

Moreover, we’ve delved into the Mock() object, its dynamic nature, side effects, and specifications. Python Mocking is an essential concept for Python development due to its crucial role in debugging.

It enables developers to test their code in meaningful, efficient, industry-accepted ways and detect issues before they become problems. In practice, this means that developers can avoid implementing faulty code in production, which can cause data breaches and other grave issues.

Instead, developers can use Python Mocking to accurately simulate different scenarios to ensure the code performs as expected. When using Python Mocking, it’s important to remember that it’s not a holistic solution to testing and debugging.

Instead, it’s aimed at making debugging faster and more efficient by isolating units in the development process. Additionally, it’s important to know that Python Mocking is not meant to replace real objects or libraries, as only the real implementation can validate the necessary complex features.

Furthermore, Python Mocking provides a dynamic and flexible way to simulate different scenarios in testing. You can use side effects to mimic the behavior of external dependencies, such as libraries and different objects.

You can also use specifications to simulate classes, methods, and attributes behaviors, enabling you to ascertain whether the code is behaving as expected. In essence, Python Mocking is a powerful tool that helps developers create reliable, high-quality software that meets user requirements and specifications.

It helps to ensure that your code behaves as expected in different scenarios and allows developers to save time and energy during the debugging process. Moreover, it empowers developers to create clean code while removing external dependences and increasing code modularity.

In conclusion, Python Mocking is an essential tool in the toolkit of every Python developer. It helps ensure the delivery of high-quality products, thereby increasing customer satisfaction.

Therefore, it’s essential for every Python developer to learn and master the skills required to use Mock() object in Python development, and to incorporate it into their development practices. Python Mocking is a vital testing framework for Python development, helping to save time and effort when debugging code.

By providing a way to simulate the behavior of external dependences and isolate specific units of code, Python Mocking helps ensure the delivery of high-quality software that meets user requirements. The Mock() object comes with various features that help to make testing more efficient and reliable, including side effects and specifications.

Python developers must learn and master these skills for unit testing effectively, and incorporate them into their development practices to produce better and more reliable code.

Popular Posts