python - 如何将自定义 nose 插件添加到 `nosetests` 命令

标签 python unit-testing testing nose nosetests

所以我在处理 nose 插件方面非常菜鸟。

我搜索了很多,但关于 nose 插件的文档似乎很少。 我阅读并尝试了以下链接中的内容来尝试编写一个简单的 Nose 插件 并使用 nosetests 运行它,但没有成功:

  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

我不想编写自己的测试运行程序或从任何其他脚本运行测试(通过 run(argv=argv, suite=suite(), ...)), 就像他们在第一个链接中所做的那样。

我写了一个文件myplugin.py,里面有这样一个类:

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

并像这样添加了一个 setup.py:

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'
      ]
    }
)

两个文件都在同一个目录中。

每次运行 nosetests --custom-path [path] 时,我都会得到:

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

根据上面提到的链接,我认为这就是注册和启用自定义插件所需的全部内容。 但似乎,要么我做错了什么,要么 nose 的文档已经过时了。

谁能告诉我注册和启用插件的正确方法,我可以使用 nosetests

非常感谢!! :)

最佳答案

您不需要 setup.pyentry_points 中的 nose 版本。正如文档所说,只需使用 nose.plugins.0.10 即可。入口点名称中带点的版本与其说是 nose 版本,不如说是插件 API 版本。

关于python - 如何将自定义 nose 插件添加到 `nosetests` 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20132273/

相关文章:

testing - 如何让 Test-Framework 报告 QuickCheck 失败的原因?

node.js - Node ExpressJS-events.js : 167 throw er Unhandled error event

Python 策略模式 : Dynamically import class files

unit-testing - 如何编写写入标准输入的 Go 测试?

python - Py.test 修补模块内对象

python-3.x - 修补 boto3.resource 和 resource.Table 方法进行单元测试

ASP.NET Web 应用程序测试经验

python - if 部分的变量范围

python - 如何动态创建 Luigi 任务

python - BeautifulSoup 和 Python 删除 HTML 标签