Rev 3217: Rewrite tests.test_suite() to enhance readability, add sanity checks. in file:///v/home/vila/src/bzr/experimental/selftest/
Vincent Ladeuil
v.ladeuil+lp at free.fr
Fri Feb 15 13:51:00 GMT 2008
At file:///v/home/vila/src/bzr/experimental/selftest/
------------------------------------------------------------
revno: 3217
revision-id:v.ladeuil+lp at free.fr-20080215135055-5gk3fpdyoytnbfj8
parent: v.ladeuil+lp at free.fr-20080215132504-m43kszd99ufmwxr3
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: selftest
timestamp: Fri 2008-02-15 14:50:55 +0100
message:
Rewrite tests.test_suite() to enhance readability, add sanity checks.
* bzrlib/tests/test_selftest.py:
(TestTestIdList.test_test_suite): Update test, well only the
comments had become wrong ;-)
* bzrlib/tests/__init__.py:
(test_suite): Create a dedicated loader when a test id list is
provided. Load DocTest by module *name* to avoid imports when
possible (and have a processing coherent with other
tests). Rewrite test id list handling in a more readable way. Add
sanity checks.
(multiply_tests_from_modules): Add an optional loader parameter.
modified:
bzrlib/tests/__init__.py selftest.py-20050531073622-8d0e3c8845c97a64
bzrlib/tests/test_selftest.py test_selftest.py-20051202044319-c110a115d8c0456a
-------------- next part --------------
=== modified file 'bzrlib/tests/__init__.py'
--- a/bzrlib/tests/__init__.py 2008-02-15 13:25:04 +0000
+++ b/bzrlib/tests/__init__.py 2008-02-15 13:50:55 +0000
@@ -95,8 +95,8 @@
from bzrlib.tests import TestUtil
from bzrlib.tests.http_server import HttpServer
from bzrlib.tests.TestUtil import (
+ TestLoader,
TestSuite,
- TestLoader,
)
from bzrlib.tests.treeshape import build_tree_contents
import bzrlib.version_info_formats.format_custom
@@ -107,33 +107,9 @@
# shown frame is the test code, not our assertXYZ.
__unittest = 1
+
default_transport = LocalURLServer
-MODULES_TO_DOCTEST = [
- bzrlib.timestamp,
- bzrlib.errors,
- bzrlib.export,
- bzrlib.inventory,
- bzrlib.iterablefile,
- bzrlib.lockdir,
- bzrlib.merge3,
- bzrlib.option,
- bzrlib.store,
- bzrlib.version_info_formats.format_custom,
- # quoted to avoid module-loading circularity
- 'bzrlib.tests',
- ]
-
-
-def packages_to_test():
- """Return a list of packages to test.
-
- The packages are not globally imported so that import failures are
- triggered when running selftest, not when importing the command.
- """
- return [
- ]
-
class ExtendedTestResult(unittest._TextTestResult):
"""Accepts, reports and accumulates the results of running tests.
@@ -2774,54 +2750,57 @@
'bzrlib.tests.tree_implementations',
'bzrlib.tests.workingtree_implementations',
]
- suite = TestUtil.TestSuite()
- loader = TestUtil.TestLoader()
-
- if keep_only is not None:
+ if keep_only is None:
+ loader = TestUtil.TestLoader()
+ else:
id_filter = TestIdList(keep_only)
+ exclude_module = lambda (name): not id_filter.refers_to(name)
+ loader = TestUtil.FilteredByModuleTestLoader(exclude_module)
+ suite = loader.suiteClass()
# modules building their suite with loadTestsFromModuleNames
- if keep_only is None:
- suite.addTest(loader.loadTestsFromModuleNames(testmod_names))
- else:
- for mod in [m for m in testmod_names if id_filter.refers_to(m)]:
- mod_suite = loader.loadTestsFromModuleNames([mod])
- mod_suite = filter_suite_by_id_list(mod_suite, id_filter)
- suite.addTest(mod_suite)
-
- # modules defining their own test_suite()
- for package in [p for p in packages_to_test()
- if (keep_only is None or id_filter.refers_to(p.__name__))]:
- pack_suite = package.test_suite()
- if keep_only is not None:
- pack_suite = filter_suite_by_id_list(pack_suite, id_filter)
- suite.addTest(pack_suite)
-
- for mod in MODULES_TO_DOCTEST:
+ suite.addTest(loader.loadTestsFromModuleNames(testmod_names))
+
+ modules_to_doctest = [
+ 'bzrlib.errors',
+ 'bzrlib.export',
+ 'bzrlib.inventory',
+ 'bzrlib.iterablefile',
+ 'bzrlib.lockdir',
+ 'bzrlib.merge3',
+ 'bzrlib.option',
+ 'bzrlib.store',
+ 'bzrlib.tests',
+ 'bzrlib.timestamp',
+ 'bzrlib.version_info_formats.format_custom',
+ ]
+
+ for mod in modules_to_doctest:
+ if not (keep_only is None or id_filter.refers_to(mod)):
+ # No tests to keep here, move along
+ continue
try:
doc_suite = doctest.DocTestSuite(mod)
except ValueError, e:
print '**failed to get doctest for: %s\n%s' % (mod, e)
raise
- if keep_only is not None:
- # DocTests may use ids which doesn't contain the module name
- doc_suite = filter_suite_by_id_list(doc_suite, id_filter)
suite.addTest(doc_suite)
default_encoding = sys.getdefaultencoding()
- for name, plugin in [(n, p) for (n, p) in bzrlib.plugin.plugins().items()
- if (keep_only is None
- or id_filter.refers_to(p.module.__name__))]:
+ for name, plugin in bzrlib.plugin.plugins().items():
+ if not (keep_only is None
+ or id_filter.refers_to(plugin.module.__name__)):
+ # No tests to keep here, move along
+ continue
try:
+ # XXX: It would be nice if plugins were able to define
+ # load_tests(), but that may not be worth it --vila 080215
plugin_suite = plugin.test_suite()
except ImportError, e:
bzrlib.trace.warning(
'Unable to test plugin "%s": %s', name, e)
else:
if plugin_suite is not None:
- if keep_only is not None:
- plugin_suite = filter_suite_by_id_list(plugin_suite,
- id_filter)
suite.addTest(plugin_suite)
if default_encoding != sys.getdefaultencoding():
bzrlib.trace.warning(
@@ -2829,6 +2808,18 @@
sys.getdefaultencoding())
reload(sys)
sys.setdefaultencoding(default_encoding)
+
+ if keep_only is not None:
+ # Now that the referred modules have loaded their tests, keep only the
+ # requested ones.
+ suite = filter_suite_by_id_list(suite, id_filter)
+ # Do some checks
+ not_found, duplicates = suite_matches_id_list(suite, keep_only)
+ for id in not_found:
+ bzrlib.trace.warning('%s not found in the test suite', id)
+ for id in duplicates:
+ bzrlib.trace.warning('%s is used as an id by several tests', id)
+
return suite
@@ -2845,8 +2836,7 @@
:param scenario_iter: Iterable of pairs of (scenario_name,
scenario_param_dict).
:param loader: If provided, will be used instead of a new
- bzrlib.tests.TestLoader() instance. It will also be used to get
- a TestSuite object.
+ bzrlib.tests.TestLoader() instance.
This returns a new TestSuite containing the cross product of
all the tests in all the modules, each repeated for each scenario.
@@ -2868,11 +2858,12 @@
>>> tests[1].param
2
"""
+ # XXX: Isn't load_tests() a better way to provide the same functionality
+ # without forcing a predefined TestScenarioApplier ? --vila 080215
if loader is None:
- loader = TestLoader()
- suite = TestSuite()
- else:
- suite = loader.suiteClass()
+ loader = TestUtil.TestLoader()
+
+ suite = loader.suiteClass()
adapter = TestScenarioApplier()
adapter.scenarios = list(scenario_iter)
=== modified file 'bzrlib/tests/test_selftest.py'
--- a/bzrlib/tests/test_selftest.py 2008-02-15 13:25:04 +0000
+++ b/bzrlib/tests/test_selftest.py 2008-02-15 13:50:55 +0000
@@ -1980,14 +1980,12 @@
# This test is slow, so we do a single test with one test in each
# category
test_list = [
- # packages_to_test()
+ # testmod_names
'bzrlib.tests.blackbox.test_branch.TestBranch.test_branch',
- # testmod_names
'bzrlib.tests.test_selftest.TestTestIdList.test_test_suite',
- # transport implementations
'bzrlib.tests.test_transport_implementations.TransportTests'
'.test_abspath(LocalURLServer)',
- # MODULES_TO_DOCTEST
+ # modules_to_doctest
'bzrlib.timestamp.format_highres_date',
# plugins can't be tested that way since selftest may be run with
# --no-plugins
More information about the bazaar-commits
mailing list