Opened 18 years ago

Closed 18 years ago

#3162 closed enhancement (fixed)

Test client should re-raise exceptions created while rendering a page

Reported by: afternoon@… Owned by: Russell Keith-Magee
Component: Testing framework Version: dev
Severity: normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

With the current django.test.client.Client class it is not possible to inspect an exception created by the code being tested.

For example, a view may raise an exception due to syntax error, malformed input from the test case or for any number of reasons. The exception is handled by Django and a standard error view (either a 500 page or error debug page) is returned with no other indication about the original exception. It is therefore difficult to track down the actual cause of the error.

A simple patch can handle the got_request_exception signal created by the base handler and capture the actual exception object and it's associated exc_info. The exception can then be reraised, complete with the original frame/location information, after the handler has finished. The exception can then be inspected by the test case. The standard behaviour of the test case is to print a stack trace to stdout, which is very useful when creating or fixing tests. As the exception location information is not modified, the original cause can be found and fixed.

Attachments (2)

test_client_exception_catch.patch (1.9 KB ) - added by afternoon@… 18 years ago.
[patch] Reraise exceptions created during request handling so test cases may inspect them
text_client_exception_raise_v2.patch (5.4 KB ) - added by afternoon@… 18 years ago.
[patch] Reraise exceptions created during request handling so test cases may inspect them, added test and docs

Download all attachments as: .zip

Change History (7)

by afternoon@…, 18 years ago

[patch] Reraise exceptions created during request handling so test cases may inspect them

by afternoon@…, 18 years ago

[patch] Reraise exceptions created during request handling so test cases may inspect them, added test and docs

comment:1 by anonymous, 18 years ago

Has patch: set
Needs documentation: set
Needs tests: set

comment:2 by Chris Beaven, 18 years ago

Needs documentation: unset
Needs tests: unset
Triage Stage: UnreviewedReady for checkin

Certainly seems like a good idea and a good quality patch. Maybe a bit overzealous of me, but I'll fast-track to "ready".

comment:3 by anonymous, 18 years ago

Hmm, I have another version of this patch with tests and docs ready to go, except that it's causing one of the pre-existing Django tests to fail. I've been meaning to fix it for two weeks. I will get my act together.

comment:4 by afternoon@…, 18 years ago

The current version of the patch causes the test_view_with_bad_login test from tests/modeltests/test_client/models.py to fail. The auth module throws an exception. Somehow _ becomes bound to an Article object during this test. Adding from django.utils.translation import gettext_lazy as _ to django.contrib.auth.forms fixes the test.

Here is a stack trace of the exception occurring:

Traceback (most recent call last):
  File "/Users/ben/Documents/Code/Python/django_svn/tests/modeltests/test_client/models.py", line 108, in test_view_with_bad_login
    response = self.client.login('/test_client/login_protected_view/', 'otheruser', 'nopassword')
  File "/Users/ben/Documents/Code/Python/django_svn/django/test/client.py", line 236, in login
    response = self.post(login_path, data=form_data, **extra)
  File "/Users/ben/Documents/Code/Python/django_svn/django/test/client.py", line 188, in post
    return self.request(**r)
  File "/Users/ben/Documents/Code/Python/django_svn/django/core/handlers/base.py", line 77, in get_response
    response = callback(request, *callback_args, **callback_kwargs)
  File "/Users/ben/Documents/Code/Python/lib/django/contrib/auth/views.py", line 16, in login
    errors = manipulator.get_validation_errors(request.POST)
  File "/Users/ben/Documents/Code/Python/lib/django/oldforms/__init__.py", line 59, in get_validation_errors
    errors.update(field.get_validation_errors(new_data))
  File "/Users/ben/Documents/Code/Python/lib/django/oldforms/__init__.py", line 357, in get_validation_errors
    self.run_validator(new_data, validator)
  File "/Users/ben/Documents/Code/Python/lib/django/oldforms/__init__.py", line 347, in run_validator
    validator(new_data.get(self.field_name, ''), new_data)
  File "/Users/ben/Documents/Code/Python/django_svn/django/contrib/auth/forms.py", line 59, in isValidUser
    raise validators.ValidationError, _("Please enter a correct username and password. Note that both fields are case-sensitive.")
TypeError: 'Article' object is not callable

comment:5 by Russell Keith-Magee, 18 years ago

Resolution: fixed
Status: newclosed

(In [4482]) Fixed #3162 -- Added coded to catch and rethrow exceptions that are thrown by the views visited by the test client. Thanks, Ben <afternoon@…>.

Note: See TracTickets for help on using tickets.
Back to Top