django-webtest is a Python library that provides instant integration of Ian Bicking's WebTest with Django's testing framework. It offers a `django.test.TestCase` subclass (`django_webtest.WebTest`) that wraps a `webtest.TestApp` around the Django WSGI interface, enabling high-level functional and integration testing. The library is actively maintained, with frequent updates to ensure compatibility with the latest Django and Python versions.
pip install django-webtestVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates creating a basic functional test using `django_webtest.WebTest`. It shows how to perform GET requests, interact with forms, simulate user login, and assert properties of the response, such as status code and content. Remember to replace `home_page`, `login_page`, and `profile_page` with actual URL names from your Django project. The `fixtures` attribute is optional and allows loading initial data for tests.
Upgrade Django to a supported version (e.g., Django 4.2 or 5.x) or downgrade django-webtest if legacy Django versions must be maintained.
Migrate your tests to use alternative methods for asserting form errors, such as inspecting `response.html` with Beautiful Soup or checking specific text in `response.text`.
Ensure that your forms include a `{% csrf_token %}` in your templates. If you need to explicitly disable CSRF for a specific view in tests, configure Django's `MIDDLEWARE` setting or use a pytest fixture (if using pytest-django) to unpatch settings, if supported by `django-webtest`.For JavaScript-dependent interactions, consider using a browser automation tool like Selenium or Playwright alongside your functional tests.