summaryrefslogtreecommitdiff
path: root/tests/regressiontests/test_utils
diff options
context:
space:
mode:
authorLuke Plant <L.Plant.98@cantab.net>2010-11-11 15:06:20 +0000
committerLuke Plant <L.Plant.98@cantab.net>2010-11-11 15:06:20 +0000
commit02fc6276d7577dcec26be30f3805b242ff0ce8a0 (patch)
treea85f54ee41330e4289169d7cac4ee04b55e22ba6 /tests/regressiontests/test_utils
parent7beca4d3e5da784297ff7163d00dcfeee9a34dd6 (diff)
Fixed #14508 - test suite silences warnings.
Utility functions get_warnings_state and save_warnings_state have been added to django.test.utils, and methods to django.test.TestCase for convenience. The implementation is based on the catch_warnings context manager from Python 2.6. git-svn-id: http://code.djangoproject.com/svn/django/trunk@14526 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'tests/regressiontests/test_utils')
-rw-r--r--tests/regressiontests/test_utils/tests.py29
1 files changed, 29 insertions, 0 deletions
diff --git a/tests/regressiontests/test_utils/tests.py b/tests/regressiontests/test_utils/tests.py
index 995306e01d..2e55ba7718 100644
--- a/tests/regressiontests/test_utils/tests.py
+++ b/tests/regressiontests/test_utils/tests.py
@@ -26,6 +26,35 @@ class SkippingTestCase(TestCase):
self.assertRaises(ValueError, test_func)
+class SaveRestoreWarningState(TestCase):
+
+ def test_save_restore_warnings_state(self):
+ """
+ Ensure save_warnings_state/restore_warnings_state work correctly.
+ """
+ # In reality this test could be satisfied by many broken implementations
+ # of save_warnings_state/restore_warnings_state (e.g. just
+ # warnings.resetwarnings()) , but it is difficult to test more.
+ import warnings
+ self.save_warnings_state()
+
+ class MyWarning(Warning):
+ pass
+
+ # Add a filter that causes an exception to be thrown, so we can catch it
+ warnings.simplefilter("error", MyWarning)
+ self.assertRaises(Warning, lambda: warnings.warn("warn", MyWarning))
+
+ # Now restore.
+ self.restore_warnings_state()
+ # After restoring, we shouldn't get an exception. But we don't want a
+ # warning printed either, so we have to silence the warning.
+ warnings.simplefilter("ignore", MyWarning)
+ warnings.warn("warn", MyWarning)
+
+ # Remove the filter we just added.
+ self.restore_warnings_state()
+
__test__ = {"API_TEST": r"""
# Some checks of the doctest output normalizer.
# Standard doctests do fairly