diff options
author | Jelmer Vernooij <jelmer@jelmer.uk> | 2022-10-19 13:04:44 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-19 13:04:44 +0200 |
commit | c735451ee11587f1edd36e022be4dd4078df55a3 (patch) | |
tree | 5b72e14c7553f9eac9fb7a1dbf2821955e3c7b52 /fixtures/tests | |
parent | f9c6653afff17951cd0523a1fe100754d37af50a (diff) | |
parent | ecc3d8f3f3d031fefb28c14511ae43da94974f32 (diff) | |
download | fixtures-git-c735451ee11587f1edd36e022be4dd4078df55a3.tar.gz |
Merge pull request #50 from stephenfin/warnings-filter
Add WarningsFilter fixture
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) |