Skip to main content
Warning: You are using the test version of PyPI. This is a pre-production deployment of Warehouse. Changes made here affect the production instance of TestPyPI (
Help us improve Python packaging - Donate today!

Easy unit testing for HTTP requests

Project Description

Mockrequests: Easy unit testing for HTTP requests

Mockrequests is a Python 3 library for unit testing code that makes HTTP requests using the requests module. Simply monkey patch the imported requests module with mockrequests when unittesting and everything else is done for you. Mockrequests makes use of the unittest.mock module added in Python 3.3 so you will be able to take advantage of its functionality.


import unittest
from my_package import my_module
import mockrequests

class MyModuleTest(unittest.TestCase):
    def setUp(self):
        self.oldrequests = my_module.requests  # Make a backup of your requests import
        my_module.requests = mockrequests  # Replace the requests import with mockrequests

    def test_my_module(self):
        # blah blah

    def tearDown(self):
        my_module.requests = self.oldrequests  # Move the old requests back

Mockrequests will redirect all the HTTP request of your code to cached request objects that you set up before.


import requests
import mockrequests

>>> r = requests.get('')

# That's it. Now every time you try to access this url you will get the cached response.

>>> mockrequests.get('')
<Response [200]>

If the url that your code tries to reach is not static (i.e it contains a date) then you can pass a regex expression and mockrequests will return the first saved url that matches the regex.

>>> r = requests.get('')
>>>, regex='^.*google.*?')
>>> mockrequests.get('')
<Response [200]>

You can also pass the strict=True parameter and you will get a response only if the HTTP headers match.

Release History

This version
History Node


History Node


History Node


Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

File Name & Hash SHA256 Hash Help Version File Type Upload Date
(3.2 kB) Copy SHA256 Hash SHA256
Source Nov 3, 2016

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting