You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@bloodhound.apache.org by ju...@apache.org on 2013/02/01 13:14:19 UTC
svn commit: r1441417 -
/incubator/bloodhound/branches/bep_0003_multiproduct/bloodhound_multiproduct/tests/__init__.py
Author: jure
Date: Fri Feb 1 12:14:18 2013
New Revision: 1441417
URL: http://svn.apache.org/viewvc?rev=1441417&view=rev
Log:
#355, test discovery patch t355_r1437383_pkgresources_discovery.diff (from Olemis)
Modified:
incubator/bloodhound/branches/bep_0003_multiproduct/bloodhound_multiproduct/tests/__init__.py
Modified: incubator/bloodhound/branches/bep_0003_multiproduct/bloodhound_multiproduct/tests/__init__.py
URL: http://svn.apache.org/viewvc/incubator/bloodhound/branches/bep_0003_multiproduct/bloodhound_multiproduct/tests/__init__.py?rev=1441417&r1=1441416&r2=1441417&view=diff
==============================================================================
--- incubator/bloodhound/branches/bep_0003_multiproduct/bloodhound_multiproduct/tests/__init__.py (original)
+++ incubator/bloodhound/branches/bep_0003_multiproduct/bloodhound_multiproduct/tests/__init__.py Fri Feb 1 12:14:18 2013
@@ -15,3 +15,62 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
+
+from collections import deque
+from fnmatch import fnmatch
+import sys
+try:
+ import unittest2 as unittest
+except ImportError:
+ import unittest
+
+from pkg_resources import resource_listdir, resource_isdir, resource_exists
+
+
+class TestLoader(unittest.TestLoader):
+ testLoaderAttribute = '__testloader__'
+ testMethodPrefix = 'test'
+ sortTestMethodsUsing = cmp
+ suiteClass = unittest.TestSuite
+
+ def discover_package(self, package_or_requirement, pattern='test*.py'):
+ """Find and return all test modules from the specified package
+ directory, recursing into subdirectories to find them. Only test files
+ that match the pattern will be loaded. (Using shell style pattern
+ matching.)
+
+ All test modules must be importable from the top level of the project
+ and registered with `pkg_resources` (e.g. via `setup.py develop`).
+
+ If a target test module contains a '__testloader__' attribute then
+ related object will override current loader for every individual
+ module across the hierarchy.
+ """
+ pending = deque([(package_or_requirement, self, True)])
+ tests = []
+ while pending:
+ mdlnm, loader, isdir = pending.popleft()
+ mdl = self._get_module_from_name(mdlnm)
+ loader = getattr(mdl, self.testLoaderAttribute, None) or loader
+ if mdlnm != package_or_requirement and hasattr(mdl, 'test_suite'):
+ tests.append(mdl.test_suite())
+ else:
+ tests.append(loader.loadTestsFromModule(mdl))
+ if isdir and resource_exists(mdlnm, '__init__.py'):
+ for fnm in resource_listdir(mdlnm, ''):
+ if resource_isdir(mdlnm, fnm):
+ pending.append( (mdlnm + '.' + fnm, loader, True) )
+ elif any(fnm.endswith(ext) for ext in ['.py', '.pyc']) \
+ and fnmatch(fnm, pattern) and fnm != '__init__.py':
+ submdlnm = mdlnm + '.' + fnm.rsplit('.', 1)[0]
+ pending.append( (submdlnm, loader, False) )
+ return self.suiteClass(tests)
+
+ def _get_module_from_name(self, name):
+ __import__(name)
+ return sys.modules[name]
+
+def test_suite():
+ return TestLoader().discover_package('tests', pattern='*.py')
+
+