Question

I'm looking to do some tests and I'm not really familiar with the URLResolver quite yet but I'd like to solve this issue quickly.

In a TestCase, I'd like to add a URL to the resolver so that I can then use Client.get('/url/') and keep it separate from urls.py.

Was it helpful?

Solution

https://docs.djangoproject.com/en/2.1/topics/testing/tools/#urlconf-configuration

In your test:

class TestMyViews(TestCase):
     urls = 'myapp.test_urls'

This will use myapp/test_urls.py as the ROOT_URLCONF.

OTHER TIPS

Since Django 1.8 using of django.test.TestCase.urls is deprecated. You can use django.test.utils.override_settings instead:

from django.test import TestCase
from django.test.utils import override_settings

urlpatterns = [
    # custom urlconf
]

@override_settings(ROOT_URLCONF=__name__)
class MyTestCase(TestCase):
    pass

override_settings can be applied either to a whole class or to a particular method.

I know this was asked a while ago, but I thought I'd answer it again to offer something more complete and up-to-date.

You have two options to solve this, one is to provide your own urls file, as suggested by SystemParadox's answer:

class MyTestCase(TestCase):
    urls = 'my_app.test_urls'

The other is to monkey patch your urls. This is NOT the recommended way to deal with overriding urls but you might get into a situation where you still need it. To do this for a single test case without affecting the rest you should do it in your setUp() method and then cleanup in your tearDown() method.

import my_app.urls
from django.conf.urls import patterns

class MyTestCase(TestCase):
    urls = 'my_app.urls'

    def setUp(self):
        super(MyTestCase, self).setUp()
        self.original_urls = my_app.urls.urlpatterns
        my_app.urls.urlpatterns += patterns(
            '',
            (r'^my/test/url/pattern$', my_view),
        )

    def tearDown(self):
        super(MyTestCase, self).tearDown()
        my_app.urls.urlpatterns = self.original_urls

Please note that this will not work if you omit the urls class attribute. This is because the urls will otherwise be cached and your monkey patching will not take effect if you run your test together with other test cases.

Couldn't get it running with the answers above. Not even with the override_settings. Found a solution which works for me. My usecase was to write some integration tests where I want to test put/post methods where I needed the urls from my app.

The main clue here is to use the set_urlconf function of django.urls instead of overwriting it in the class or using override_settings.

from django.test import TestCase
from django.urls import reverse, set_urlconf

class MyTests(TestCase):
    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        set_urlconf('yourapp.urls')  # yourapp is the folder where you define your root urlconf.

    def test_url_resolving_with_app_urlconf(self):
        response = self.client.put(
            path=reverse('namespace:to:your:view-name'), data=test_data
        )
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top