WebTest

3.0.7 · active · verified Sat Apr 11

WebTest is a Python library that wraps any WSGI application, making it easy to send test requests without starting an HTTP server. It provides convenient full-stack testing for WSGI-compatible frameworks. An extraction of `paste.fixture.TestApp`, rewritten to use `WebOb`, it is under active development as part of the Pylons cloud of packages and is currently at version 3.0.7.

Warnings

Install

Imports

Quickstart

Initializes `TestApp` with a basic WSGI application and performs a GET request, asserting the response status and content.

from webtest import TestApp

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

app = TestApp(simple_app)
resp = app.get('/')

assert resp.status == '200 OK'
assert 'Hello, world!' in resp.text

print(f"Status: {resp.status}")
print(f"Body: {resp.text}")

view raw JSON →