Standardize on test.test_support.run_unittest() (as opposed to a mix of run_unittest() and run_suite()). Also, add functionality to run_unittest() that admits usage of unittest.TestLoader.loadTestsFromModule().

This commit is contained in:
Collin Winter 2007-04-25 17:29:52 +00:00
parent 0d4c06e06e
commit c2898c5a67
29 changed files with 87 additions and 180 deletions

View File

@ -196,7 +196,9 @@ regression tests.
This module defines the following exceptions:
\begin{excdesc}{TestFailed}
Exception to be raised when a test fails.
Exception to be raised when a test fails. This is deprecated in favor
of \module{unittest}-based tests and \class{unittest.TestCase}'s
assertion methods.
\end{excdesc}
\begin{excdesc}{TestSkipped}
@ -273,14 +275,18 @@ filter settings.
Execute \class{unittest.TestCase} subclasses passed to the function.
The function scans the classes for methods starting with the prefix
\samp{test_} and executes the tests individually.
This is the preferred way to execute tests.
\end{funcdesc}
\begin{funcdesc}{run_suite}{suite\optional{, testclass}}
Execute the \class{unittest.TestSuite} instance \var{suite}.
The optional argument \var{testclass} accepts one of the test classes in the
suite so as to print out more detailed information on where the testing suite
originated from.
It is also legal to pass strings as parameters; these should be keys in
\code{sys.modules}. Each associated module will be scanned by
\code{unittest.TestLoader.loadTestsFromModule()}. This is usually seen in
the following \function{test_main()} function:
\begin{verbatim}
def test_main():
test_support.run_unittest(__name__)
\end{verbatim}
This will run all tests defined in the named module.
\end{funcdesc}
The \module{test.test_support} module defines the following classes:

View File

@ -40,18 +40,22 @@ Java implementation of Beck's original SmallTalk test framework. Please
see the documentation of the unittest_ module for detailed information on
the interface and general guidelines on writing unittest-based tests.
The test_support helper module provides two functions for use by
unittest-based tests in the Python regression testing framework:
The test_support helper module provides a function for use by
unittest-based tests in the Python regression testing framework,
``run_unittest()``. This is the primary way of running tests in the
standard library. You can pass it any number of the following:
- ``run_unittest()`` takes a number of ``unittest.TestCase`` derived classes as
parameters and runs the tests defined in those classes.
- ``run_suite()`` takes a populated ``TestSuite`` instance and runs the
tests.
``run_suite()`` is preferred because unittest files typically grow multiple
test classes, and you might as well be prepared.
- classes derived from or instances of ``unittest.TestCase`` or
``unittest.TestSuite``. These will be handed off to unittest for
converting into a proper TestSuite instance.
- a string; this must be a key in sys.modules. The module associated with
that string will be scanned by ``unittest.TestLoader.loadTestsFromModule``.
This is usually seen as ``test_support.run_unittest(__name__)`` in a test
module's ``test_main()`` function. This has the advantage of picking up
new tests automatically, without you having to add each new test case
manually.
All test methods in the Python regression framework have names that
start with "``test_``" and use lower-case names with words separated with
underscores.
@ -96,11 +100,7 @@ looks like this (with minor variations)::
...etc...
def test_main():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(MyTestCase1))
suite.addTest(unittest.makeSuite(MyTestCase2))
...add more suites...
test_support.run_suite(suite)
test_support.run_unittest(__name__)
if __name__ == "__main__":
test_main()

View File

@ -183,16 +183,8 @@ class BaseXYTestCase(unittest.TestCase):
def suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(LegacyBase64TestCase))
suite.addTest(unittest.makeSuite(BaseXYTestCase))
return suite
def test_main():
test_support.run_suite(suite())
test_support.run_unittest(__name__)
if __name__ == '__main__':
unittest.main(defaultTest='suite')
test_main()

View File

