Skip to content

Commit

Permalink
Fixed the test harness to work with Python 2.3 again (tested that it …
Browse files Browse the repository at this point in the history
…still

works with 2.4 and 2.5 as well).


git-svn-id: http://code.djangoproject.com/svn/django/trunk@5211 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
malcolmt committed May 12, 2007
1 parent 3780a45 commit c7d7b6d
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions django/test/testcases.py
Original file line number Original file line Diff line number Diff line change
@@ -1,4 +1,5 @@
import re, doctest, unittest import re, doctest, unittest
import sys
from urlparse import urlparse from urlparse import urlparse
from django.db import transaction from django.db import transaction
from django.core import management, mail from django.core import management, mail
Expand Down Expand Up @@ -45,16 +46,16 @@ def _pre_setup(self):
if hasattr(self, 'fixtures'): if hasattr(self, 'fixtures'):
management.load_data(self.fixtures, verbosity=0) management.load_data(self.fixtures, verbosity=0)
mail.outbox = [] mail.outbox = []

def run(self, result=None): def __call__(self, result=None):
"""Wrapper around default run method to perform common Django test set up. """
This means that user-defined Test Cases aren't required to include a call Wrapper around default __call__ method to perform common Django test
to super().setUp(). set up. This means that user-defined Test Cases aren't required to
include a call to super().setUp().
""" """
self.client = Client() self.client = Client()
self._pre_setup() self._pre_setup()
super(TestCase, self).run(result) super(TestCase, self).__call__(result)


def assertRedirects(self, response, expected_path, status_code=302, target_status_code=200): def assertRedirects(self, response, expected_path, status_code=302, target_status_code=200):
"""Assert that a response redirected to a specific URL, and that the """Assert that a response redirected to a specific URL, and that the
Expand Down Expand Up @@ -108,7 +109,7 @@ def assertFormError(self, response, form, field, errors):
for err in errors: for err in errors:
if field: if field:
if field in context[form].errors: if field in context[form].errors:
self.assertTrue(err in context[form].errors[field], self.failUnless(err in context[form].errors[field],
"The field '%s' on form '%s' in context %d does not contain the error '%s' (actual errors: %s)" % "The field '%s' on form '%s' in context %d does not contain the error '%s' (actual errors: %s)" %
(field, form, i, err, list(context[form].errors[field]))) (field, form, i, err, list(context[form].errors[field])))
elif field in context[form].fields: elif field in context[form].fields:
Expand All @@ -117,7 +118,7 @@ def assertFormError(self, response, form, field, errors):
else: else:
self.fail("The form '%s' in context %d does not contain the field '%s'" % (form, i, field)) self.fail("The form '%s' in context %d does not contain the field '%s'" % (form, i, field))
else: else:
self.assertTrue(err in context[form].non_field_errors(), self.failUnless(err in context[form].non_field_errors(),
"The form '%s' in context %d does not contain the non-field error '%s' (actual errors: %s)" % "The form '%s' in context %d does not contain the non-field error '%s' (actual errors: %s)" %
(form, i, err, list(context[form].non_field_errors()))) (form, i, err, list(context[form].non_field_errors())))
if not found_form: if not found_form:
Expand All @@ -127,7 +128,7 @@ def assertTemplateUsed(self, response, template_name):
"Assert that the template with the provided name was used in rendering the response" "Assert that the template with the provided name was used in rendering the response"
if isinstance(response.template, list): if isinstance(response.template, list):
template_names = [t.name for t in response.template] template_names = [t.name for t in response.template]
self.assertTrue(template_name in template_names, self.failUnless(template_name in template_names,
"Template '%s' was not one of the templates used to render the response. Templates used: %s" % "Template '%s' was not one of the templates used to render the response. Templates used: %s" %
(template_name, template_names)) (template_name, template_names))
elif response.template: elif response.template:
Expand All @@ -140,9 +141,9 @@ def assertTemplateUsed(self, response, template_name):
def assertTemplateNotUsed(self, response, template_name): def assertTemplateNotUsed(self, response, template_name):
"Assert that the template with the provided name was NOT used in rendering the response" "Assert that the template with the provided name was NOT used in rendering the response"
if isinstance(response.template, list): if isinstance(response.template, list):
self.assertFalse(template_name in [t.name for t in response.template], self.failIf(template_name in [t.name for t in response.template],
"Template '%s' was used unexpectedly in rendering the response" % template_name) "Template '%s' was used unexpectedly in rendering the response" % template_name)
elif response.template: elif response.template:
self.assertNotEqual(template_name, response.template.name, self.assertNotEqual(template_name, response.template.name,
"Template '%s' was used unexpectedly in rendering the response" % template_name) "Template '%s' was used unexpectedly in rendering the response" % template_name)


0 comments on commit c7d7b6d

Please sign in to comment.