Question

I am writing some tests in Django to check the apps which I have written. I am more or less working through what is suggested on here (http://toastdriven.com/blog/2011/apr/10/guide-to-testing-in-django/). However, when I put something like

    resp = self.client.get('made_up_url')
    self.assertEqual(resp.status_code, 404)

which doesn't exist (and hence I'm hoping for a 404 response, i.e. the test will pass)...but the test fails saying 200!=400, i.e. it seems to think that the made_up_url is a valid page.

Any ideas what could be wrong?

Was it helpful?

Solution

You are trying to self.client.get an unexistent URL: made_up_url. As dan-klasson told you, you need to get /made_up_url.

I suggest that, instead of hardcoding the URL in the self.client.get arguments, first name the related URL in your urls.py file. So you can resolve it via the reverse function.

Then, once you have a name for it, you can do this in your unit tests:

from django.core.urlresolvers import reverse

url = reverse('made_up_url')
resp = self.client.get(url)
self.assertEqual(resp.status_code, 404)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top