diff options
author | Stephen Finucane <stephenfin@redhat.com> | 2021-12-24 11:21:24 +0000 |
---|---|---|
committer | Stephen Finucane <stephen@that.guru> | 2022-10-19 11:51:24 +0100 |
commit | 4cd367fb4bb9038c3b236532173ea68ba8c6d5c3 (patch) | |
tree | 696ce9e09f38ed3a19df4e619c683f9a80cd3ad6 /fixtures/tests | |
parent | f0376a18496f2707f41a028882a500b83192b8d0 (diff) | |
download | fixtures-git-4cd367fb4bb9038c3b236532173ea68ba8c6d5c3.tar.gz |
Add WarningsFilter fixture
This has enough users around OpenStack to justify adding it to
'fixtures' proper. It's intentionally dumb, since the main purpose of
this is to avoid people calling `resetwarnings` in their variant of the
fixture, as that clears *all* filters including those we don't control.
Signed-off-by: Stephen Finucane <stephenfin@redhat.com>
Diffstat (limited to 'fixtures/tests')
-rw-r--r-- | fixtures/tests/_fixtures/test_warnings.py | 59 |
1 files changed, 58 insertions, 1 deletions
diff --git a/fixtures/tests/_fixtures/test_warnings.py b/fixtures/tests/_fixtures/test_warnings.py index c9e68bf..4b037be 100644 --- a/fixtures/tests/_fixtures/test_warnings.py +++ b/fixtures/tests/_fixtures/test_warnings.py @@ -18,11 +18,12 @@ import testtools import fixtures -class TestWarnings(testtools.TestCase, fixtures.TestWithFixtures): +class TestWarningsCapture(testtools.TestCase, fixtures.TestWithFixtures): def test_capture_reuse(self): # DeprecationWarnings are hidden by default in Python 3.2+, enable them # https://docs.python.org/3/library/warnings.html#default-warning-filter + self.useFixture(fixtures.WarningsFilter()) warnings.simplefilter("always") w = fixtures.WarningsCapture() @@ -35,6 +36,7 @@ class TestWarnings(testtools.TestCase, fixtures.TestWithFixtures): def test_capture_message(self): # DeprecationWarnings are hidden by default in Python 3.2+, enable them # https://docs.python.org/3/library/warnings.html#default-warning-filter + self.useFixture(fixtures.WarningsFilter()) warnings.simplefilter("always") w = self.useFixture(fixtures.WarningsCapture()) @@ -45,6 +47,7 @@ class TestWarnings(testtools.TestCase, fixtures.TestWithFixtures): def test_capture_category(self): # DeprecationWarnings are hidden by default in Python 3.2+, enable them # https://docs.python.org/3/library/warnings.html#default-warning-filter + self.useFixture(fixtures.WarningsFilter()) warnings.simplefilter("always") w = self.useFixture(fixtures.WarningsCapture()) @@ -59,3 +62,57 @@ class TestWarnings(testtools.TestCase, fixtures.TestWithFixtures): for i, category in enumerate(categories): c = w.captures[i] self.assertEqual(category, c.category) + + +class TestWarningsFilter(testtools.TestCase, fixtures.TestWithFixtures): + + def test_filter(self): + fixture = fixtures.WarningsFilter( + [ + { + 'action': 'ignore', + 'category': DeprecationWarning, + }, + { + 'action': 'once', + 'category': UserWarning, + }, + ], + ) + self.useFixture(fixture) + with warnings.catch_warnings(record=True) as w: + warnings.warn('deprecated', DeprecationWarning) + warnings.warn('user', UserWarning) + + # only the user warning should be present, and it should only have been + # raised once + self.assertEqual(1, len(w)) + + def test_filters_restored(self): + + class CustomWarning(Warning): + pass + + fixture = fixtures.WarningsFilter( + [ + { + 'action': 'once', + 'category': CustomWarning, + }, + ], + ) + + # we copy the filter values rather than a reference to the containing + # list since that can change + old_filters = warnings.filters[:] + + # NOTE: we intentionally do not use 'self.useFixture' since we want to + # teardown the fixture manually here before we exit this test method + with fixture: + new_filters = warnings.filters[:] + self.assertEqual(len(old_filters) + 1, len(new_filters)) + self.assertNotEqual(old_filters, new_filters) + + new_filters = warnings.filters[:] + self.assertEqual(len(old_filters), len(new_filters)) + self.assertEqual(old_filters, new_filters) |