Search code examples
pythonnosenosetests

How to make nose plugin startTest method to return info about the test


Ive created this demo plugin to see how plugins work: (only thing added is the startTest method)

import logging 
import os
from nose.plugins import Plugin
import nose

log = logging.getLogger('nose.plugins.helloworld')

class HelloWorld(Plugin):
    name = 'helloworld'

    def options(self, parser, env=os.environ):
        super(HelloWorld, self).options(parser, env=env)

    def configure(self, options, conf):
        super(HelloWorld, self).configure(options, conf)
        if not self.enabled:
            return

    def finalize(self, result):
        log.info('Hello pluginized world!')

    def startTest(self, test):
        print "startTest method was run"
        print test.__doc__

But when running a test with this plugin , instead of getting the __doc__ of the test, im getting the universal test case wrapper doc.

startTest method was run
The universal test case wrapper.

When a plugin sees a test, it will always see an instance of this
class. To access the actual test case that will be run, access the
test property of the nose.case.Test instance.

I cant figure out how to access test property of the nose.case.test Ive tried to use print nose.case.Test.__test__ but this returns a bool value.

Any ideas?


Solution

  • I would inject import pdb; pdb.set_trace() in your startTest() function and have a nice look at test instance in the debugger. I'm sure the information you are looking for is there, it just might be few levels down. Do not forget to add -s to your nosetests run to enable stdout interaction with your debugger.

    Edit: I just did just that with a basic function test. For this particular case, the doc was under test.test.test.__doc__. The test was defined as:

    def test_a():
        "mydoc"
        pass
    

    Note: do not forget to actually enable your plugin by setting self.enable=True.