Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a custom nose plugin to the `nosetests` command

So I'm very noob in dealing with nose plugins.

I've been searching a lot but docs regarding nose plugins seem scarce. I read and tried what's in the following links to try to write a simple nose plugin and run it with nosetests, without success:

  1. https://nose.readthedocs.org/en/latest/doc_tests/test_init_plugin/init_plugin.html
  2. https://nose.readthedocs.org/en/latest/plugins/writing.html

I don't want to write my own test-runner or run the tests from any other script (via run(argv=argv, suite=suite(), ...)), like they do in the first link.

I wrote a file myplugin.py with a class like this:

import os
from nose.plugins import Plugin

class MyCustomPlugin(Plugin):
    name = 'myplugin'

    def options(self, parser, env=os.environ):
        parser.add_option('--custom-path', action='store',
                          dest='custom_path', default=None,
                          help='Specify path to widget config file')

    def configure(self, options, conf):
        if options.custom_path:
            self.make_some_configs(options.custom_path)
            self.enabled = True

    def make_some_configs(self, path):
        # do some stuff based on the given path

    def begin(self):
        print 'Maybe print some useful stuff...'
        # do some more stuff

and added a setup.py like this:

try:
    from setuptools import setup, find_packages
except ImportError:
    import distribute_setup
    distribute_setup.use_setuptools()
    from setuptools import setup, find_packages

setup(
    name='mypackage',
    ...
    install_requires=['nose==1.3.0'],
    py_modules=['myplugin'],
    entry_points={
      'nose.plugins.1.3.0': [
        'myplugin = myplugin:MyCustomPlugin'
      ]
    }
)

Both files are in the same directory.

Every time I run nosetests --custom-path [path], I get:

nosetests: error: no such option: --custom-path

From the links mentioned above, I thought that's all that was required to register and enable a custom plugin. But it seems that, either I'm doing something really wrong, or nose's docs are outdated.

Can someone please point me the correct way to register and enable a plugin, that I can use with nosetests?

Thanks a lot!! :)

like image 321
Gerard Avatar asked Oct 02 '22 08:10

Gerard


1 Answers

You don't want the nose version in entry_points in setup.py. Just use nose.plugins.0.10 as the docs say. The dotted version in the entry point name is not so much a nose version as a plugin API version.

like image 115
Silas Ray Avatar answered Oct 13 '22 12:10

Silas Ray