Skip to content

unittest.assertWarnsRegex swallows warnings that do not match the regex #143231

@bitdancer

Description

@bitdancer

Bug report

Bug description:

Suppose a bit of code emits two warnings, and you want to check that both are generated. In #69110 rbcollins said "...there's little if any reason to add support for multiple regexes - just nest two context managers." This doesn't work, though, since the inner context manager swallows the warnings and the outer one doesn't see them.

In addition, the fact that non-matching warnings are swallowed means that a test that checks for a warning will hide any other warnings that the code generates, which is also a bug in the sense that the behavior is unexpected and can lead to tests that hide potential issues from the test author.

Here is a minimal reproducer:

from unittest import TestCase, main
from warnings import warn

def foo():
    warn("msg1", DeprecationWarning)
    warn("msg2", UserWarning)

class Test(TestCase):

    def test_foo(self):
        with self.assertWarnsRegex(DeprecationWarning, 'msg1'):
            with self.assertWarnsRegex(UserWarning, 'msg2'):
                foo()

main()

This results in a test failure for 'msg1'. if you remove one of the asserts, the test will pass, but the second warning will not appear on the console.

I'm not sure the warnings machinery has the facilities needed to actually fix the second issue, but the unittest issue should be fixable.

In my actual use case the outer assert is made by a wrapper used by multiple tests, while the inner is made by an individual test, and both are DeprecationWarnings, so using the tuple feature of assertWarnsRegex would not solve my issue for multiple reasons.

CPython versions tested on:

CPython main branch

Operating systems tested on:

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions