One thing that has bugged me about testing Django apps is that I couldn't figure out how to override the global URL settings, which meant that app tests were tightly coupled with the global URL scheme.
For example, to test the adder
app, I used Client calls like this:
resp = Client().get("/toys/calculator/adder/1+2")
Which were, of course, totally lame because they a tight coupling between the adder
app and the project it lived in.
But there is a better (albeit poorly documented) way: overwriting django.conf.settings.ROOT_URLCONF
!
There are two ways to do it: hand-rolling a solution, or using TestCase.urls.
Here is a quick example of both. First, using TestCase.urls
:
import adder
class AdderTests(TestCase):
urls = adder.urls
def test_adder(self):
resp = Client().get("/1+2")
And then, the hand-rolled solution I've been using with django-nose:
_old_root_urlconf = [ None ]
def setup_module():
_old_root_urlconf[0] = settings.ROOT_URLCONF
settings.ROOT_URLCONF = urls
def teardown_module():
settings.ROOT_URLCONF = _old_root_urlconf[0]
def test_adder():
resp = Client().get("/1+2")