Opened 6 years ago

Closed 5 years ago

#14503 closed Cleanup/optimization (fixed)

Unify test for exception+message value assertion method

Reported by: Ramiro Morales Owned by: nobody
Component: Testing framework Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

Currently we have a few copies of methods with similar tasks:

$ ack def\ assertRaises --python
[...]
tests/regressiontests/forms/fields.py
52:    def assertRaisesErrorWithMessage(self, error, message, callable, *args, **kwargs):

tests/regressiontests/forms/localflavor/be.py
11:    def assertRaisesErrorWithMessage(self, error, message, callable, *args, **kwargs):

tests/regressiontests/utils/datastructures.py
11:    def assertRaisesErrorWithMessage(self, error, message, callable,

tests/regressiontests/queries/tests.py
24:    def assertRaisesMessage(self, exc, msg, func, *args, **kwargs):

tests/regressiontests/fixtures_regress/tests.py
340:    def assertRaisesMessage(self, exc, msg, func, *args, **kwargs):

tests/regressiontests/urlpatterns_reverse/tests.py
136:    def assertRaisesErrorWithMessage(self, error, message, callable,

tests/regressiontests/custom_columns_regress/tests.py
12:    def assertRaisesMessage(self, exc, msg, func, *args, **kwargs):

tests/regressiontests/admin_validation/tests.py
21:    def assertRaisesMessage(self, exc, msg, func, *args, **kwargs):

tests/regressiontests/file_storage/tests.py
35:    def assertRaisesErrorWithMessage(self, error, message, callable,

And some of them even run the callable twice:

    def assertRaisesErrorWithMessage(self, error, message, callable, *args, **kwargs):
        self.assertRaises(error, callable, *args, **kwargs)
        try:
            callable(*args, **kwargs)
        except error, e:
            self.assertEqual(message, str(e))

Attachments (6)

14503-assertraisesmessage-unification-simple.diff (73.4 KB) - added by Ramiro Morales 6 years ago.
Simple unification using a new django.test.{Transaction,}TestCase() method
14503-replacement-with-assertraisesregexp.diff (82.7 KB) - added by Ramiro Morales 6 years ago.
Different strategy as sugested by Alex: Migrate to use assertRaisesRegexp() unittest2.TestCase method
14503-replacement-with-assertraisesregexp.2.diff (82.4 KB) - added by Ramiro Morales 6 years ago.
Same as previous one, moving inheritance of some non-DB tests from django.test.TestCase back to django.utils.unittest.TestCase
14503-assertraisesregex-r14981.diff (79.7 KB) - added by Ramiro Morales 6 years ago.
Updated patch for trunk as of now
14503-with-mixin.diff (70.1 KB) - added by Ramiro Morales 5 years ago.
Uupdated patch
14503-with-testcase-class.diff (70.4 KB) - added by Ramiro Morales 5 years ago.
Similar patch but using a thin subclass of django.utuls.unitest.TestCase

Download all attachments as: .zip

Change History (9)

Changed 6 years ago by Ramiro Morales

Simple unification using a new django.test.{Transaction,}TestCase() method

Changed 6 years ago by Ramiro Morales

Different strategy as sugested by Alex: Migrate to use assertRaisesRegexp() unittest2.TestCase method

Changed 6 years ago by Ramiro Morales

Same as previous one, moving inheritance of some non-DB tests from django.test.TestCase back to django.utils.unittest.TestCase

Changed 6 years ago by Ramiro Morales

Updated patch for trunk as of now

comment:1 Changed 6 years ago by Ramiro Morales

Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset
Triage Stage: UnreviewedAccepted

comment:2 Changed 5 years ago by Julien Phalip

Severity: Normal
Type: Cleanup/optimization

Changed 5 years ago by Ramiro Morales

Attachment: 14503-with-mixin.diff added

Uupdated patch

Changed 5 years ago by Ramiro Morales

Similar patch but using a thin subclass of django.utuls.unitest.TestCase

comment:3 Changed 5 years ago by Ramiro Morales

Resolution: fixed
Status: newclosed

In [16610]:

Fixed #14503 -- Unified multiple implementations of test cases assert* methods that verify a given exception is raised by a callable throughout the Django test suite.

Replaced them with a new assertRaisesMessage method of a new SimpleTestCase, a lightweight subclass of unittest.TestCase. Both are also available for usage in user tests.

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