[Python-checkins] cpython (3.4): Fix test_warnings: don't modify warnings.filters

victor.stinner python-checkins at python.org
Thu Sep 3 00:10:10 CEST 2015


https://hg.python.org/cpython/rev/c1396d28c440
changeset:   97594:c1396d28c440
branch:      3.4
parent:      97591:f394be80bc7a
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Thu Sep 03 00:07:47 2015 +0200
summary:
  Fix test_warnings: don't modify warnings.filters

BaseTest now ensures that unittest.TestCase.assertWarns() uses the same
warnings module than warnings.catch_warnings(). Otherwise,
warnings.catch_warnings() will be unable to remove the added filter.

files:
  Lib/test/test_warnings.py |  6 ++++++
  1 files changed, 6 insertions(+), 0 deletions(-)


diff --git a/Lib/test/test_warnings.py b/Lib/test/test_warnings.py
--- a/Lib/test/test_warnings.py
+++ b/Lib/test/test_warnings.py
@@ -44,6 +44,7 @@
     """Basic bookkeeping required for testing."""
 
     def setUp(self):
+        self.old_unittest_module = unittest.case.warnings
         # The __warningregistry__ needs to be in a pristine state for tests
         # to work properly.
         if '__warningregistry__' in globals():
@@ -55,10 +56,15 @@
         # The 'warnings' module must be explicitly set so that the proper
         # interaction between _warnings and 'warnings' can be controlled.
         sys.modules['warnings'] = self.module
+        # Ensure that unittest.TestCase.assertWarns() uses the same warnings
+        # module than warnings.catch_warnings(). Otherwise,
+        # warnings.catch_warnings() will be unable to remove the added filter.
+        unittest.case.warnings = self.module
         super(BaseTest, self).setUp()
 
     def tearDown(self):
         sys.modules['warnings'] = original_warnings
+        unittest.case.warnings = self.old_unittest_module
         super(BaseTest, self).tearDown()
 
 class PublicAPITests(BaseTest):

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list