Skip to content Skip to sidebar Skip to footer

Nose, Unittest.testcase And Metaclass: Auto-generated Test_* Methods Not Discovered

This is a follow-up question for unittest and metaclass: automatic test_* method generation: For this (fixed) unittest.TestCase layout: #!/usr/bin/env python import unittest cla

Solution 1:

So, after sleuthing through both stdlib's unittest and nose's loader and selector source code, it turns out that nose overrides unittest.TestLoader.getTestCaseNames to use its own selector (with plugin points).

Now, nose's selector looks for a potential method's method.__name__ to match certain regexes, black and white lists, and plugins' decisions.

In my case, the dynamically generated functions have its testable.__name__ == '<lambda>', matching none of nose's selector criteria.

To fix,

        # inject methods: test{testname}_v4,6(self)
        for suffix, argin (('_false', False), ('_true', True)):
            testable_name = 'test{0}{1}'.format(testname, suffix)
            testable = lambda self, arg=arg: meth(self, arg)
            testable.__name__ = testable_name    # XXX: the fix
            attrs[testable_name] = testable

And sure enough:

(sandbox-2.7)bash-3.2$ nosetests -vv 
test_normal (test_testgen.TestCase) ... ok
test_that_false (test_testgen.TestCase) ... ok
test_that_true (test_testgen.TestCase) ... ok
test_this_false (test_testgen.TestCase) ... ok
test_this_true (test_testgen.TestCase) ... ok

----------------------------------------------------------------------
Ran 5 tests in 0.005s

OK

Post a Comment for "Nose, Unittest.testcase And Metaclass: Auto-generated Test_* Methods Not Discovered"