2

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)
  • In Python the "is" operator tests for exact object equality. Just because response.status_code prints as 404 doesn't mean that it's an integer with the value 404. Try printing response.status_code.__class__ or isinstance(response.status_code) and see what you get. – Paul Cornelius Oct 11 '16 at 02:40

1 Answers1

3

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)
Shadow
  • 8,749
  • 4
  • 47
  • 57
  • It very strange that my 200 code sanity check doesnt fail. It uses assertIs. But my test works now thanks! – user1762708 Oct 11 '16 at 03:18
  • This question explains what is going on: http://stackoverflow.com/questions/306313/is-operator-behaves-unexpectedly-with-integers – Shadow Oct 11 '16 at 03:48