{"id":7182,"library":"django-webtest","title":"Django WebTest","description":"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.","status":"active","version":"1.9.14","language":"en","source_language":"en","source_url":"https://github.com/django-webtest/django-webtest","tags":["django","testing","webtest","functional testing","integration testing","wsgi"],"install":[{"cmd":"pip install django-webtest","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Core framework integration; specific Django versions are supported per django-webtest release.","package":"django"},{"reason":"The underlying functional testing library that django-webtest integrates with Django.","package":"webtest"}],"imports":[{"symbol":"WebTest","correct":"from django_webtest import WebTest"},{"note":"Use this class for tests requiring Django's TransactionTestCase behavior.","symbol":"TransactionWebTest","correct":"from django_webtest import TransactionWebTest"}],"quickstart":{"code":"from django_webtest import WebTest\nfrom django.urls import reverse\n\nclass MyBasicTests(WebTest):\n    fixtures = ['users'] # Optional: if you need initial data, e.g., for login\n\n    def test_homepage_access(self):\n        # Simulate a GET request to the homepage\n        resp = self.app.get(reverse('home_page'))\n        self.assertEqual(resp.status_code, 200)\n        self.assertIn('Welcome', resp.text)\n\n    def test_login_form_submission(self):\n        # Simulate login as a specific user\n        login_page = self.app.get(reverse('login_page'))\n        form = login_page.form\n        form['username'] = 'testuser'\n        form['password'] = 'testpassword'\n        resp = form.submit().follow()\n        self.assertEqual(resp.status_code, 200)\n        self.assertIn('Logged in as testuser', resp.text)\n\n    def test_authenticated_page(self):\n        # Make an authenticated request using the user argument\n        user_resp = self.app.get(reverse('profile_page'), user='testuser')\n        self.assertEqual(user_resp.status_code, 200)\n        self.assertIn('User Profile for testuser', user_resp.text)","lang":"python","description":"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."},"warnings":[{"fix":"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.","message":"Starting with django-webtest 1.9.13, support for Django 3.x and 4.1 has been dropped. Ensure your Django version is supported by the installed django-webtest version.","severity":"breaking","affected_versions":">=1.9.13"},{"fix":"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`.","message":"As of django-webtest 1.9.12, the `assertFormError` method is no longer compatible. This is due to changes in Django's internal handling of form errors.","severity":"breaking","affected_versions":">=1.9.12"},{"fix":"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`.","message":"Unlike Django's native test client, django-webtest does NOT suppress CSRF checks by default. This means missing CSRF tokens in forms will cause tests to fail with HTTP 403 Forbidden errors.","severity":"gotcha","affected_versions":"all"},{"fix":"For JavaScript-dependent interactions, consider using a browser automation tool like Selenium or Playwright alongside your functional tests.","message":"The `click` method on a WebTest response object will not work for links that rely on JavaScript for their action (e.g., `onclick` attributes without an `href`). WebTest does not execute JavaScript.","severity":"gotcha","affected_versions":"all"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Ensure `django_webtest.middleware.WebtestAuthenticationMiddleware` is placed AFTER `django.contrib.auth.middleware.AuthenticationMiddleware` in your `MIDDLEWARE` setting.","cause":"Incorrect ordering of middleware in `settings.py`.","error":"django.core.exceptions.ImproperlyConfigured: The WebtestAuthentication middleware must be placed after Django's AuthenticationMiddleware."},{"fix":"Include `{% csrf_token %}` in your Django form templates. Alternatively, for specific tests, you might need to temporarily disable CSRF middleware or manually add a valid CSRF token if directly constructing form data.","cause":"Submitting a form without a CSRF token, as django-webtest does not disable CSRF checks by default.","error":"CSRF verification failed. Request aborted."},{"fix":"Install the package using `pip install django-webtest`. If using a virtual environment, activate it first. For pytest-django, ensure your project's `manage.py` directory is discoverable or explicitly configured in `pytest.ini`.","cause":"The `django-webtest` library is not installed in the active Python environment or there's an issue with the Python path.","error":"ModuleNotFoundError: No module named 'django_webtest'"},{"fix":"Update your tests to check form errors manually by inspecting `response.text`, `response.html` (e.g., using Beautiful Soup), or other WebTest methods to verify error messages.","cause":"Using `self.assertFormError` with `django-webtest` versions 1.9.12 or newer, which no longer support this assertion due to changes in Django 5.","error":"AttributeError: 'TestResponse' object has no attribute 'assertFormError'"}]}