Opened 3 weeks ago

Last modified 2 weeks ago

#37076 assigned Bug

assertWarnsMessage() only checks the first warning for a matching message

Reported by: Mike Edmunds Owned by: artirix1927
Component: Testing framework Version: 6.0
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

assertWarnsMessage(category, message) fails if the first warning issued in category does not contain message, even if some later warning does match.

As a result, test cases depend on the specific order warnings are issued. This can become confusing when there are several related deprecations going in at once, or when a feature being tested uses some other feature that also issues a warning.

For example, you might expect this test to pass, but it fails with AssertionError: 'Deprecated in MyApp' not found in 'Deprecated in Django':

class MyAppTests(SimpleTestCase):
    def test_deprecated_in_my_app(self):
        with self.assertWarnsMessage(DeprecationWarning, "Deprecated in MyApp"):
            # MyApp calls something in Django that happens to be deprecated:
            warnings.warn("Deprecated in Django", RemovedInNextVersionWarning)
            # Then MyApp issues the warning being tested:
            warnings.warn("Deprecated in MyApp", DeprecationWarning)

If you switch the order of the two warnings, the test passes. (And the second warning is then ignored—see #37072.)

There is a workaround by combining a very carefully crafted ignore_warnings() context with assertWarnsMessage(). (It requires knowing that the semi-documented ignore_warnings() also accepts a message argument. And that it interprets it quite differently from assertWarnsMessage().)

Change History (5)

comment:1 by Artyom Kotovskiy, 3 weeks ago

Would love to work on it in case it gets accepted.

comment:2 by Jacob Walls, 3 weeks ago

Triage Stage: UnreviewedAccepted

Ouch, great find. This is supposed to be a non-regex version of the stdlib assertWarnsRegex, which isn't order-sensitive.

comment:3 by Jacob Walls, 3 weeks ago

Owner: set to artirix1927
Status: newassigned

comment:4 by Mike Edmunds, 3 weeks ago

I would suggest trying to fix #37072 at the same time—they're closely related. I outlined a possible approach in a comment there. There are 3-4 existing tests that seem to unintentionally depend on either this or #37072's behavior, so will need to be updated as part of the fix.

comment:5 by Artyom Kotovskiy, 2 weeks ago

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