[TIP] Nose bug? using inspect.ismethod too aggressive?

Fernando Perez fperez.net at gmail.com
Sat Sep 19 23:05:30 PDT 2009

Hi all,

I have implemented support for 'parametric tests' that are easier to
debug (using pdb pops you into the actual test function, not the list
of the already-consumed generator).  If I write them as normal
unittest.TestCase classes:

def check(i,j):
    assert i<j,"%s !< %s" % (i,j)

class Tester(ParametricTestCase):

    def test_parametric(self):
        yield check(3, 4)
        x, y = 1, 2
        yield check(x, y)

they are recognized and work fine both with normal unittest and with
nose.  But I made a little decorator to make it easier to write
standalone, nose-style functions in this manner:

def test_par_standalone():
    yield check(3, 4)
    x, y = 2, 2
    yield check(x, y)

Unfortunately, nose now doesn't recognize them anymore.  The reason is
that nose is more aggressive than unittest in what it considers a
valid test method.  Unittest uses this in TestLoader.getTestCaseNames:

        def isTestMethod(attrname, testCaseClass=testCaseClass,
            return attrname.startswith(prefix) and
hasattr(getattr(testCaseClass, attrname), '__call__')

the key is that it just checks that it's a callable with the right
name prefix.  Nose, instead, uses (in loader.py,
TestLoader.getTestCaseNames that subclasses the one above):

      def wanted(attr, cls=testCaseClass, sel=self.selector):
            item = getattr(cls, attr, None)
            if not ismethod(item):
                return False
            return sel.wantMethod(item)

where ismethod comes from inspect.

But inspect.ismethod() returns false for staticmethods, while unittest
accepts them (and I need that for my use case).

It seems to me that this is a nose bug, fixable with:

-            if not ismethod(item):
+            if not hasattr(item,'__call__'):

I'll try to monkeypatch it in the meantime, but it would be great to
have this done upstream, if it's indeed seen as a bug.



More information about the testing-in-python mailing list