Skip to main content
Travis status Code issues Join the chat at https://gitter.im/Trax-air/swagger-stub Dependency Status https://img.shields.io/pypi/v/swagger-stub.svg https://img.shields.io/pypi/dw/swagger-stub.svg

swagger-stub

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.
@pytest.fixture
def test_stub():
    return swagger_stub([('swagger.yaml', 'http://foo.com')]).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
    test_stub.definitions['Foo']

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

    # Check that an invalid body cause an error
    response = requests.post('http://foo.com/v1/bar/', data='invalid data')
    assert response.status_code == 400

    # Mock a call
    test_stub.add_mock_call('get', '/test', {'mock': 'call'})
    response = requests.get('http://foo.com/v1/test')
    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('http://foo.com/v1/iter')
    assert response.json() == {'test': '1'}
    response = requests.get('http://foo.com/v1/iter')
    assert response.json() == {'test': '2'}
    response = requests.get('http://foo.com/v1/iter')
    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('http://foo.com/v1/error')

Documentation

More documentation is available at https://swagger-stub.readthedocs.org/en/latest/.

Setup

make install or pip install swagger-stub

License

swagger-stub is licensed under http://opensource.org/licenses/MIT.

History

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.

Metadata

Release files for swagger-stub 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for swagger-stub 0.2.1
File Size Uploaded Provenance
swagger_stub-0.2.1.tar.gz 18.7 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for swagger-stub 0.2.1
File Interpreter ABI Platform
swagger_stub-0.2.1-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 26.6 kB

Release files / swagger_stub-0.2.1.tar.gz

Download URL swagger_stub-0.2.1.tar.gz
Size 18.7 kB
Tags Source
SHA-256 checksum
How to use checksums
6ff47e489e183a5f981de9554228750a738cd18aaf2c7b140bff3680a0317db1
BLAKE2b-256 checksum
How to use checksums
f0dc458bad9fbf7aa5747581ae0bde76fc087a61e2aed7852d269c6872b5a50c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / swagger_stub-0.2.1-py2.py3-none-any.whl

Download URL swagger_stub-0.2.1-py2.py3-none-any.whl
Size 7.9 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
84eb254ccf94f6adb67a3a658e7f3f5a2d5007b3e32784b9b6461cde54c36c23
BLAKE2b-256 checksum
How to use checksums
214cb41ed3f24142f3c8655286aeef928666c50708af4c075ec5173e8eca8b2e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for swagger-stub 0.2.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page