Django 健全性测试显示 404 不是 404

Django Sanity Test Reveals 404 is not 404

我在我的 Django 测试中创建健全性检查并遇到了一个我已经解释过的错误。不幸的是,我的测试在应该成功的时候失败了。我正在测试页面何时不存在(404 状态代码)。错误信息和代码如下。当我添加引号时,我收到的 404 不是“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)

您的方向是正确的 - 但是 assertIs ensures you have the same instance of an object. You just want to make sure they are equal

因此,将最后一行更改为

self.assertEqual(response.status_code, 404)