[Python-checkins] distutils2: even better warnings test

tarek.ziade python-checkins at python.org
Sun Sep 19 10:20:22 CEST 2010


tarek.ziade pushed 7129748463ae to distutils2:

http://hg.python.org/distutils2/rev/7129748463ae
changeset:   628:7129748463ae
user:        Konrad Delong <konryd at gmail.com>
date:        Sun Aug 15 00:18:54 2010 +0200
summary:     even better warnings test
files:       src/distutils2/tests/support.py, src/distutils2/tests/test_test.py

diff --git a/src/distutils2/tests/support.py b/src/distutils2/tests/support.py
--- a/src/distutils2/tests/support.py
+++ b/src/distutils2/tests/support.py
@@ -4,8 +4,8 @@
 (standard library unittest for 3.2 and higher, third-party unittest2
 release for older versions).
 
-Three helper classes are provided: LoggingSilencer, TempdirManager and
-EnvironGuard. They are written to be used as mixins, e.g. ::
+Four helper classes are provided: LoggingSilencer, TempdirManager, EnvironGuard
+and WarningsCatcher. They are written to be used as mixins, e.g. ::
 
     from distutils2.tests.support import unittest
     from distutils2.tests.support import LoggingSilencer
@@ -30,6 +30,7 @@
 import sys
 import shutil
 import tempfile
+import warnings
 from copy import deepcopy
 
 from distutils2 import log
@@ -58,7 +59,6 @@
     def setUp(self):
         super(LoggingSilencer, self).setUp()
         self.threshold = log.set_threshold(FATAL)
-        # catching warnings
         # when log is replaced by logging we won't need
         # such monkey-patching anymore
         self._old_log = log.Log._log
@@ -172,6 +172,25 @@
         super(EnvironGuard, self).tearDown()
 
 
+class WarningsCatcher(object):
+    
+    def setUp(self):
+        self._orig_showwarning = warnings.showwarning
+        warnings.showwarning = self._record_showwarning
+        self.warnings = []
+
+    def _record_showwarning(self, message, category, filename, lineno, file=None, line=None):
+        self.warnings.append({"message": message,
+                              "category": category,
+                              "filename": filename,
+                              "lineno": lineno,
+                              "file": file,
+                              "line": line})
+
+    def tearDown(self):
+        warnings.showwarning = self._orig_showwarning
+
+
 class DummyCommand(object):
     """Class to store options for retrieval via set_undefined_options().
 
diff --git a/src/distutils2/tests/test_test.py b/src/distutils2/tests/test_test.py
--- a/src/distutils2/tests/test_test.py
+++ b/src/distutils2/tests/test_test.py
@@ -8,7 +8,7 @@
 from copy import copy
 from os.path import join
 from StringIO import StringIO
-from distutils2.tests.support import unittest, LoggingSilencer, TempdirManager
+from distutils2.tests.support import unittest, WarningsCatcher, TempdirManager
 from distutils2.command.test import test
 from distutils2.dist import Distribution
 
@@ -63,6 +63,7 @@
     return wrapper
 
 class TestTest(TempdirManager,
+               WarningsCatcher,
                unittest.TestCase):
 
     def setUp(self):
@@ -123,20 +124,10 @@
     def test_checks_requires(self):
         dist = Distribution()
         cmd = test(dist)
-        record = []
-        orig_showwarning = warnings.showwarning
-        def record_showwarning(*args):
-            record.append(args)
-            return orig_showwarning(*args)
-        try:
-            warnings.showwarning = record_showwarning
-            cmd.tests_require = ['ohno_ohno-impossible_1234-name_stop-that!']
-            cmd.ensure_finalized()
-            self.assertEqual(1, len(record))
-            warning = record[0]
-            self.assertIs(warning[1], RuntimeWarning)
-        finally:
-            warnings.showwarning = orig_showwarning
+        cmd.tests_require = ['ohno_ohno-impossible_1234-name_stop-that!']
+        cmd.ensure_finalized()
+        self.assertEqual(1, len(self.warnings))
+        self.assertIs(self.warnings[0]["category"], RuntimeWarning)
 
     def test_custom_runner(self):
         dist = Distribution()

--
Repository URL: http://hg.python.org/distutils2


More information about the Python-checkins mailing list