How to access plugin options within a test? (Python Nose)

与世无争的帅哥 提交于 2019-12-01 00:24:45
Oleksiy

One shortcut is to access import sys; sys.argv within the test - it will have the list of parameters passed to the nose executable, including the plugin ones. Alternatively your plugin can add attributes to your tests, and you can refer to those attributes - but it requires more heavy lifting - similar to this answer.

So I've found out how to make this work:

import os
from nose.plugins import Plugin

case_options = None

class test_args(Plugin):
    """
    Attempting to add command line parameters.
    """
    name = 'test_args'
    enabled = True

    def options(self, parser, env=os.environ):
        super(test_args, self).options(parser, env)
        parser.add_option("--hostname",
                    action="store",
                    type="str",
                    help="The hostname of the server")

    def configure(self, options, conf):
        global case_options 
        case_options = options

Using this you can do this in your test case to get the options:

    from test_args import case_options

To solve the different config file issues, I've found you can use a setup.cfg file written like an INI file to pass in default command line parameters. You can also pass in a -c config_file.cfg to pick a different config. This should work nicely for what we need.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!