Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

We are trying to implement an automated testing framework using nose. The intent is to add a few command line options to pass into the tests, for example a hostname. We run these tests against a web app as integration tests.

So, we've created a simple plugin that adds a option to the parser:

import os
from nose.plugins import Plugin

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):
        self.hostname = options.hostname    

The option is available now when we run nosetests...but I can't figure out how to use it within a test case? Is this possible? I can't find any documentation on how to access options or the configuration within a test case.

The adding of the command line arguments is purely for development/debugging code purposes. We plan to use config files for our automated runs in bamboo. However, when developing integration tests and also debugging issues, it is nice to change the config on the fly. But we need to figure out how to actually use the options first...I feel like I'm just missing something basic, or I'm blind...

Ideally we could extend the testconfig plugin to make passing in config arguments from this:

--tc=key:value

to:

--key=value

If there is a better way to do this then I'm all ears.

like image 865
rjbez Avatar asked Aug 26 '26 23:08

rjbez


1 Answers

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.

like image 188
Oleksiy Avatar answered Aug 28 '26 14:08

Oleksiy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!