pytest-dev / pytest-django

A Django plugin for pytest.
https://pytest-django.readthedocs.io/
Other
1.37k stars 344 forks source link

Add a django_set_urlconf fixture #1064

Closed orf closed 1 year ago

orf commented 1 year ago

Currently overriding the Django urlconf requires a marker, which also requires defining the urlconf at the root of the test module. If you have many tests that need this (with differing patterns and views) this can get quite annoying.

This doesn't seem to be necessary, as the Django urlconf setting can be an object and it can be changed at runtime.

This PR adds a django_set_urlconf fixture that allows you to define the urlconf within the test and set it directly with the fixture:

def test_django_set_urlconf(django_set_urlconf, client) -> None:
    def my_view(request):
        return http.HttpResponse(status=200, content="Success!")

    urlpatterns = (urls.path("test/view", my_view, name="test_view"),)
    django_set_urlconf(urlpatterns)
    assert b'Success!' in client.get("test_view").content
orf commented 1 year ago

Done, thank you @adamchainz 🚀