之前有一篇文章介绍了自己写的插件 nose进度插件,但最近有朋友问我,看着nose的官方文档写的插件没用,下面再详细介绍一下
一、准备
1、新建一个文件夹,随便文件夹的名字,假设文件夹放在f://aa这里
2、安装easy_install
二、开始
1、进入刚刚新建的文件夹f:/aa
2、在该文件夹下新建一个文件,名字叫myplugin.py,内容如下:
import os from nose.plugins import Plugin class MyCustomPlugin(Plugin): name = 'huplugin' def options(self, parser, env=os.environ):
Plugin.options(self, parser, env) 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): pass # do some stuff based on the given path def begin(self): print 'Maybe print some useful stuff...'
3、再新建一个文件,文件名叫setup.py,内容如下:
import sys try: import ez_setup ez_setup.use_setuptools() except ImportError: pass from setuptools import setup setup( name='mypackage', version='0.1', entry_points={ 'nose.plugins.0.1.0': [ 'myplugin = myplugin:MyCustomPlugin' ] } )
三、在本地安装插件
这里要注意了,如果直接使用python setup.py install的话,可以安装成功,但nosetests -h没有自己写的插件,要用如下方式
1、使用cmd,进入f:a 2、输入easy_install . 注意,上步最后有一个 .
四、验证
1、方式一:
nosetests -p 查看,会看到下面这一行 Plugin huplugin score: 100 (no help available) 说明安装成功
2、方式二:
nosetests -h查看 --with-huplugin Enable plugin MyCustomPlugin: (no help available) [NOSE_WITH_HUPLUGIN] --custom-path=CUSTOM_PATH Specify path to widget config file 有上面这二个,说明安装成功