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 (testpypi.python.org).
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.

Usage

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.

Setup

import requests
import mockrequests

>>> r = requests.get('http://www.somewebsite.com/')
>>> mockrequests.save(r)

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

>>> mockrequests.get('http://www.somewebsite.com/')
<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('http://www.google.com/')
>>> mockrequests.save(r, regex='^.*google.*?')
>>> mockrequests.get('http://www.google.co.uk/')
<Response [200]>

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

Release History

Release History

This version
History Node

0.1.2

History Node

0.1.1

History Node

0.1.0

Download Files

Download Files

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

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
mockrequests-0.1.2.tar.gz (3.2 kB) Copy SHA256 Checksum 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