Code Monkey home page Code Monkey logo

mbtest's Introduction

mbtest

Opinionated Python wrapper & utils for the Mountebank over the wire test double tool.

Includes pytest fixture and PyHamcrest matchers.

made-with-python Build Status PyPi Version Python Versions Licence GitHub all releases GitHub forks GitHub stars GitHub watchers GitHub contributors GitHub issues GitHub issues-closed GitHub pull-requests GitHub pull-requests closed Coverage Lines of Code

Setup

Install with pip:

pip install mbtest

(As usual, use of a venv or virtualenv is recommended.) Also requires Mountebank to have been installed:

npm install [email protected] --production

Basic example

import requests
from hamcrest import assert_that, is_
from brunns.matchers.response import response_with
from mbtest.matchers import had_request
from mbtest.imposters import Imposter, Predicate, Response, Stub

def test_request_to_mock_server(mock_server):
    # Set up mock server with required behavior
    imposter = Imposter(Stub(Predicate(path="/test"), 
                             Response(body="sausages")))

    with mock_server(imposter) as server:
        # Make request to mock server - exercise code under test here
        response = requests.get("{}/test".format(imposter.url))

        assert_that("We got the expected response", 
                    response, is_(response_with(status_code=200, body="sausages")))
        assert_that("The mock server recorded the request", 
                    server, had_request(path="/test", method="GET"))

Needs a pytest fixture, most easily defined in conftest.py:

import pytest
from mbtest import server

@pytest.fixture(scope="session")
def mock_server(request):
    return server.mock_server(request)

Examples of more complex predicates can be found in the integration tests.

Developing

Requires make and tox. Run make precommit tells you if you're OK to commit. For more options, run:

make help

Releasing

Requires hub, setuptools and twine. To release n.n.n:

version="n.n.n" # Needs to match new version number in setup.py.
make precommit && git commit -am"Release $version" && git push # If not already all pushed, which it should be.
hub release create $version -m"Release $version"
python setup.py sdist bdist_wheel
twine upload dist/*$version*

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.