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.
pip install WebTestVerified import paths — ran on the pinned version, not inferred.
Initializes `TestApp` with a basic WSGI application and performs a GET request, asserting the response status and content.
Upgrade Python environment to 3.6 or newer (preferably 3.9+). Review code for Python 2/3.5 specific syntax.
Update usage of `click` methods, adapt to the new cookiejar API if targeting Python 3.3+, and install `webtest-selenium` or `webtest-casperjs` if those integrations are still required.
If expecting an error status (e.g., 404, 500), pass `status=EXPECTED_STATUS_CODE` (e.g., `status=404`) or `expect_errors=True` to the request method (e.g., `app.get('/nonexistent', status=404)` or `app.post('/error', expect_errors=True)`).Ensure the necessary parsing library is installed (`pip install beautifulsoup4 lxml pyquery`) and that the response's `Content-Type` header matches the expected format (e.g., `text/html` for `.html`, `application/json` for `.json`).
pip install webtest
response = app.post('/api/endpoint', json={'key': 'value'})response = app.post('/form_submit', params={'field1': 'value1', 'field2': 'value2'})To follow the redirect, call the `.follow()` method on the response object, optionally allowing non-2xx statuses for the initial request:
response = app.post('/submit', params={'data': 'value'}, status='*')
final_response = response.follow()
# Or more concisely:
final_response = app.post('/submit', params={'data': 'value'}).follow()