Reputation: 4669
I'm a ways down my Django project and have decided unit tests are a good idea. I am trying to write a simple unit test around a view, but I'm getting <h1>Not Found</h1><p>The requested URL /index was not found on this server.</p>
from the response. Why is that happening?
Here is my unit test..
from django.test import TestCase
class BookerIndexTests(TestCase):
def test_anonymous_request(self):
response = self.client.get('booker:index')
self.assertEqual(response.status_code, 200)
Within my urls.py
, I have this line for my index: url(r'^$', views.index, name='index'),
Am I missing a setup step here? Why is this basic unit test throwing a 404 error?
Upvotes: 0
Views: 628
Reputation: 151370
As Daniel Roseman pointed out, you can't use a pattern name directly in client.get()
. If you want to use the pattern name rather than the paths themselves, you can use reverse
. You code could look like this:
from django.test import TestCase
from django.core.urlresolvers import reverse
class BookerIndexTests(TestCase):
def test_anonymous_request(self):
response = self.client.get(reverse('booker:index'))
self.assertEqual(response.status_code, 200)
This is typically what I do in my test suites because I prefer to use the pattern names over the paths.
Upvotes: 4
Reputation: 599450
You're passing a URL pattern name to client.get()
, rather than an actual path. You need to pass the actual index path, which is - according to that urlconf - "/".
Upvotes: 1