JasonTS
JasonTS

Reputation: 2589

How to unit test urls.py when included by project?

How can I run unit tests to validate that a certain URL calls a particular function?

I want to do something like this:

class HomePageTest(TestCase):

  def test_root_url_resolves_to_list_view(self):
    found = resolve('/testme/')
    self.assertEqual(found.func.func_name, ListView.__name__)
    #self.assertEqual(found.func, ListView.as_view())

But lets imagine the apps urls.py is included in the projects urls.py under something like:

url(r'^submodule/$', include('fhqna.urls')),

How can I write the test included in the app so it checks the url "/testme/" indepent of how it is included? ("/submodule/testme/" in this example)?

Upvotes: 2

Views: 63

Answers (1)

catavaran
catavaran

Reputation: 45575

You can configure urls for test case

class HomePageTest(TestCase):

    urls = 'fhqna.urls'

    def test_root_url_resolves_to_list_view(self):
        found = resolve('/testme/')
        self.assertEqual(found.func.func_name, ListView.__name__)

Or give a name to your url and resolve it by this name regardless of actual url is used. In this case you don't need to configure urls for TestCase.

Upvotes: 2

Related Questions