Rev 2475: Another helper to make the test_suite() implementations trivial. in http://bzr.arbash-meinel.com/branches/bzr/0.17-dev/test_autoloader

John Arbash Meinel john at arbash-meinel.com
Fri Apr 27 23:34:43 BST 2007


At http://bzr.arbash-meinel.com/branches/bzr/0.17-dev/test_autoloader

------------------------------------------------------------
revno: 2475
revision-id: john at arbash-meinel.com-20070427223214-8ut70ar9op2qz7zm
parent: john at arbash-meinel.com-20070427221911-ehmmqy22jijis3ko
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: test_autoloader
timestamp: Fri 2007-04-27 17:32:14 -0500
message:
  Another helper to make the test_suite() implementations trivial.
  Update most adapters to use the new helper.
modified:
  bzrlib/tests/__init__.py       selftest.py-20050531073622-8d0e3c8845c97a64
  bzrlib/tests/blackbox/__init__.py __init__.py-20051128053524-eba30d8255e08dc3
  bzrlib/tests/blackbox/encoding_tests/__init__.py __init__.py-20070427205653-dxoru4wmr60nyag0-1
  bzrlib/tests/transport_implementations/__init__.py __init__.py-20070427204159-rs104bugblkajzg0-1
  bzrlib/tests/workingtree_implementations/__init__.py __init__.py-20060203003124-b2aa5aca21a8bfad
-------------- next part --------------
=== modified file 'bzrlib/tests/__init__.py'
--- a/bzrlib/tests/__init__.py	2007-04-27 20:47:34 +0000
+++ b/bzrlib/tests/__init__.py	2007-04-27 22:32:14 +0000
@@ -2244,8 +2244,7 @@
     suite on a global basis, but it is not encouraged.
     """
     loader = TestLoader()
-    path = os.path.dirname(__file__)
-    suite = loader.recursiveLoadTests(path, __name__)
+    suite = load_and_adapt_modules(None, __file__, __name__)
     for package in packages_to_test():
         suite.addTest(package.test_suite())
     for m in MODULES_TO_TEST:
@@ -2275,6 +2274,43 @@
     return suite
 
 
+def load_and_adapt_modules(adapter, context_fname, context_python_name):
+    """Load and adapt modules in the given context.
+
+    This is a helper for test suites. In most cases test_suite() can be
+    implemented as a simple call to::
+
+        def test_suite():
+            return load_and_adapt_modules(None, __file__, __name__)
+
+    or::
+
+        def test_suite():
+            from foo import MyAdapter
+            adapter = MyAdapter()
+            return load_and_adapt_modules(adapter, __file__, __name__)
+
+    This uses '__file__' and '__name__' to search for available tests.
+    And then passes all tests through adapter if it is supplied.
+
+    :param adapter: All tests will be adapted with adapter.adapt() or None
+    :param context_fname: Usually __file__.
+    :param context_python_name: Usually __name__.
+    :return: A TestSuite
+    """
+    suite = TestSuite()
+    loader = TestLoader()
+    if adapter is None:
+        return loader.recursiveLoadTests(os.path.dirname(context_fname),
+                                         context_python_name)
+
+    modules, packages = loader.findTestModules(os.path.dirname(context_fname),
+                                               context_python_name)
+    adapt_modules(modules, adapter, loader, suite)
+    suite.addTests(loader.loadTestsFromPackageNames(packages))
+    return suite
+
+
 def adapt_modules(mods_list, adapter, loader, suite):
     """Adapt the modules in mods_list using adapter and add to suite."""
     for test in iter_suite_tests(loader.loadTestsFromModuleNames(mods_list)):

=== modified file 'bzrlib/tests/blackbox/__init__.py'
--- a/bzrlib/tests/blackbox/__init__.py	2007-04-27 21:03:23 +0000
+++ b/bzrlib/tests/blackbox/__init__.py	2007-04-27 22:32:14 +0000
@@ -32,8 +32,7 @@
 
 
 def test_suite():
-    loader = tests.TestLoader()
-    return loader.recursiveLoadTests(os.path.dirname(__file__), __name__)
+    return tests.load_and_adapt_modules(None, __file__, __name__)
 
 
 class ExternalBase(tests.TestCaseWithTransport):

=== modified file 'bzrlib/tests/blackbox/encoding_tests/__init__.py'
--- a/bzrlib/tests/blackbox/encoding_tests/__init__.py	2007-04-27 22:19:11 +0000
+++ b/bzrlib/tests/blackbox/encoding_tests/__init__.py	2007-04-27 22:32:14 +0000
@@ -23,11 +23,5 @@
 
 
 def test_suite():
-    loader = tests.TestLoader()
-    suite = tests.TestSuite()
-    modules, packages = loader.findTestModules(os.path.dirname(__file__), __name__)
-
     adapter = EncodingTestAdapter()
-    tests.adapt_modules(modules, adapter, loader, suite)
-    suite.addTests(loader.loadTestsFromPackageNames(packages))
-    return suite
+    return tests.load_and_adapt_modules(adapter, __file__, __name__)

=== modified file 'bzrlib/tests/transport_implementations/__init__.py'
--- a/bzrlib/tests/transport_implementations/__init__.py	2007-04-27 22:19:11 +0000
+++ b/bzrlib/tests/transport_implementations/__init__.py	2007-04-27 22:32:14 +0000
@@ -23,10 +23,5 @@
 
 def test_suite():
     from bzrlib.transport import TransportTestProviderAdapter
-    suite = tests.TestSuite()
-    loader = tests.TestLoader()
     adapter = TransportTestProviderAdapter()
-    modules, packages = loader.findTestModules(os.path.dirname(__file__), __name__)
-    tests.adapt_modules(modules, adapter, loader, suite)
-    suite.addTests(loader.loadTestsFromPackageNames(packages))
-    return suite
+    return tests.load_and_adapt_modules(adapter, __file__, __name__)

=== modified file 'bzrlib/tests/workingtree_implementations/__init__.py'
--- a/bzrlib/tests/workingtree_implementations/__init__.py	2007-04-27 22:19:11 +0000
+++ b/bzrlib/tests/workingtree_implementations/__init__.py	2007-04-27 22:32:14 +0000
@@ -24,13 +24,9 @@
 
 import os
 
-import bzrlib.errors as errors
-from bzrlib.transport import get_transport
 from bzrlib.tests import (
-                          adapt_modules,
                           default_transport,
-                          TestLoader,
-                          TestSuite,
+                          load_and_adapt_modules,
                           )
 from bzrlib.tests.bzrdir_implementations.test_bzrdir import TestCaseWithBzrDir
 from bzrlib.workingtree import (WorkingTreeFormat,
@@ -49,7 +45,6 @@
 
 
 def test_suite():
-    result = TestSuite()
     adapter = WorkingTreeTestProviderAdapter(
         default_transport,
         # None here will cause a readonly decorator to be created
@@ -57,8 +52,4 @@
         None,
         [(format, format._matchingbzrdir) for format in 
          WorkingTreeFormat._formats.values() + _legacy_formats])
-    loader = TestLoader()
-    modules, packages = loader.findTestModules(os.path.dirname(__file__), __name__)
-    adapt_modules(modules, adapter, loader, result)
-    result.addTests(loader.loadTestsFromPackageNames(packages))
-    return result
+    return load_and_adapt_modules(adapter, __file__, __name__)



More information about the bazaar-commits mailing list