2012-01-22 13 views
7

Djangoアプリケーションでいくつかのビューをテストするために、Djangoテストクライアントdjango.test.client.Clientを使用しています。特に、ビューがget_object_or_404メソッドを呼び出し、オブジェクトが存在しない場合をテストしているので、404を返す必要があります。Djangoテストクライアントで予想される404のテストで未処理の例外が発生する

私のテストコードは次のようになります。

class ViewTests(TestCase): 
    fixtures=['test.json'] 

    def test_thing_not_there_get(self): 
     url = '/foo/30afda98-b9d7-4e26-a59a-76ac1b6a001f/' 
     c = django.test.client.Client() 
     response = c.get(url) 
     self.assertEqual(response.status_code, 404) 

しかし、私が代わりに取得していますが、ビューコード内の未処理の例外エラーです:

python projects/unittests/manage.py test 
Creating test database for alias 'default'... 
......ERROR:root:Unhandled Exception on request for http://testserver/foo/30afda98-b9d7-4e26-a59a-76ac1b6a001f/ 
Traceback (most recent call last): 
    File "/Users/lorin/.virtualenvs/myvenv/lib/python2.7/site-packages/django/core/handlers/base.py", line 111, in get_response 
    response = callback(request, *callback_args, **callback_kwargs) 
    File "/Users/lorin/.virtualenvs/myvenv/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 39, in wrapped_view 
    resp = view_func(*args, **kwargs) 
    File "/Users/lorin/.virtualenvs/myvenv/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 52, in wrapped_view 
    return view_func(*args, **kwargs) 
    File "/Users/lorin/django-myvenv/apps/myvenv_desktop/views.py", line 85, in foo_view 
    instance = get_object_or_404(Foo, uuid=foo_uuid) 
    File "/Users/lorin/.virtualenvs/myvenv/lib/python2.7/site-packages/django/shortcuts/__init__.py", line 115, in get_object_or_404 
    raise Http404('No %s matches the given query.' % queryset.model._meta.object_name) 
Http404: No Foo matches the given query. 

Django 1.3 docs

によると、

The only exceptions that are not visible to the test client are Http404, PermissionDenied and SystemExit. Django catches these exceptions internally and converts them into the appropriate HTTP response codes. In these cases, you can check response.status_code in your test.

この場合、DjangoがHttp404の例外をキャッチしないのはなぜですか?

(ドキュメントに準拠して)、例外はではなく、がテストクライアントに送信されることに注意してください。私は、クライアント側の例外をキャッチしようとした場合:私は同じエラーを取得

with self.assertRaises(django.http.Http404): 
    response = c.get(url) 

を、だけでなく、追加のエラー:テストを通過しているように、さらに検査の際

AssertionError: Http404 not raised 
+0

しかし、あなたの答えによると、djangoは404応答コードを返す必要があります。これはdjangoエラーですか? – AlejandroPerezLillo

答えて

6

これはしばらく前のことですが、私はこれを解決して共有する価値があると思いました。必要なことは、コード内のエラーハンドラをカスタマイズすることです。例えば、私が持っている:

urls.pyに:

from myapp.views import error_handler 
handler404 = error_handler.error404 

と関数error404:

from django.http import HttpResponseNotFound 
def error404(request): 
    t = loader.get_template('404.html') 
    html = t.render(Context()) 

    return HttpResponseNotFound(html) 

をあなたはHttpResponseNotFoundオブジェクトを送信する場合、それは404のステータスコードとして解釈します。ちょうどtemplatesディレクトリに404.htmlを入れて、その[OK]をジャンゴの新しいバージョンで

EDIT

。上記のコントローラーは、エラーページを表示する以外のロジックが必要な場合にのみ必要です。

2

は、それが見えます書かれているとおり:エラーメッセージは実際にはテストの失敗には対応していません。

関連する問題