Skip to main content

Generate a stub from a swagger file

Project description

Travis status Code issues Join the chat at Dependency Status


Swagger-stub create automatically a stub of your swagger REST API. This stub can be used anywhere you want like in a pytest fixture for your unit test.

In addition of mocking your API, you can mock some call, and check every call that have been made to the API.

Example Usage

import pytest
import requests

from swagger_stub import swagger_stub

# This is the fixture of your stub
# You only need to specify the path of the swagger file and the address
# where you want to bind your stub.
def test_stub():
    return swagger_stub([('swagger.yaml', '')]).next()

# Then you can use this fixture anywhere you want like your API is really running.
def test_swagger_stub(test_stub):
    # Get a definition example

    # Check a simple call
    response = requests.get('')
    assert response.status_code == 200
    assert response.json() == {
      'foo': 'bar'

    # Check that an invalid body cause an error
    response ='', data='invalid data')
    assert response.status_code == 400

    # Mock a call
    test_stub.add_mock_call('get', '/test', {'mock': 'call'})
    response = requests.get('')
    assert response.json() == {'mock': 'call'}

    # Set some side_effect like in the mock library
    test_stub.add_mock_side_effect('get', '/iter', [{'test': '1'}, {'test': '2'}, {'test': '3'}])
    response = requests.get('')
    assert response.json() == {'test': '1'}
    response = requests.get('')
    assert response.json() == {'test': '2'}
    response = requests.get('')
    assert response.json() == {'test': '3'}

    # This side effect will raise a custom error
    test_stub.add_mock_side_effect('get', '/error', Exception)

    with pytest.raises(Exception):
        response = requests.get('')


More documentation is available at


make install or pip install swagger-stub


swagger-stub is licensed under


0.2.1 (2016-6-6)

  • Fix repeated base path.

0.2.0 (2016-5-2)

  • Add access to definition example.

0.1.1 (2016-1-31)

  • Change license to MIT.

0.1 (2016-1-29)

  • First release on PyPI.

Project details

Download files

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

Source Distribution

swagger_stub-0.2.1.tar.gz (18.7 kB view hashes)

Uploaded Source

Built Distribution

swagger_stub-0.2.1-py2.py3-none-any.whl (7.9 kB view hashes)

Uploaded Python 2 Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page