Skip to main content

Mock web server for testing web clients

Project description

This tool provides an in-process WSGI server on an ephemeral port. It is intended for use in unit tests, when the system under test makes outgoing HTTP connections that cannot easily be mocked.


First, create a WSGI application that represents the fake web server you want to create. This is simpler than it seems; for example:

def simple_app(environ, start_response):
    status = '200 OK'
    headers = [('Content-type', 'text/plain')]
    start_response(status, headers)
    return ['Hello, world!\n']

Next, activate the test server. There are several ways to do this, but all of them produce a port number.

As a context manager:

from mockweb import mock_server
def test_web_request():
    with mock_server(simple_app) as port:

As a decorator:

from mockweb import mock_server
def test_web_request(port):

Or manually started and stopped:

from mockweb import mock_server
def test_web_request():
    server = mock_server(simple_app)
    # ...

In the latter case, be careful to stop the server.

Project details

Download files

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

Source Distributions

No source distribution files available for this release. See tutorial on generating distribution archives.

Built Distribution

webmock-1.0.0-py2.py3-none-any.whl (4.6 kB view hashes)

Uploaded py2 py3

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page