我在 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
确保您拥有相同的对象实例。您只想确保它们是 equal .
因此,将最后一行更改为
self.assertEqual(response.status_code, 404)
关于python - Django 理智测试显示 404 不是 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39969494/