python - Django 理智测试显示 404 不是 404

标签 python django unit-testing automated-tests http-status-code-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确保您拥有相同的对象实例。您只想确保它们是 equal .

因此,将最后一行更改为

self.assertEqual(response.status_code, 404)

关于python - Django 理智测试显示 404 不是 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39969494/

相关文章:

python - matplotlib 颜色 rgb_to_hsv 不能正常工作。也许需要报告?

python - 需要整数参数,得到 float

python - 过滤A010784序列的最快方法

jquery - django-debug-toolbar 无法在 Django 网站中使用 jQuery

python - 按属性总和列出的对象 block 列表

python - 如何在 apache : 10048 "Address already in use" 上捕获 Django 项目中的错误

django - Celery 和 Redis 不断耗尽内存

java - Spring 3.2.4 中测试 Controller 的替代方案

c# - 无法将带 [] 的索引应用于类型 'CaSTLe.Proxies.RangeProxy' 的表达式

php - Yii 装置 --- 异常 : Unknown property 'projects' for class 'ProjectTest'