{"id":16417540,"url":"https://github.com/django-webtest/django-webtest","last_synced_at":"2025-05-15T15:05:05.887Z","repository":{"id":1558682,"uuid":"1962269","full_name":"django-webtest/django-webtest","owner":"django-webtest","description":"django-webtest provides integration of Ian Bicking's WebTest (http://docs.pylonsproject.org/projects/webtest/) with django's testing framework.","archived":false,"fork":false,"pushed_at":"2024-08-30T08:02:21.000Z","size":334,"stargazers_count":328,"open_issues_count":24,"forks_count":68,"subscribers_count":10,"default_branch":"master","last_synced_at":"2025-03-31T16:13:10.446Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://pypi.python.org/pypi/django-webtest/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/django-webtest.png","metadata":{"files":{"readme":"README.rst","changelog":"CHANGES.rst","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":"AUTHORS.txt","dei":null,"publiccode":null,"codemeta":null}},"created_at":"2011-06-27T19:59:52.000Z","updated_at":"2024-11-28T16:28:54.000Z","dependencies_parsed_at":"2024-06-18T12:42:17.753Z","dependency_job_id":"f3d16a79-f61d-47f5-85ab-a3882cca3dac","html_url":"https://github.com/django-webtest/django-webtest","commit_stats":{"total_commits":332,"total_committers":46,"mean_commits":7.217391304347826,"dds":0.6295180722891567,"last_synced_commit":"28689aa54bf8a823cf874fdca9cea7646fcf50d0"},"previous_names":[],"tags_count":36,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/django-webtest%2Fdjango-webtest","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/django-webtest%2Fdjango-webtest/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/django-webtest%2Fdjango-webtest/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/django-webtest%2Fdjango-webtest/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/django-webtest","download_url":"https://codeload.github.com/django-webtest/django-webtest/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247694875,"owners_count":20980733,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-11T07:11:34.067Z","updated_at":"2025-04-07T17:06:56.538Z","avatar_url":"https://github.com/django-webtest.png","language":"Python","readme":"==============\ndjango-webtest\n==============\n\n.. image:: https://img.shields.io/pypi/v/django-webtest.svg\n   :target: https://pypi.python.org/pypi/django-webtest\n   :alt: PyPI Version\n\n.. image:: https://img.shields.io/github/license/kmike/django-webtest.svg\n   :target: https://github.com/django-webtest/django-webtest/blob/master/LICENSE.txt\n   :alt: License\n\n.. image:: https://img.shields.io/travis/django-webtest/django-webtest/master.svg\n   :target: http://travis-ci.org/django-webtest/django-webtest\n   :alt: Build Status\n\ndjango-webtest is an app for instant integration of Ian Bicking's\nWebTest (http://docs.pylonsproject.org/projects/webtest/) with Django's\ntesting framework.\n\nInstallation\n============\n\n.. code-block:: console\n\n    $ pip install django-webtest\n\nUsage\n=====\n\n.. code-block:: python\n\n    from django_webtest import WebTest\n\n    class MyTestCase(WebTest):\n\n        # optional: we want some initial data to be able to login\n        fixtures = ['users', 'blog_posts']\n\n        # optional: default extra_environ for this TestCase\n        extra_environ = {'HTTP_ACCEPT_LANGUAGE': 'ru'}\n\n        def testBlog(self):\n            # pretend to be logged in as user `kmike` and go to the index page\n            index = self.app.get('/', user='kmike')\n\n            # All the webtest API is available. For example, we click\n            # on a \u003ca href='/tech-blog/'\u003eBlog\u003c/a\u003e link, check that it\n            # works (result page doesn't raise exceptions and returns 200 http\n            # code) and test if result page have 'My Article' text in\n            # its body.\n            assert 'My Article' in index.click('Blog')\n\ndjango-webtest provides a django.test.TestCase subclass\n(``django_webtest.WebTest``) that creates ``webtest.TestApp`` around\ndjango wsgi interface and makes it available in tests as ``self.app``.\n\nIt also features an optional ``user`` argument for ``self.app.get``,\n``self.app.post``, etc. to help making authorized requests. This argument\nshould be a django.contrib.auth.models.User instance or a string with user's\n``username`` for the user who is supposed to be logged in. To log out again,\ncall ``self.app.reset``, clearing all cookies.  To make a bunch of calls\nwith the same user, call ``app.set_user(user)`` before your requests; if\nyou want to disable that user, call ``app.get(..., user=None)`` for one\nrequest or ``app.set_user(None)`` to unset the user for all following calls.\n\nFor 500 errors original traceback is shown instead of usual html result\nfrom handler500.\n\nYou also get the ``response.templates`` and ``response.context`` goodness that\nis usually only available if you use Django's native test client. These\nattributes contain a list of templates that were used to render the response\nand the context used to render these templates. All of Django's native asserts (\n``assertFormError``,  ``assertTemplateUsed``, ``assertTemplateNotUsed``,\n``assertContains``, ``assertNotContains``, ``assertRedirects``) are\nalso supported for WebTest responses.\n\nThe session dictionary is available via ``self.app.session``, and has the\nsame content than Django's native test client.\n\nUnlike Django's native test client CSRF checks are not suppressed\nby default so missing CSRF tokens will cause test fails (and that's good).\n\nIf forms are submitted via WebTest forms API then all form fields (including\nCSRF token) are submitted automagically:\n\n.. code-block:: python\n\n    class AuthTest(WebTest):\n        fixtures = ['users.json']\n\n        def test_login(self):\n            form = self.app.get(reverse('auth_login')).form\n            form['username'] = 'foo'\n            form['password'] = 'bar'\n            response = form.submit().follow()\n            self.assertEqual(response.context['user'].username, 'foo')\n\nHowever if forms are submitted via raw POST requests using ``app.post`` then\ncsrf tokens become hard to construct. CSRF checks can be disabled by setting\n``csrf_checks`` attribute to False in this case:\n\n.. code-block:: python\n\n    class MyTestCase(WebTest):\n        csrf_checks = False\n\n        def test_post(self):\n            self.app.post('/')\n\nWhen a subclass of Django's ``TransactionTestCase`` is desired,\nuse ``django_webtest.TransactionWebTest``.\n\nFor disabling CSRF checks in a ``pytest-django`` fixture, see\n`Usage with PyTest`_.\n\nAll of these features can be easily set up manually (thanks to WebTest\narchitecture) and they are even not neccessary for using WebTest with Django but\nit is nice to have some sort of integration instantly.\n\nSee http://docs.pylonsproject.org/projects/webtest/ for API help. Webtest can\nfollow links, submit forms, parse html, xml and json responses with different\nparsing libraries, upload files and more.\n\nIntegration with django-rest-framework\n======================================\n\nIf your project uses django-rest-framework__, the setting\n``REST_FRAMEWORK['AUTHENTICATION_CLASSES']`` will be patched\nautomatically to include a class that links the rest-framework\nauthentication system with ``app.get(user=user)``.\n\n.. __: https://www.django-rest-framework.org/\n\nUsage with PyTest\n=================\n\nYou need to install `pytest-django \u003chttps://pytest-django.readthedocs.io\u003e`_:\n\n.. code-block:: console\n\n    $ pip install pytest-django\n\nThen you can use ``django-webtest``'s fixtures:\n\n.. code-block:: python\n\n    def test_1(django_app):\n        resp = django_app.get('/')\n        assert resp.status_code == 200, 'Should return a 200 status code'\n\nWe have a ``django_app_factory`` fixture we can use to create custom fixtures.\nFor example, one that doesn't do CSRF checks:\n\n.. code-block:: python\n\n    # conftest.py\n\n    @pytest.fixture\n    def csrf_exempt_django_app(django_app_factory):\n        return django_app_factory(csrf_checks=False)\n\n``csrf_checks`` and ``extra_environ`` are the only arguments to\n``django_app_factory``.\n\n\nWhy?\n====\n\nWhile django.test.client.Client is fine for its purposes, it is not\nwell-suited for functional or integration testing. From Django's test client\ndocstring:\n\n    This is not intended as a replacement for Twill/Selenium or\n    the like - it is here to allow testing against the\n    contexts and templates produced by a view, rather than the\n    HTML rendered to the end-user.\n\nWebTest plays on the same field as twill. WebTest has a nice API,\nis fast, small, talks to the django application via WSGI instead of HTTP\nand is an easy way to write functional/integration/acceptance tests.\ndjango-webtest is able to provide access to the names of rendered templates\nand template context just like native Django TestClient.\n\nContributing\n============\n\nDevelopment happens at github: https://github.com/django-webtest/django-webtest\nIssue tracker: https://github.com/django-webtest/django-webtest/issues\n\nFeel free to submit ideas, bugs or pull requests.\n\nRunning tests\n-------------\n\nMake sure `tox`_ is installed and run:\n\n.. code-block:: console\n\n    $ tox\n\nfrom the source checkout.\n\n.. _tox: http://tox.testrun.org\n","funding_links":[],"categories":["Python"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdjango-webtest%2Fdjango-webtest","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdjango-webtest%2Fdjango-webtest","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdjango-webtest%2Fdjango-webtest/lists"}