[Django] #36280: Replace cm.exception.messages with assertRaisesMessage() in tests

30 views
Skip to first unread message

Django

unread,
Mar 27, 2025, 9:33:35 PMMar 27
to django-...@googlegroups.com
#36280: Replace cm.exception.messages with assertRaisesMessage() in tests
-------------------------------------+-------------------------------------
Reporter: Tim Graham | Type:
| Cleanup/optimization
Status: new | Component: Core
| (Other)
Version: dev | Severity: Normal
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
I haven't audited each instance, but most likely many of the uses of
`cm.exception.messages` could be made more consistent with the rest of the
Django test suite by instead using `self.assertRaisesMessage()`.

{{{
tests/composite_pk/test_models.py:
self.assertSequenceEqual(ctx.exception.messages, messages)
tests/composite_pk/test_models.py: ctx.exception.messages,
("User with this Email already exists.",)
tests/validation/test_error_messages.py:
self.assertEqual(cm.exception.messages, expected)
tests/auth_tests/test_validators.py:
self.assertEqual(cm.exception.messages, [msg_too_short])
tests/auth_tests/test_validators.py: cm.exception.messages,
["This password is too common.", msg_too_short]
tests/auth_tests/test_validators.py:
self.assertEqual(cm.exception.messages, [expected_error % 8])
tests/auth_tests/test_validators.py:
self.assertEqual(cm.exception.messages, [expected_error % 3])
tests/auth_tests/test_validators.py:
self.assertEqual(cm.exception.messages, [expected_error % "username"])
tests/auth_tests/test_validators.py:
self.assertEqual(cm.exception.messages, [expected_error % "email
address"])
tests/auth_tests/test_validators.py:
self.assertEqual(cm.exception.messages, [expected_error % "first name"])
tests/auth_tests/test_validators.py:
self.assertEqual(cm.exception.messages, [expected_error % "first name"])
tests/auth_tests/test_validators.py: cm.exception.messages,
["The password is too similar to the username."]
tests/auth_tests/test_validators.py:
self.assertEqual(cm.exception.messages, [expected_error])
tests/auth_tests/test_validators.py:
self.assertEqual(cm.exception.messages, [expected_error])
tests/auth_tests/test_validators.py:
self.assertEqual(cm.exception.messages, [expected_error])
tests/auth_tests/test_validators.py:
self.assertEqual(cm.exception.messages, [expected_error])
tests/forms_tests/tests/test_error_messages.py:
self.assertEqual(cm.exception.messages, expected)
tests/forms_tests/tests/test_validators.py: self.assertEqual(2,
len(e.exception.messages))
tests/test_exceptions/test_validation_error.py:
self.assertEqual(sorted(exception.messages), [])
tests/test_exceptions/test_validation_error.py:
self.assertEqual(sorted(exception.messages), ["E1", "E2"])
tests/test_exceptions/test_validation_error.py:
self.assertEqual(sorted(exception.messages), ["E1", "E2", "E3", "E4"])
tests/postgres_tests/test_array.py: cm.exception.messages[0],
tests/postgres_tests/test_array.py: cm.exception.messages[0],
"Nested arrays must have the same length."
tests/postgres_tests/test_array.py: cm.exception.messages[0],
tests/postgres_tests/test_array.py: cm.exception.messages[0],
tests/postgres_tests/test_array.py: cm.exception.messages[0],
tests/postgres_tests/test_array.py: cm.exception.messages[0],
tests/postgres_tests/test_array.py: cm.exception.messages[0],
tests/postgres_tests/test_array.py:
self.assertEqual(cm.exception.messages[0], "This field is required.")
tests/postgres_tests/test_array.py: cm.exception.messages,
tests/postgres_tests/test_array.py: cm.exception.messages,
tests/postgres_tests/test_hstore.py:
self.assertEqual(cm.exception.messages[0], "Could not load JSON data.")
tests/postgres_tests/test_hstore.py:
self.assertEqual(cm.exception.messages[0], "Some keys were missing: b")
tests/postgres_tests/test_hstore.py:
self.assertEqual(cm.exception.messages[0], "Some unknown keys were
provided: c")
tests/postgres_tests/test_hstore.py:
self.assertEqual(cm.exception.messages[0], "Foobar")
tests/postgres_tests/test_hstore.py:
self.assertEqual(cm.exception.messages[0], "Some unknown keys were
provided: c")
tests/postgres_tests/test_ranges.py:
self.assertEqual(cm.exception.messages[0], msg)
tests/postgres_tests/test_ranges.py:
self.assertEqual(cm.exception.messages[0], msg)
tests/postgres_tests/test_ranges.py: cm.exception.messages[0],
tests/postgres_tests/test_ranges.py:
self.assertEqual(cm.exception.messages[0], "Enter two whole numbers.")
tests/postgres_tests/test_ranges.py:
self.assertEqual(cm.exception.messages[0], "Enter a whole number.")
tests/postgres_tests/test_ranges.py:
self.assertEqual(cm.exception.messages[0], "Enter a whole number.")
tests/postgres_tests/test_ranges.py:
self.assertEqual(cm.exception.messages[0], "This field is required.")
tests/postgres_tests/test_ranges.py: cm.exception.messages[0],
tests/postgres_tests/test_ranges.py:
self.assertEqual(cm.exception.messages[0], "Enter two numbers.")
tests/postgres_tests/test_ranges.py:
self.assertEqual(cm.exception.messages[0], "Enter a number.")
tests/postgres_tests/test_ranges.py:
self.assertEqual(cm.exception.messages[0], "Enter a number.")
tests/postgres_tests/test_ranges.py:
self.assertEqual(cm.exception.messages[0], "This field is required.")
tests/postgres_tests/test_ranges.py: cm.exception.messages[0],
tests/postgres_tests/test_ranges.py:
self.assertEqual(cm.exception.messages[0], "Enter two valid dates.")
tests/postgres_tests/test_ranges.py:
self.assertEqual(cm.exception.messages[0], "Enter a valid date.")
tests/postgres_tests/test_ranges.py:
self.assertEqual(cm.exception.messages[0], "Enter a valid date.")
tests/postgres_tests/test_ranges.py:
self.assertEqual(cm.exception.messages[0], "This field is required.")
tests/postgres_tests/test_ranges.py: cm.exception.messages[0],
tests/postgres_tests/test_ranges.py:
self.assertEqual(cm.exception.messages[0], "Enter two valid date/times.")
tests/postgres_tests/test_ranges.py:
self.assertEqual(cm.exception.messages[0], "Enter a valid date/time.")
tests/postgres_tests/test_ranges.py:
self.assertEqual(cm.exception.messages[0], "Enter a valid date/time.")
tests/postgres_tests/test_ranges.py:
self.assertEqual(cm.exception.messages[0], "This field is required.")
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36280>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Mar 28, 2025, 4:07:41 AMMar 28
to django-...@googlegroups.com
#36280: Replace cm.exception.messages with assertRaisesMessage() in tests
--------------------------------------+------------------------------------
Reporter: Tim Graham | Owner: (none)
Type: Cleanup/optimization | Status: new
Component: Core (Other) | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Sarah Boyce):

* stage: Unreviewed => Accepted

Comment:

Agree that most can be updated
Likely that the ones that check the error code (`cm.exception.code`) may
need to stay the same
--
Ticket URL: <https://code.djangoproject.com/ticket/36280#comment:1>

Django

unread,
Mar 29, 2025, 7:53:28 AMMar 29
to django-...@googlegroups.com
#36280: Replace cm.exception.messages with assertRaisesMessage() in tests
--------------------------------------+------------------------------------
Reporter: Tim Graham | Owner: (none)
Type: Cleanup/optimization | Status: new
Component: Core (Other) | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
--------------------------------------+------------------------------------
Comment (by aloony):

Replying to [comment:1 Sarah Boyce]:
> Agree that most can be updated
> Likely that the ones that check the error code (`cm.exception.code`) may
need to stay the same
I assume the same goes for assertEqual's that validate two lists of
errors? Since, assertRaisesMessage validates against a string, not a list
of them. Otherwise, we would need to write a new assert for that
specifically.
--
Ticket URL: <https://code.djangoproject.com/ticket/36280#comment:2>

Django

unread,
Mar 29, 2025, 7:58:31 AMMar 29
to django-...@googlegroups.com
#36280: Replace cm.exception.messages with assertRaisesMessage() in tests
--------------------------------------+------------------------------------
Reporter: Tim Graham | Owner: aloony
Type: Cleanup/optimization | Status: assigned
Component: Core (Other) | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by aloony):

* owner: (none) => aloony
* status: new => assigned

--
Ticket URL: <https://code.djangoproject.com/ticket/36280#comment:3>

Django

unread,
Mar 29, 2025, 1:54:23 PMMar 29
to django-...@googlegroups.com
#36280: Replace cm.exception.messages with assertRaisesMessage() in tests
-------------------------------------+-------------------------------------
Reporter: Tim Graham | Owner: Georgii
Type: | (George) Randiuk
Cleanup/optimization | Status: assigned
Component: Core (Other) | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Georgii (George) Randiuk):

* has_patch: 0 => 1
* needs_better_patch: 0 => 1


Old description:
New description:
PR link: https://github.com/django/django/pull/19317

--
--
Ticket URL: <https://code.djangoproject.com/ticket/36280#comment:4>

Django

unread,
Apr 7, 2025, 9:23:38 AMApr 7
to django-...@googlegroups.com
#36280: Replace cm.exception.messages with assertRaisesMessage() in tests
-------------------------------------+-------------------------------------
Reporter: Tim Graham | Owner: Georgii
Type: | (George) Randiuk
Cleanup/optimization | Status: assigned
Component: Core (Other) | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by yussypu):

Hi! I noticed the current patch for this ticket is marked as needing
improvement.

I’d love to help move this forward — would it be alright if I tried
improving the patch or opened a new PR based on the feedback so far?

Just want to make sure it’s okay before proceeding. Thanks in advance!

— Yahya
--
Ticket URL: <https://code.djangoproject.com/ticket/36280#comment:5>

Django

unread,
Apr 7, 2025, 9:37:02 AMApr 7
to django-...@googlegroups.com
#36280: Replace cm.exception.messages with assertRaisesMessage() in tests
-------------------------------------+-------------------------------------
Reporter: Tim Graham | Owner: Georgii
Type: | (George) Randiuk
Cleanup/optimization | Status: assigned
Component: Core (Other) | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Tim Graham):

No, you should wait at least a month before considering a patch abandoned
due to inactivity.
--
Ticket URL: <https://code.djangoproject.com/ticket/36280#comment:6>

Django

unread,
Apr 26, 2025, 7:42:08 AMApr 26
to django-...@googlegroups.com
#36280: Replace cm.exception.messages with assertRaisesMessage() in tests
-------------------------------------+-------------------------------------
Reporter: Tim Graham | Owner: Georgii
Type: | (George) Randiuk
Cleanup/optimization | Status: assigned
Component: Core (Other) | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by David Bogar):

* cc: David Bogar (added)
* stage: Accepted => Ready for checkin

--
Ticket URL: <https://code.djangoproject.com/ticket/36280#comment:7>

Django

unread,
Apr 26, 2025, 9:05:16 AMApr 26
to django-...@googlegroups.com
#36280: Replace cm.exception.messages with assertRaisesMessage() in tests
-------------------------------------+-------------------------------------
Reporter: Tim Graham | Owner: Georgii
Type: | (George) Randiuk
Cleanup/optimization | Status: assigned
Component: Core (Other) | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* stage: Ready for checkin => Accepted

Comment:

The patch is still marked as needs improvement.
--
Ticket URL: <https://code.djangoproject.com/ticket/36280#comment:8>

Django

unread,
May 5, 2025, 3:29:30 AMMay 5
to django-...@googlegroups.com
#36280: Replace cm.exception.messages with assertRaisesMessage() in tests
-------------------------------------+-------------------------------------
Reporter: Tim Graham | Owner: Georgii
Type: | (George) Randiuk
Cleanup/optimization | Status: assigned
Component: Core (Other) | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Clifford Gama):

