Changeset 4482
- Timestamp:
- 02/10/07 18:23:31 (2 years ago)
- Files:
-
- django/trunk/AUTHORS (modified) (1 diff)
- django/trunk/django/contrib/auth/forms.py (modified) (1 diff)
- django/trunk/django/test/client.py (modified) (4 diffs)
- django/trunk/docs/testing.txt (modified) (1 diff)
- django/trunk/tests/modeltests/test_client/models.py (modified) (1 diff)
- django/trunk/tests/modeltests/test_client/urls.py (modified) (1 diff)
- django/trunk/tests/modeltests/test_client/views.py (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/AUTHORS
r4479 r4482 54 54 Esdras Beleza <linux@esdrasbeleza.com> 55 55 James Bennett 56 Ben <afternoon@uk2.net> 56 57 Paul Bissex <http://e-scribe.com/> 57 58 Simon Blanchard django/trunk/django/contrib/auth/forms.py
r4266 r4482 5 5 from django.core import validators 6 6 from django import oldforms 7 from django.utils.translation import gettext as _ 7 8 8 9 class UserCreationForm(oldforms.Manipulator): django/trunk/django/test/client.py
r4464 r4482 1 import sys 1 2 from cStringIO import StringIO 2 3 from django.conf import settings 3 4 from django.core.handlers.base import BaseHandler 4 5 from django.core.handlers.wsgi import WSGIRequest 6 from django.core.signals import got_request_exception 5 7 from django.dispatch import dispatcher 6 8 from django.http import urlencode, SimpleCookie … … 101 103 self.cookies = SimpleCookie() 102 104 self.session = {} 105 self.exc_info = None 106 107 def store_exc_info(self, *args, **kwargs): 108 """ 109 Utility method that can be used to store exceptions when they are 110 generated by a view. 111 """ 112 self.exc_info = sys.exc_info() 103 113 104 114 def request(self, **request): … … 128 138 on_template_render = curry(store_rendered_templates, data) 129 139 dispatcher.connect(on_template_render, signal=signals.template_rendered) 140 141 # Capture exceptions created by the handler 142 dispatcher.connect(self.store_exc_info, signal=got_request_exception) 130 143 131 144 response = self.handler(environ) … … 143 156 setattr(response, detail, None) 144 157 158 # Look for a signalled exception and reraise it 159 if self.exc_info: 160 raise self.exc_info[1], None, self.exc_info[2] 161 162 # Update persistent cookie and session data 145 163 if response.cookies: 146 164 self.cookies.update(response.cookies) django/trunk/docs/testing.txt
r4464 r4482 291 291 292 292 .. _RFC2616: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html 293 294 Exceptions 295 ~~~~~~~~~~ 296 297 If you point the Test Client at a view that raises an exception, that exception 298 will be visible in the test case. You can then use a standard ``try...catch`` 299 block, or ``unittest.TestCase.assertRaises()`` to test for exceptions. 300 301 The only exceptions that are not visible in a Test Case are ``Http404``, 302 ``PermissionDenied`` and ``SystemExit``. Django catches these exceptions 303 internally and converts them into the appropriate HTTP responses codes. 293 304 294 305 Persistent state django/trunk/tests/modeltests/test_client/models.py
r4464 r4482 115 115 # Check that the session was modified 116 116 self.assertEquals(self.client.session['tobacconist'], 'hovercraft') 117 118 def test_view_with_exception(self): 119 "Request a page that is known to throw an error" 120 self.assertRaises(KeyError, self.client.get, "/test_client/broken_view/") 117 121 122 #Try the same assertion, a different way 123 try: 124 self.client.get('/test_client/broken_view/') 125 self.fail('Should raise an error') 126 except KeyError: 127 pass django/trunk/tests/modeltests/test_client/urls.py
r4464 r4482 7 7 (r'^redirect_view/$', views.redirect_view), 8 8 (r'^login_protected_view/$', views.login_protected_view), 9 (r'^session_view/$', views.session_view) 9 (r'^session_view/$', views.session_view), 10 (r'^broken_view/$', views.broken_view) 10 11 ) django/trunk/tests/modeltests/test_client/views.py
r4464 r4482 37 37 def session_view(request): 38 38 "A view that modifies the session" 39 40 39 request.session['tobacconist'] = 'hovercraft' 41 40 … … 44 43 c = Context() 45 44 return HttpResponse(t.render(c)) 46 45 46 def broken_view(request): 47 """A view which just raises an exception, simulating a broken view.""" 48 raise KeyError("Oops! Looks like you wrote some bad code.")
