user1762708 user1762708 - 1 month ago 8
Python Question

Django Sanity Test Reveals 404 is not 404

I was creating sanity checks in my django tests and came across an error I had accounted for. Unfortunately my test fails when it should be successful. I am testing when a page is not there (a 404 status code). The error message and code are below. When I added quotes I received 404 is not '404'.

Django-1.10.2 & Python 3.4.5

./manage.py test app

Traceback (most recent call last):
File "/tests/dir/tests.py", line 26, in test_404
self.assertIs(response.status_code,404)
AssertionError: 404 is not 404

from django.test import TestCase
from django.test import Client

class SanityCheckTests(TestCase):
def test_404(self):
"""
Http Code 404
"""
client = Client()

response = client.get('/test404')
print(response)
self.assertIs(response.status_code,404)

Answer

You're on the right track - but assertIs ensures you have the same instance of an object. You just want to make sure they are equal.

Therefore, change your last line to

self.assertEqual(response.status_code, 404)