Package: mockserver-friendly-client
MockServer Python ClientPython client to James D. Bloom's awesome MockServer :http://www.mock-server.com/PhilosophyTests should be readable and Mock Server is already very complete and customizable.  This library tries to keep it
simple and straight-forward using python's kwargs to avoid big declarations.We think a mock should be able to fit into one 120 char line of code should the expectation be simple enough :)client.stub(request(method="GET", path="/auth"), response(code=401, body="unauthorized"))InstallationWARNING:THIS IS A VERY EARLY VERSION THE API IS MOST LIKELY TO CHANGEFrom source:git clone https://github.com/internap/python-mockserver-friendly-client.git
cd python-mockserver-friendly-client
python setup.py installFromPyPidirectly:pip install mockserver-friendly-clientPrerequisiteYou need a running MockServer running, seehttp://www.mock-server.com/mock_server/getting_started.html#start_mockserverThis project's tests uses the docker image :https://github.com/internap/python-mockserver-friendly-client/blob/master/docker-compose.ymlUsageThe whole Mock Server API is not all covered. We just implemented what we needed, if you need something not yet
implemented you can open an issue and/or contributeStubbing(For when you are testing what your codeDOESwith another component's data)from mockserver import MockServerClient, request, response

client = MockServerClient("http://localhost:1080")

client.stub(
    request(method="GET", path="/that/thing", querystring={"is": "good"}, headers={"so": "good"}),
    response(code=418, body="i'm a teapot", headers={"hi": "haa"})
)You can also add atimes(N)as a third parameter to limit how many times this stub can be called, default is unlimitedAll parameters are always optional.  When not specified, the match everything.Expecting(For when calling another componentISwhat you are testing)Using theexpectwill remember the request and verify it whenverify_expectationsis called.import json
from mockserver import MockServerClient, request, response, times

client = MockServerClient("http://localhost:1080")

client.expect(
    request(method="POST", path="/postme", body=json.dumps({"some": "json"})),
    response(code=204, body=json.dumps({"return": "something"}), headers={"Content-Type": "application/json"}),
    times(1)
)

client.verify_expectations()  # AssertionError !Thetimes(N)parameter is mandatory for expectResetting(Because tests shouldn't impact each other)from mockserver import MockServerClient

client = MockServerClient("http://localhost:1080")

client.reset()Customizing and shortcutsThis client consumes Mock Server's REST API :https://app.swaggerhub.com/apis/jamesdbloom/mock-server-openapiHere's a few shortcuts already ready, there may be more to comeMocking a form postfrom mockserver import MockServerClient, request, response, form

client = MockServerClient("http://localhost:1080")

client.stub(
    request(method="POST", body=form({"user": "foo", "pass": "bar"})),
    response(code=201)
)This currently takes a 1 leveldict. For array mocking, use{"key[index]": "value"}Returning JSONfrom mockserver import MockServerClient, request, json_response

client = MockServerClient("http://localhost:1080")

client.stub(
    request(path="/stuff"),
    json_response(code=200, body={"full": "json", "structure": ["with", "stuff", 1]})
)This automatically dumps the body in json and appends theapplication/jsonContent-Type to the headers.Expecting JSON requestfrom mockserver import MockServerClient, request, json_equals

client = MockServerClient("http://localhost:1080")
client.expect(
    request(body=json_equals({"key": "value"})),
    response(),
    times(1)
)This only matches requests with the provided json body. Json conversation is done automatically.
If you only want to match parts of a given JSON you can provide only the matching parts usingjson_contains:from mockserver import MockServerClient, request, json_contains

client = MockServerClient("http://localhost:1080")
client.expect(
    request(body=json_contains({"key": "value"})),
    response(),
    times(1)
)I.e. this will match{"key": "value"}or{"key": "value", "another": "key"}Verifying requestfrom mockserver import MockServerClient, request, times

client = MockServerClient("http://localhost:1080")
client.verify(
    request(path="/some_path", querystring({"key": "value"})),
    times(1)
)Verify will check request on MockServer and raise AssertionError if request not found.More documentationThere is currently no official documentation, however you can consider the tests as a type of documentation, they are
pretty explicit and simple to follow and help to clarify the purpose of each feature.Good practicesHaving your test setup/teardown like this is probably a good idea.class ServerMockingTestBase(...):
    def setUp(self):
        super(ServerMockingTestBase, self).setUp()
        
        self.client = MockServerClient(MOCK_SERVER_URL)
        self.client.reset()

    def tearDown(self):
        super(ServerMockingTestBase, self).tearDown()
        
        self.client.verify_expectations()TroubleshootingChecking MockServer's logs is the first place to go.  If you don't see any logs, try another LOG_LEVEL such as INFO.If the problem is in the code, please open an issue :)ContributingThe form may not be final, we would love to hear what you think of the client!Feel free to raise issues and send some pull request, we'll be happy to look at them!Make sure all new code is tested, current tests run against a MockServer container.Running testsYou can easily run the tests withhttps://pypi.python.org/pypi/toxtox -e py34You can also call thetest-runner.shdirectly, you will needhttps://pypi.python.org/pypi/noseinstalledYou can also launch the containerdocker-compose up-dand run the tests in your favorite IDE :)