diff options
| -rw-r--r-- | src/distutils2/tests/__init__.py | 78 | ||||
| -rw-r--r-- | src/runtests.py | 12 |
2 files changed, 83 insertions, 7 deletions
diff --git a/src/distutils2/tests/__init__.py b/src/distutils2/tests/__init__.py index 61c9b98..08c508b 100644 --- a/src/distutils2/tests/__init__.py +++ b/src/distutils2/tests/__init__.py @@ -16,9 +16,10 @@ import os import sys import unittest2 - here = os.path.dirname(__file__) +verbose = 1 + def test_suite(): suite = unittest2.TestSuite() @@ -30,6 +31,81 @@ def test_suite(): suite.addTest(module.test_suite()) return suite +class Error(Exception): + """Base class for regression test exceptions.""" + + +class TestFailed(Error): + """Test failed.""" + + +class BasicTestRunner: + def run(self, test): + result = unittest2.TestResult() + test(result) + return result + + +def _run_suite(suite): + """Run tests from a unittest2.TestSuite-derived class.""" + if verbose: + runner = unittest2.TextTestRunner(sys.stdout, verbosity=2) + else: + runner = BasicTestRunner() + + result = runner.run(suite) + if not result.wasSuccessful(): + if len(result.errors) == 1 and not result.failures: + err = result.errors[0][1] + elif len(result.failures) == 1 and not result.errors: + err = result.failures[0][1] + else: + err = "errors occurred; run in verbose mode for details" + raise TestFailed(err) + + +def run_unittest(*classes): + """Run tests from unittest2.TestCase-derived classes. + + Extracted from stdlib test.test_support and modified to support unittest2. + """ + valid_types = (unittest2.TestSuite, unittest2.TestCase) + suite = unittest2.TestSuite() + for cls in classes: + if isinstance(cls, str): + if cls in sys.modules: + suite.addTest(unittest2.findTestCases(sys.modules[cls])) + else: + raise ValueError("str arguments must be keys in sys.modules") + elif isinstance(cls, valid_types): + suite.addTest(cls) + else: + suite.addTest(unittest2.makeSuite(cls)) + _run_suite(suite) + + +def reap_children(): + """Use this function at the end of test_main() whenever sub-processes + are started. This will help ensure that no extra children (zombies) + stick around to hog resources and create problems when looking + for refleaks. + + Extracted from stdlib test.test_support. + """ + + # Reap all our dead child processes so we don't leave zombies around. + # These hog resources and might be causing some of the buildbots to die. + if hasattr(os, 'waitpid'): + any_process = -1 + while True: + try: + # This will raise an exception on Windows. That's ok. + pid, status = os.waitpid(any_process, os.WNOHANG) + if pid == 0: + break + except: + break + if __name__ == "__main__": unittest2.main(defaultTest="test_suite") diff --git a/src/runtests.py b/src/runtests.py index ef9481f..151c462 100644 --- a/src/runtests.py +++ b/src/runtests.py @@ -1,18 +1,18 @@ -"""Tests for distutils. +"""Tests for distutils2. -The tests for distutils are defined in the distutils.tests package; +The tests for distutils are defined in the distutils2.tests package; the test_suite() function there returns a test suite that's ready to be run. """ import distutils2.tests -import test.test_support +from distutils2.tests import run_unittest, reap_children def test_main(): - test.test_support.run_unittest(distutils2.tests.test_suite()) - test.test_support.reap_children() - + run_unittest(distutils2.tests.test_suite()) + reap_children() + if __name__ == "__main__": test_main() |
