Advertisement
  1. Code
  2. Python

Introduction to Mocking in Python

Scroll to top
5 min read

Mocking is a library for testing in Python. It allows you to replace parts of your system under test with mock objects and make assertions about how they have been used. This tutorial will discuss in detail what mocking is and how to use it in Python applications.

What Is Mocking?

Mocking is a library for testing in Python which allows you to replace parts of your system under test with mock objects and make assertions about how they have been used.

In Python, mocking is accomplished by replacing parts of your system with mock objects using the unittest.mock module. This module contains a number of useful classes and functions, namely the patch function (as decorator and context manager) and the MagicMock class. These two components are very important in achieving mocking in Python.

A mock function call usually returns a predefined value immediately.  A mock object's attributes and methods are defined in the test as well, without creating the real object.

Mocking also allows you to return predefined values to each function call when writing tests. This allows you to have more control when testing.

Prerequisites

Mock is available in Python 3, but if you are using a Python version below
3.3, you can still use unittest.mock by importing it as a separate library like so.

1
$ pip install mock

Benefits of Mocking

Some of the benefits of mocking include:

  1. Avoiding Too Many Dependencies. Mocking reduces the dependence of functions. For instance, if you have a function A class that depends on a function B, you will need to write a few unit tests covering the features provided by function B. Let's say the code grows in future and you have more functions, i.e. A depends on B, B depends on C, and C depends on D. If a fault is introduced in Z, all your unit tests will fail.
  2. Reduced overload. This applies to resource-intensive functions. A mock of that function would cut down on unnecessary resource usage during testing, therefore reducing test run time.
  3. Bypass time constraints in functions. This applies to scheduled activities. Imagine a process that has been scheduled to execute every hour. In such a situation, mocking the time source lets you actually unit test such logic so that your test doesn't have to run for hours, waiting for the time to pass.

Usage

Usage of mock is simple as:

1
>>> from mock import Mock
2
>>> mock = Mock(return_values = 10)
3
>>> mock(1,4,foo ='bar')
4
<Mock name='mock()' id='140305284793040'>
5
>>> mock.return_values
6
10

Here, we import the mock module, create a mock object, and specify return values. When the mock object is called, we want it to be able to return some values. In our case, we want the mock object to return a value of 10. If we call the mock object with the arguments (1, 4, foo ='bar'), the result will be the value 10, which was defined as a return value.

You can also raise exceptions inside mocks as follows:

1
>>> mock = Mock(side_effect=KeyError('foobar'))
2
>>> mock()
3
Traceback (most recent call last):
4
 ...
5
KeyError: 'foobar'

The side_effects argument allows you to perform certain things like raising an exception when a mock is called.

Example

Consider this simple function:

1
import requests
2
3
4
def api():
5
    response = requests.get('https://www.google.com/')
6
    return response

This function performs an API request to the Google webpage and returns a response.

The corresponding simple test case will be as follows:

1
import unittest
2
from main import api
3
4
5
class TetsApi(unittest.TestCase):
6
7
    def test_api(self):
8
        assert api() == 200

Running the above test should give an output like so:

1
----------------------------------------------------------------------
2
Ran 1 test in 3.997s
3
4
OK

Let's introduce mocking to this example, and the resulting test with the Mock module will be as shown below:

1
import unittest
2
from mock import Mock
3
from mock import patch
4
import requests
5
import unittest
6
7
8
9
class TetsApi(unittest.TestCase):
10
11
    def test_api(self):
12
        with patch.object(requests, 'get') as get_mock:
13
            get_mock.return_value = mock_response = Mock()
14
            mock_response.status_code = 200
15
            assert api() == 200

Running the above test should give an output like so:

1
----------------------------------------------------------------------
2
Ran 1 test in 0.001s
3
4
OK

As seen above, the mocking module takes less time to make the same API call as the normal test case.

Larger Example

Let's assume you have a script that interacts with an external API and makes calls to that API whenever a certain function is called. In this example, we are going to use the Twitter API to implement a Python script which will post to the Twitter profile page.

We don't want to post messages on Twitter every time we test the script, and that's where Mocking comes in.

Let's get started. We will be using the python-twitter library, and the first thing we will do is create a folder python_mock and, inside the folder, create two files, namely tweet.py and mock_test.py.

Write the following code to the file tweet.py.

1
# pip install python-twitter

2
import twitter
3
4
# define authentication credentials

5
consumer_key = 'iYD2sKY4NC8teRb9BUM8UguRa'
6
consumer_secret = 'uW3tHdH6UAqlxA7yxmcr8FSMSzQIBIpcC4NNS7jrvkxREdJ15m'
7
access_token_key = '314746354-Ucq36TRDnfGAxpOVtnK1qZxMfRKzFHFhyRqzNpTx7wZ1qHS0qycy0aNjoMDpKhcfzuLm6uAbhB2LilxZzST8w'
8
access_token_secret = '7wZ1qHS0qycy0aNjoMDpKhcfzuLm6uAbhB2LilxZzST8w'
9
10
11
def post_tweet(api, tweet):
12
    # post tweet

13
    status = api.PostUpdate(tweet)
14
    return status
15
16
17
def main():
18
19
    api = twitter.Api(consumer_key=consumer_key,
20
                      consumer_secret=consumer_secret,
21
                      access_token_key=access_token_key,
22
                      access_token_secret=access_token_secret)
23
24
    message = raw_input("Enter your tweet :")
25
26
    post_tweet(api, message)
27
28
29
if __name__ == '__main__':
30
    main()

In the code above, we first import the Twitter library and then define the authentication credentials, which you can easily get from the Twitter Apps page.

The Twitter API is exposed via the twitter.Api class, so we create the class by passing our tokens and secret keys.

The post_tweet function takes in an authentication object and the message and then posts the tweet to the Twitter profile.

We then go ahead and mock the API call to Twitter so that the API doesn't post to Twitter every time it is called. Go ahead and open the mock_test.py file and add the following code.

1
# mock_test.py

2
3
#!/usr/bin/env python

4
import unittest
5
from mock import Mock
6
7
8
import tweet
9
10
11
class TweetTest(unittest.TestCase):
12
13
    def test_example(self):
14
        mock_twitter = Mock()
15
        tweet.post_tweet(
16
            mock_twitter, "Creating a Task Manager App Using Ionic: Part 1")
17
        mock_twitter.PostUpdate.assert_called_with(
18
            "Creating a Task Manager App Using Ionic: Part 1")
19
20
21
if __name__ == '__main__':
22
    unittest.main()

Running the above test should give an output like so:

1
----------------------------------------------------------------------
2
Ran 1 test in 0.001s
3
4
OK

Conclusion

This tutorial has covered most of the fundamentals of mocking and how to use mocking to perform external API calls. For more information, visit the official Python mocking documentation. You can also find additional resources on authentication with the Twitter API in this tutorial.

Additionally, don’t hesitate to see what we have available for sale and for study in the Envato Market, and please go ahead and ask any questions and provide your valuable feedback using the feed below.

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.