WebTest
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
- breaking Version 3.0.0 dropped support for Python 2.7 and Python 3.5. Applications must use Python 3.6+ (3.9+ officially supported).
- breaking Version 2.0 introduced several breaking changes, including the removal of `anchor` from `TestResponse.click` and `button` from `TestResponse.clickbutton`. The cookiejar API changed for Python 3.3. Selenium and CasperJS integrations were moved to separate, optional packages (`webtest-selenium`, `webtest-casperjs`).
- gotcha By default, `TestApp.get()`, `post()`, and other request methods raise an `AppError` if the response status code is not 2xx or 3xx (success or redirect).
- gotcha Accessing parsed response attributes like `resp.html`, `resp.xml`, `resp.lxml`, `resp.pyquery`, or `resp.json` requires the respective parsing library (BeautifulSoup4, lxml, PyQuery, or `json` module for `json`) to be installed. An `ImportError` or `AttributeError` may be raised if the library is missing or the content-type is incorrect.
Install
-
pip install WebTest
Imports
- TestApp
from webtest import TestApp
Quickstart
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}")