Giter Club home page Giter Club logo

swagger-stub's Introduction

Travis status

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):
    # 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.

swagger-stub's People

Contributors

cyprien-g avatar cyprieng avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.