@ -4,11 +4,11 @@ Run all test cases.
"""
import sys
import unittest
from test.test_support import requires, verbose, run_suite, unlink
from test.test_support import requires, verbose, run_unittest, unlink
# When running as a script instead of within the regrtest framework, skip the
# requires test, since it's obvious we want to run them.
if __name__ <> '__main__':
if __name__ != '__main__':
requires('bsddb')
verbose = False
@ -58,9 +58,7 @@ def suite():
# For invocation through regrtest
def test_main():
tests = suite()
run_suite(tests)
run_unittest(suite())
# For invocation as a script
if __name__ == '__main__':
@ -73,4 +71,4 @@ if __name__ == '__main__':
print 'python version: %s' % sys.version
print '-=' * 38
unittest.main(defaultTest='suite')
test_main()

View File

@ -51,11 +51,7 @@ class Test_GB18030(test_multibytecodec_support.TestBase, unittest.TestCase):
has_iso10646 = True
def test_main():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(Test_GB2312))
suite.addTest(unittest.makeSuite(Test_GBK))
suite.addTest(unittest.makeSuite(Test_GB18030))
test_support.run_suite(suite)
test_support.run_unittest(__name__)
if __name__ == "__main__":
test_main()

View File

@ -21,9 +21,7 @@ class Test_Big5HKSCS(test_multibytecodec_support.TestBase, unittest.TestCase):
)
def test_main():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(Test_Big5HKSCS))
test_support.run_suite(suite)
test_support.run_unittest(__name__)
if __name__ == "__main__":
test_main()

View File

@ -99,13 +99,7 @@ class Test_SJISX0213(test_multibytecodec_support.TestBase, unittest.TestCase):
)
def test_main():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(Test_CP932))
suite.addTest(unittest.makeSuite(Test_EUC_JISX0213))
suite.addTest(unittest.makeSuite(Test_EUC_JP_COMPAT))
suite.addTest(unittest.makeSuite(Test_SJIS_COMPAT))
suite.addTest(unittest.makeSuite(Test_SJISX0213))
test_support.run_suite(suite)
test_support.run_unittest(__name__)
if __name__ == "__main__":
test_main()

View File

@ -45,11 +45,7 @@ class Test_JOHAB(test_multibytecodec_support.TestBase, unittest.TestCase):
)
def test_main():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(Test_CP949))
suite.addTest(unittest.makeSuite(Test_EUCKR))
suite.addTest(unittest.makeSuite(Test_JOHAB))
test_support.run_suite(suite)
test_support.run_unittest(__name__)
if __name__ == "__main__":
test_main()

View File

@ -21,9 +21,7 @@ class Test_Big5(test_multibytecodec_support.TestBase, unittest.TestCase):
)
def test_main():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(Test_Big5))
test_support.run_suite(suite)
test_support.run_unittest(__name__)
if __name__ == "__main__":
test_main()

View File

@ -20,10 +20,7 @@ class TestGBKMap(test_multibytecodec_support.TestBase_Mapping,
'MICSFT/WINDOWS/CP936.TXT'
def test_main():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(TestGB2312Map))
suite.addTest(unittest.makeSuite(TestGBKMap))
test_support.run_suite(suite)
test_support.run_unittest(__name__)
if __name__ == "__main__":
test_main()

View File

@ -14,9 +14,7 @@ class TestBig5HKSCSMap(test_multibytecodec_support.TestBase_Mapping,
mapfileurl = 'http://people.freebsd.org/~perky/i18n/BIG5HKSCS.TXT'
def test_main():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(TestBig5HKSCSMap))
test_support.run_suite(suite)
test_support.run_unittest(__name__)
if __name__ == "__main__":
test_main()

View File

@ -61,13 +61,7 @@ class TestSJISX0213Map(test_multibytecodec_support.TestBase_Mapping,
def test_main():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(TestCP932Map))
suite.addTest(unittest.makeSuite(TestEUCJPCOMPATMap))
suite.addTest(unittest.makeSuite(TestSJISCOMPATMap))
suite.addTest(unittest.makeSuite(TestEUCJISX0213Map))
suite.addTest(unittest.makeSuite(TestSJISX0213Map))
test_support.run_suite(suite)
test_support.run_unittest(__name__)
if __name__ == "__main__":
test_main()

View File

@ -34,11 +34,7 @@ class TestJOHABMap(test_multibytecodec_support.TestBase_Mapping,
pass_dectest = [('\\', u'\u20a9')]
def test_main():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(TestCP949Map))
suite.addTest(unittest.makeSuite(TestEUCKRMap))
suite.addTest(unittest.makeSuite(TestJOHABMap))
test_support.run_suite(suite)
test_support.run_unittest(__name__)
if __name__ == "__main__":
test_main()

View File

@ -25,10 +25,7 @@ class TestCP950Map(test_multibytecodec_support.TestBase_Mapping,
]
def test_main():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(TestBIG5Map))
suite.addTest(unittest.makeSuite(TestCP950Map))
test_support.run_suite(suite)
test_support.run_unittest(__name__)
if __name__ == "__main__":
test_main()

View File

@ -9,7 +9,7 @@ import tempfile
import unittest
import threading
from contextlib import * # Tests __all__
from test.test_support import run_suite
from test import test_support
class ContextManagerTestCase(unittest.TestCase):
@ -332,9 +332,7 @@ class LockContextTestCase(unittest.TestCase):
# This is needed to make the test actually run under regrtest.py!
def test_main():
run_suite(
unittest.defaultTestLoader.loadTestsFromModule(sys.modules[__name__])
)
test_support.run_unittest(__name__)
if __name__ == "__main__":
test_main()

View File

@ -1,12 +1,12 @@
import unittest
from test.test_support import run_suite
from test.test_support import run_unittest
import ctypes.test
def test_main():
skipped, testcases = ctypes.test.get_tests(ctypes.test, "test_*.py", verbosity=0)
suites = [unittest.makeSuite(t) for t in testcases]
run_suite(unittest.TestSuite(suites))
run_unittest(unittest.TestSuite(suites))
if __name__ == "__main__":
test_main()

View File

@ -128,7 +128,7 @@ class TestTZInfo(unittest.TestCase):
# Base clase for testing a particular aspect of timedelta, time, date and
# datetime comparisons.
class HarmlessMixedComparison(unittest.TestCase):
class HarmlessMixedComparison:
# Test that __eq__ and __ne__ don't complain for mixed-type comparisons.
# Subclasses must define 'theclass', and theclass(1, 1, 1) must be a
@ -167,7 +167,7 @@ class HarmlessMixedComparison(unittest.TestCase):
#############################################################################
# timedelta tests
class TestTimeDelta(HarmlessMixedComparison):
class TestTimeDelta(HarmlessMixedComparison, unittest.TestCase):
theclass = timedelta
@ -514,7 +514,7 @@ class TestDateOnly(unittest.TestCase):
class SubclassDate(date):
sub_var = 1
class TestDate(HarmlessMixedComparison):
class TestDate(HarmlessMixedComparison, unittest.TestCase):
# Tests here should pass for both dates and datetimes, except for a
# few tests that TestDateTime overrides.
@ -1596,7 +1596,7 @@ class TestDateTime(TestDate):
class SubclassTime(time):
sub_var = 1
class TestTime(HarmlessMixedComparison):
class TestTime(HarmlessMixedComparison, unittest.TestCase):
theclass = time
@ -1879,7 +1879,7 @@ class TestTime(HarmlessMixedComparison):
# A mixin for classes with a tzinfo= argument. Subclasses must define
# theclass as a class atribute, and theclass(1, 1, 1, tzinfo=whatever)
# must be legit (which is true for time and datetime).
class TZInfoBase(unittest.TestCase):
class TZInfoBase:
def test_argument_passing(self):
cls = self.theclass
@ -2039,7 +2039,7 @@ class TZInfoBase(unittest.TestCase):
# Testing time objects with a non-None tzinfo.
class TestTimeTZ(TestTime, TZInfoBase):
class TestTimeTZ(TestTime, TZInfoBase, unittest.TestCase):
theclass = time
def test_empty(self):
@ -2287,7 +2287,7 @@ class TestTimeTZ(TestTime, TZInfoBase):
# Testing datetime objects with a non-None tzinfo.
class TestDateTimeTZ(TestDateTime, TZInfoBase):
class TestDateTimeTZ(TestDateTime, TZInfoBase, unittest.TestCase):
theclass = datetime
def test_trivial(self):
@ -3248,31 +3248,13 @@ class Oddballs(unittest.TestCase):
self.assertEqual(as_datetime, datetime_sc)
self.assertEqual(datetime_sc, as_datetime)
def test_suite():
allsuites = [unittest.makeSuite(klass, 'test')
for klass in (TestModule,
TestTZInfo,
TestTimeDelta,
TestDateOnly,
TestDate,
TestDateTime,
TestTime,
TestTimeTZ,
TestDateTimeTZ,
TestTimezoneConversions,
Oddballs,
)
]
return unittest.TestSuite(allsuites)
def test_main():
import gc
import sys
thesuite = test_suite()
lastrc = None
while True:
test_support.run_suite(thesuite)
test_support.run_unittest(__name__)
if 1: # change to 0, under a debug build, for some leak detection
break
gc.collect()

View File

@ -4,10 +4,10 @@
import unittest
# The specific tests now live in Lib/email/test
from email.test.test_email import suite
from test.test_support import run_suite
from test import test_support
def test_main():
run_suite(suite())
test_support.run_unittest(suite())
if __name__ == '__main__':
test_main()

View File

@ -9,7 +9,7 @@ from test import test_support
def test_main():
suite = test_email_codecs.suite()
suite.addTest(test_email_codecs_renamed.suite())
test_support.run_suite(suite)
test_support.run_unittest(suite)
if __name__ == '__main__':
test_main()

View File

@ -4,10 +4,10 @@
import unittest
# The specific tests now live in Lib/email/test
from email.test.test_email_renamed import suite
from test.test_support import run_suite
from test import test_support
def test_main():
run_suite(suite())
test_support.run_unittest(suite())
if __name__ == '__main__':
test_main()

View File

@ -4,7 +4,7 @@ import shutil
import gettext
import unittest
from test.test_support import run_suite
from test import test_support
# TODO:
@ -336,19 +336,8 @@ class WeirdMetadataTest(GettextBaseTest):
'John Doe <jdoe@example.com>\nJane Foobar <jfoobar@example.com>')
def suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(GettextTestCase1))
suite.addTest(unittest.makeSuite(GettextTestCase2))
suite.addTest(unittest.makeSuite(PluralFormsTestCase))
suite.addTest(unittest.makeSuite(UnicodeTranslationsTest))
suite.addTest(unittest.makeSuite(WeirdMetadataTest))
return suite
def test_main():
run_suite(suite())
test_support.run_unittest(__name__)
if __name__ == '__main__':
test_main()

View File

@ -219,13 +219,7 @@ class Test_ISO2022(unittest.TestCase):
myunichr(x).encode('iso_2022_jp', 'ignore')
def test_main():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(Test_MultibyteCodec))
suite.addTest(unittest.makeSuite(Test_IncrementalEncoder))
suite.addTest(unittest.makeSuite(Test_IncrementalDecoder))
suite.addTest(unittest.makeSuite(Test_StreamWriter))
suite.addTest(unittest.makeSuite(Test_ISO2022))
test_support.run_suite(suite)
test_support.run_unittest(__name__)
if __name__ == "__main__":
test_main()

View File

@ -1631,18 +1631,8 @@ class TestParseNumber(BaseTest):
"option -l: invalid long integer value: '0x12x'")
def _testclasses():
mod = sys.modules[__name__]
return [getattr(mod, name) for name in dir(mod) if name.startswith('Test')]
def suite():
suite = unittest.TestSuite()
for testclass in _testclasses():
suite.addTest(unittest.makeSuite(testclass))
return suite
def test_main():
test_support.run_suite(suite())
test_support.run_unittest(__name__)
if __name__ == '__main__':
unittest.main()
test_main()

View File

@ -135,8 +135,8 @@ bad = [] # Bug report says "/" should be denied, but that is not in the RFC
RobotTest(7, doc, good, bad)
def test_main():
test_support.run_suite(tests)
test_support.run_unittest(tests)
if __name__=='__main__':
test_support.Verbose = 1
test_support.run_suite(tests)
test_main()

View File

@ -8,6 +8,7 @@ import errno
import socket
import sys
import warnings
import types
class Error(Exception):
"""Base class for regression test exceptions."""
@ -519,7 +520,7 @@ class BasicTestRunner:
return result
def run_suite(suite, testclass=None):
def _run_suite(suite):
"""Run tests from a unittest.TestSuite-derived class."""
if verbose:
runner = unittest.TextTestRunner(sys.stdout, verbosity=2)
@ -533,28 +534,26 @@ def run_suite(suite, testclass=None):
elif len(result.failures) == 1 and not result.errors:
err = result.failures[0][1]
else:
if testclass is None:
msg = "errors occurred; run in verbose mode for details"
else:
msg = "errors occurred in %s.%s" \
% (testclass.__module__, testclass.__name__)
msg = "errors occurred; run in verbose mode for details"
raise TestFailed(msg)
raise TestFailed(err)
def run_unittest(*classes):
"""Run tests from unittest.TestCase-derived classes."""
valid_types = (unittest.TestSuite, unittest.TestCase)
suite = unittest.TestSuite()
for cls in classes:
if isinstance(cls, (unittest.TestSuite, unittest.TestCase)):
if isinstance(cls, str):
if cls in sys.modules:
suite.addTest(unittest.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(unittest.makeSuite(cls))
if len(classes)==1:
testclass = classes[0]
else:
testclass = None
run_suite(suite, testclass)
_run_suite(suite)
#=======================================================================

View File

@ -20,7 +20,7 @@ def test_main():
setUp=setUp, tearDown=tearDown)
)
test_support.run_suite(suite)
test_support.run_unittest(suite)
if __name__ == '__main__':
test_main()

View File

@ -822,7 +822,7 @@ class UnicodeTest(
def test_main():
test_support.run_unittest(UnicodeTest)
test_support.run_unittest(__name__)
if __name__ == "__main__":
test_main()

View File

@ -5,7 +5,7 @@ import os, glob, time, shutil
import unicodedata
import unittest
from test.test_support import run_suite, TestSkipped, TESTFN_UNICODE
from test.test_support import run_unittest, TestSkipped, TESTFN_UNICODE
from test.test_support import TESTFN_ENCODING, TESTFN_UNICODE_UNENCODEABLE
try:
TESTFN_ENCODED = TESTFN_UNICODE.encode(TESTFN_ENCODING)
@ -205,9 +205,7 @@ class TestUnicodeFiles(unittest.TestCase):
False)
def test_main():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(TestUnicodeFiles))
run_suite(suite)
run_unittest(__name__)
if __name__ == "__main__":
test_main()

View File

@ -1,5 +1,5 @@
from __future__ import nested_scopes # Backward compat for 2.1
from unittest import TestSuite, TestCase, makeSuite
from unittest import TestCase
from wsgiref.util import setup_testing_defaults
from wsgiref.headers import Headers
from wsgiref.handlers import BaseHandler, BaseCGIHandler
@ -11,6 +11,7 @@ from StringIO import StringIO
from SocketServer import BaseServer
import re, sys
from test import test_support
class MockServer(WSGIServer):
"""Non-socket HTTP server"""
@ -575,11 +576,7 @@ class HandlerTests(TestCase):
# This epilogue is needed for compatibility with the Python 2.5 regrtest module
def test_main():
import unittest
from test.test_support import run_suite
run_suite(
unittest.defaultTestLoader.loadTestsFromModule(sys.modules[__name__])
)
test_support.run_unittest(__name__)
if __name__ == "__main__":
test_main()