Anonymous avatar Anonymous committed 2f761b6

More nose backwards compatibility with generator tests and test function fixtures

Comments (0)

Files changed (5)

 - --failed support in testid plugin
 
 - support yield x, y, z syntax from generators in addition to
-  yield x (y, z)
+  yield x, (y, z)

nose2/plugins/loader.py

+import inspect
+from unittest2.plugins import moduleloading as ml
+
+
+def unpack(_self, generator):
+    for index, func_args in enumerate(generator):
+        try:
+            func, args = func_args
+            if not isinstance(args, tuple):
+                args = (args,)
+            yield index, (func, args)
+        except ValueError:
+            func, args = func_args[0], func_args[1:]
+            yield index, (func, args)
+
+
+class Functions(ml.Functions):
+    unpack = unpack
+
+    def createTests(self, obj, testIndex=None):
+        if not hasattr(obj, 'setUp'):
+            if hasattr(obj, 'setup'):
+                obj.setUp = obj.setup
+            elif hasattr(obj, 'setUpFunc'):
+                obj.setUp = obj.setUpFunc
+        if not hasattr(obj, 'tearDown'):
+            if hasattr(obj, 'teardown'):
+                obj.tearDown = obj.teardowns
+            elif hasattr(obj, 'tearDownFunc'):
+                obj.tearDown = obj.tearDownFunc
+        return super(Functions, self).createTests(obj, testIndex)
+
+    def isGenerator(self, obj):
+        return (inspect.isgeneratorfunction(obj) or
+                super(Functions, self).isGenerator(obj))
+
+
+class Generators(ml.Generators):
+    unpack = unpack
+
+    def isGenerator(self, obj):
+        return (inspect.isgeneratorfunction(obj) or
+                super(Generators, self).isGenerator(obj))
+
+
+

nose2/plugins/loader_compat.py

-import inspect
-
-from unittest2 import Plugin
-from unittest2.events import hooks
-from unittest2.util import getObjectFromName
-
-
-class LoaderCompat(Plugin):
-    configSection = 'compat'
-
-    def pluginsLoaded(self, event):
-        # ensure that my hook calls come first
-        hook = hooks.loadTestsFromModule
-        hook -= self.loadTestsFromModule
-        hook += self.loadTestsFromModule
-
-    #def loadTestsFromName(self, event):
-    #    parent, obj = getObjectFromName(event.name, event.module)
-    #    self._update(obj)
-
-    def loadTestsFromModule(self, event):
-        module = event.module
-        # shim to make nose-style implicit generators
-        # and test function fixtures work with moduleloading plugin
-        for attr in dir(module):
-            item = getattr(module, attr, None)
-            self._update(item)
-
-    def _update(self, obj):
-        if inspect.isfunction(obj):
-            if hasattr(obj, 'setup'):
-                obj.setUp = obj.setup
-            elif hasattr(obj, 'setUpFunc'):
-                obj.setUp = obj.setUpFunc
-            if hasattr(obj, 'teardown'):
-                obj.tearDown = obj.teardown
-            elif hasattr(obj, 'tearDownFunc'):
-                obj.tearDown = obj.tearDownFunc
-            if inspect.isgeneratorfunction(obj):
-                obj.testGenerator = True

nose2/plugins/plugins.cfg

         nose2.plugins.outcomes
         nose2.plugins.prof
         nose2.plugins.testid
-        nose2.plugins.loader_compat
+        nose2.plugins.loader
+
+excluded-plugins =
         unittest2.plugins.moduleloading
 
 [functions]

support/layout1/pkg1/test/test_things.py

         yield check, (i, i,)
 test_gen.testGenerator = True
 
+
+def test_gen_nose_style():
+    def check(a, b):
+        assert a == b
+    for i in range(0, 5):
+        yield check, i, i
+
+
 did_setup = False
 def setup():
     global did_setup
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.