Django UrlResolver, adding urls at runtime for testing

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.


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.