Michael Foord avatar Michael Foord committed 939422c

Making the moduleloading plugin more modular

Comments (0)

Files changed (1)

unittest2/plugins/moduleloading.py

     parametersEnabled = False
     configSection = 'functions'
     commandLineSwitch = (None, 'functions', 'Load tests from functions')
+    unpack = enumerate
 
     def loadTestsFromName(self, event):
         name = event.name
             args['tearDown'] = tearDown
 
         paramList = getattr(obj, 'paramList', None)
-        isGenerator = getattr(obj, 'testGenerator', False)
+        isGenerator = self.isGenerator(obj)
         if self.parametersEnabled and paramList is not None:
             for index, argSet in enumerate(paramList):
                 def func(argSet=argSet, obj=obj):
             name = '%s.%s' % (obj.__module__, obj.__name__)
             def createTest(name):
                 return GeneratorFunctionCase(name, **args)
-            tests.extend(testsFromGenerator(name, extras, createTest))
+            tests.extend(testsFromGenerator(name, extras, createTest,
+                                            self.unpack))
         else:
             case = FunctionTestCase(obj, **args)
             tests.append(case)
             return [tests[testIndex-1]]
         return tests
 
+    def isGenerator(self, obj):
+        return getattr(obj, 'testGenerator', None) is not None
+
 
 class GeneratorFunctionCase(FunctionTestCase):
     def __init__(self, name, **args):
 
     configSection = 'generators'
     commandLineSwitch = (None, 'generators', 'Load tests from generators')
+    unpack = enumerate
 
     def pluginsLoaded(self, event):
         Functions.generatorsEnabled = True
         testCaseClass = event.testCase
         for name in dir(testCaseClass):
             method = getattr(testCaseClass, name)
-            if getattr(method, 'testGenerator', None) is not None:
+            if self.isGenerator(method):
                 instance = testCaseClass(name)
                 event.extraTests.extend(
-                    testsFromGenerator(name, method(instance), testCaseClass)
+                    testsFromGenerator(name, method(instance), testCaseClass,
+                                       self.unpack)
                 )
 
+    def isGenerator(self, obj):
+        return getattr(obj, 'testGenerator', None) is not None
+        
     def getTestCaseNames(self, event):
         names = filter(event.isTestMethod, dir(event.testCase))
         klass = event.testCase
         for name in names:
             method = getattr(klass, name)
-            if getattr(method, 'testGenerator', None) is not None:
+            if self.isGenerator(method):
                 event.excludedNames.append(name)
 
     def loadTestsFromName(self, event):
         parent, obj, name, index = result
         if (index is None or not isinstance(parent, type) or 
             not issubclass(parent, TestCase) or 
-            not getattr(obj, 'testGenerator', False)):
+            not self.isGenerator(obj)):
             # we're only handling TestCase generator methods here
             return
 
         instance = parent(obj.__name__)
         
         try:
-            test = list(testsFromGenerator(name, obj(instance), parent))[index-1]
+            test = list(testsFromGenerator(name, obj(instance), parent, 
+                                           self.unpack))[index-1]
         except IndexError:
             raise TestNotFoundError(original_name)
         
         return suite
 
 
-def testsFromGenerator(name, generator, testCaseClass):
+def testsFromGenerator(name, generator, testCaseClass, unpack):
     try:
-        for index, (func, args) in enumerate(generator):
+        for index, (func, args) in unpack(generator):
             method_name = name_from_args(name, index, args)
             setattr(testCaseClass, method_name, None)
             instance = testCaseClass(method_name)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.