Skip to main content

Fixtures to mock requests

Project description

pytest-requests

PyPI version Python versions See Build Status on Travis CI See Build Status on AppVeyor

Fixtures to mock requests


Features

  • Patch responses to APIs with static or dynamic data
  • Support for both requests sessions and regular method calls
  • Native support for setting responses as dicitonaries for JSON APIs

Requirements

  • PyTest 3.5+

Installation

You can install "pytest-requests" via pip from PyPI:

$ pip install pytest-requests

Usage

In the most simple use case, just use the requests_mock fixture, which provides a context manager called patch. It returns a patch instance which you can set the .returns value to a response. There is a response factory in .good or .bad which can take a string or a dictionary.

import requests

def test_simple(requests_mock):
    with requests_mock.patch('/api/test') as patch:
        patch.returns = requests_mock.good('hello')
        response = requests.get('https://test.api/api/test')
        assert response.text == 'hello'

With sessions

import requests
from requests.sessions import Session

def test_simple_with_session(requests_mock):
    with requests_mock.patch('/api/test') as patch:
        patch.returns = requests_mock.good('hello')
        with Session() as s:
            response = s.get('https://test.api/api/test')
            assert response.text == 'hello'

requests_mock.good or requests_mock.bad can also take a dictionary, which will be converted to a JSON string implicitly.

import requests
import pytest

def test_json(requests_mock):
    test_dict = {'a': 'b'}
    with requests_mock.patch('/api/test') as patch:
        patch.returns = requests_mock.good(test_dict).as_json()
        response = requests.get('https://test.api/api/test')
        assert response.json() == test_dict

Returning specific headers.

import requests
import pytest

def test_json(requests_mock):
    with requests_mock.patch('/api/test') as patch:
        patch.returns = requests_mock.good('hello', headers={'X-Special': 'value'})
        response = requests.get('https://test.api/api/test')
        assert response.text == 'hello'
        assert response.headers['X-Special'] == 'value'

Contributing

Contributions are very welcome. Tests can be run with tox, please ensure the coverage at least stays the same before you submit a pull request.

License

Distributed under the terms of the MIT license, "pytest-requests" is free and open source software

Issues

If you encounter any problems, please file an issue along with a detailed description.

Credits

This pytest plugin was generated with Cookiecutter along with @hackebrot's cookiecutter-pytest-plugin template.

Project details


Download files

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

Filename, size & hash SHA256 hash help File type Python version Upload date
pytest_requests-0.2.0-py3-none-any.whl (2.9 kB) Copy SHA256 hash SHA256 Wheel py3
pytest-requests-0.2.0.tar.gz (6.6 kB) Copy SHA256 hash SHA256 Source None

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page