diff options
| author | Konrad Delong <konryd@gmail.com> | 2010-08-15 00:18:54 +0200 |
|---|---|---|
| committer | Konrad Delong <konryd@gmail.com> | 2010-08-15 00:18:54 +0200 |
| commit | dff6f3c4538d145bb39c3f36a77eb025a0b792a2 (patch) | |
| tree | e9934b37507067dd1ca81ab543a405321b7aaa0f /src | |
| parent | 9dbb076c4e6e664343f0dcf6c7bd822905048e45 (diff) | |
| download | disutils2-dff6f3c4538d145bb39c3f36a77eb025a0b792a2.tar.gz | |
even better warnings test
Diffstat (limited to 'src')
| -rw-r--r-- | src/distutils2/tests/support.py | 25 | ||||
| -rw-r--r-- | src/distutils2/tests/test_test.py | 21 |
2 files changed, 28 insertions, 18 deletions
diff --git a/src/distutils2/tests/support.py b/src/distutils2/tests/support.py index fa83b30..0c69e58 100644 --- a/src/distutils2/tests/support.py +++ b/src/distutils2/tests/support.py @@ -4,8 +4,8 @@ Always import unittest from this module, it will be the right version (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 os import sys import shutil import tempfile +import warnings from copy import deepcopy from distutils2 import log @@ -58,7 +59,6 @@ class LoggingSilencer(object): 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 @@ class EnvironGuard(object): 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 index 616d5c0..ab18ef6 100644 --- a/src/distutils2/tests/test_test.py +++ b/src/distutils2/tests/test_test.py @@ -8,7 +8,7 @@ import warnings 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 @@ def with_ut_isolated(func): return wrapper class TestTest(TempdirManager, + WarningsCatcher, unittest.TestCase): def setUp(self): @@ -123,20 +124,10 @@ class TestTest(TempdirManager, 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() |
