小编gbe*_*yuk的帖子

如何用django测试框架测试404 NOT FOUND?

我正在尝试使用Django 1.4的测试框架自动化404页面测试.

如果我127.0.0.1:8000/something/really/weird/在运行开发服务器的浏览器地址栏中打印,我会看到一个404页面,其中包含正确的"404 NOT FOUND"状态(如firebug所示).

但是,如果我尝试使用此代码进行测试:

from django.test import TestCase
class Sample404TestCase(TestCase):
    def test_wrong_uri_returns_404(self):
        response = self.client.get('something/really/weird/')
        self.assertEqual(response.status_code, 404)
Run Code Online (Sandbox Code Playgroud)

此输出的测试失败:

$./manage.py test main
Creating test database for alias 'default'...
.F
======================================================================
FAIL: test_wrong_uri_returns_404 (main.tests.Sample404TestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File ".../main/tests.py", line 12, in test_wrong_uri_returns_404
    self.assertEqual(response.status_code, 404)
*AssertionError: 200 != 404*

----------------------------------------------------------------------
Ran 2 tests in 0.031s

FAILED (failures=1)
Destroying test database for alias 'default'...
Run Code Online (Sandbox Code Playgroud)

我在这里获得200个代码感到非常惊讶.任何人都知道为什么地球上会发生这种情况?

更新:

这里存在urls.py:http://pastebin.com/DikAVa8T 和实际失败的测试是:

def test_wrong_uri_returns_404(self):
    response = self.client.get('/something/really/weird/')
    self.assertEqual(response.status_code, 404) …
Run Code Online (Sandbox Code Playgroud)

testing django http-status-code-404

6
推荐指数
2
解决办法
4013
查看次数

标签 统计

django ×1

http-status-code-404 ×1

testing ×1