diff options
author | Joshua Harlow <harlowja@yahoo-inc.com> | 2015-03-25 13:29:00 -0700 |
---|---|---|
committer | Joshua Harlow <harlowja@yahoo-inc.com> | 2015-05-04 12:47:21 -0700 |
commit | a48a685eca8bc262c118b1f25b66ee021ba55b53 (patch) | |
tree | a316aaf1edeb566f93bbb1bae2d34d17fafbf5e0 /fixtures/tests | |
parent | d47cc58afaa8c18ee9750acb434c87713500a013 (diff) | |
download | fixtures-git-a48a685eca8bc262c118b1f25b66ee021ba55b53.tar.gz |
Add a warnings module capture fixure
Capturing the warnings module output (which is typically used
for deprecating code or functions or modules) is quite useful and
is a frequent operation that can be required to perform. So provide
a fixture that is similar (but not the same) as the warnings
``catch_warnings`` context manager that can be used to gather all
warnings emitted and allows people to later analyze them to ensure
they are as they expect.
Diffstat (limited to 'fixtures/tests')
-rw-r--r-- | fixtures/tests/_fixtures/test_warnings.py | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/fixtures/tests/_fixtures/test_warnings.py b/fixtures/tests/_fixtures/test_warnings.py new file mode 100644 index 0000000..4731626 --- /dev/null +++ b/fixtures/tests/_fixtures/test_warnings.py @@ -0,0 +1,49 @@ +# fixtures: Fixtures with cleanups for testing and convenience. +# +# Licensed under either the Apache License, Version 2.0 or the BSD 3-clause +# license at the users choice. A copy of both licenses are available in the +# project source as Apache-2.0 and BSD. You may not use this file except in +# compliance with one of these two licences. +# +# Unless required by applicable law or agreed to in writing, software +# distributed under these licenses is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# license you chose for the specific language governing permissions and +# limitations under that license. + +import warnings + +import testtools + +import fixtures + + +class TestWarnings(testtools.TestCase, fixtures.TestWithFixtures): + + def test_capture_reuse(self): + w = fixtures.WarningsCapture() + with w: + warnings.warn("test", DeprecationWarning) + self.assertEqual(1, len(w.captures)) + with w: + self.assertEqual([], w.captures) + + def test_capture_message(self): + w = self.useFixture(fixtures.WarningsCapture()) + warnings.warn("hi", DeprecationWarning) + self.assertEqual(1, len(w.captures)) + self.assertEqual("hi", str(w.captures[0].message)) + + def test_capture_category(self): + w = self.useFixture(fixtures.WarningsCapture()) + categories = [ + DeprecationWarning, Warning, UserWarning, + SyntaxWarning, RuntimeWarning, + UnicodeWarning, FutureWarning, + ] + for category in categories: + warnings.warn("test", category) + self.assertEqual(len(categories), len(w.captures)) + for i, category in enumerate(categories): + c = w.captures[i] + self.assertEqual(category, c.category) |