Search code examples
djangounit-testingdjango-urlsdjango-unittest

Test Django url 'name' is correct url


I think what I'm trying to is self evident:

urls.py

urlpatterns = [
    path('', views.home_page, name='home_page'),
]

test.py

def test_home_page_view_name_uses_correct_url(self):
    name = self.client.get(reverse('home_page'))
    path = self.client.get('/')

    self.assertEqual(name, path)

But I get this error:

AssertionError: <HttpResponse status_code=200, "text/html; charset=utf-8">
             != <HttpResponse status_code=200, "text/html; charset=utf-8">

How can I test this correctly?


Solution

  • There doesn't seem to be any reason to involve the client here.

    name = reverse('home_page')
    path = '/'
    

    self.assertEqual(name, path)