The following pattern used to test assertions looks like it could also be
covered by this ticket, maybe with the exception of tests for
`assertRaisesMessage` itself?
{{{#!python
try:
self.assertRedirects(response, "/get_view/")
except AssertionError as e:
self.assertIn(
"Response redirected to '/get_view/?var=value', expected
'/get_view/'",
str(e),
)
}}}
e.g. in `test_client_regress.tests.*`, where this pattern is used
extensively and interchangeably with `assertRaisesMessage`.
--
Ticket URL: <https://code.djangoproject.com/ticket/36280#comment:9>

Django

unread,
Nov 16, 2025, 2:35:30 PMNov 16
to django-...@googlegroups.com
#36280: Replace cm.exception.messages with assertRaisesMessage() in tests
-------------------------------------+-------------------------------------
Reporter: Tim Graham | Owner: Sachi
Type: | Jain
Cleanup/optimization | Status: assigned
Component: Core (Other) | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sachi Jain):

* owner: Georgii (George) Randiuk => Sachi Jain

--
Ticket URL: <https://code.djangoproject.com/ticket/36280#comment:10>

Django

unread,
Nov 19, 2025, 9:37:31 AM (14 days ago) Nov 19
to django-...@googlegroups.com
#36280: Replace cm.exception.messages with assertRaisesMessage() in tests
-------------------------------------+-------------------------------------
Reporter: Tim Graham | Owner: Sachi
Type: | Jain
Cleanup/optimization | Status: assigned
Component: Core (Other) | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jacob Walls):

* needs_better_patch: 1 => 0

Comment:

[https://github.com/django/django/pull/20182 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/36280#comment:11>

Django

unread,
Dec 2, 2025, 3:43:40 PM (12 hours ago) Dec 2
to django-...@googlegroups.com
#36280: Replace cm.exception.messages with assertRaisesMessage() in tests
-------------------------------------+-------------------------------------
Reporter: Tim Graham | Owner: Sachi
Type: | Jain
Cleanup/optimization | Status: assigned
Component: Core (Other) | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jacob Walls):

* needs_better_patch: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/36280#comment:12>
Reply all
Reply to author
Forward
0 new messages