Django

Code

Ticket #3162 (closed: fixed)

Opened 3 years ago

Last modified 2 years ago

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

Reported by: afternoon@uk2.net Assigned to: russellm
Milestone: Component: Testing framework
Version: SVN Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

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

test_client_exception_catch.patch (1.9 kB) - added by afternoon@uk2.net on 12/18/06 09:53:23.
[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@uk2.net on 12/20/06 22:08:52.
[patch] Reraise exceptions created during request handling so test cases may inspect them, added test and docs

Change History

12/18/06 09:53:23 changed by afternoon@uk2.net

  • attachment test_client_exception_catch.patch added.

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

12/20/06 22:08:52 changed by afternoon@uk2.net

  • attachment text_client_exception_raise_v2.patch added.

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

01/18/07 12:58:39 changed by anonymous

  • needs_docs set to 1.
  • has_patch set to 1.
  • needs_tests set to 1.

01/18/07 14:37:11 changed by SmileyChris

  • needs_docs deleted.
  • needs_tests deleted.
  • stage changed from Unreviewed to Ready 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".

01/19/07 04:24:30 changed by anonymous

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.

01/21/07 13:47:30 changed by afternoon@uk2.net

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

02/10/07 18:23:31 changed by russellm

  • status changed from new to closed.
  • resolution set to fixed.

(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@uk2.net>.


Add/Change #3162 (Test client should re-raise exceptions created while rendering a page)




Change Properties
Action