Using nose plugin to pass a boolean to my package

こ雲淡風輕ζ 提交于 2019-12-02 06:06:57

It looks like the way you are doing it the value of the variable is assigned on import. Try something like this:

#at the top of the setup() module
import setEnvironment
...

#in setup() directly
print "env =", setEnvironment.env

You also have some minor typos in your code. The following should work (setEnvironment.py):

from nose.plugins.base import Plugin

env = None

class EnvironmentSelector(Plugin):
    """Selects if test will be run against production or sandbox environments.
    """

    def __init__(self):
        Plugin.__init__(self)
        self.environment = "spam"  ## runs against sandbox by default

    def options(self, parser, env):
        """Register command line options"""
        parser.add_option("--set-env",
                          action="store",
                          dest="setEnv",
                          metavar="ENVIRON",
                          help="Run tests against production or sandbox"
                          "If no --set-env specified, runs against sandbox by default")

    def configure(self, options, config):
        """Configure the system, based on selected options."""

        #set variable to that which was passed in cmd
        self.environment = options.setEnv
        self.enabled = self.environment

        if self.enabled:
            global env
            print "This is env before: " + str(env)
            env = self.passEnv()
            print "This is env after: " + str(env)
            return env

    def passEnv(self):
        run_production = False

        if self.environment.lower() == "sandbox":
            print ("Environment set to sandbox")
            return run_production

        elif self.environment.lower() == "prod":
            print ("Environmnet set to prod")
            run_production = True
            return run_production

        else:
            print ("NO environment was set, running sandbox by default")
            return run_production

And here is my testing code (pg_test.py), to run with straight python:

import logging
import sys

import nose

from nose.tools import with_setup

import setEnvironment

def custom_setup():
    #in setup() directly
    print "env =", setEnvironment.env


@with_setup(custom_setup)
def test_pg():
    pass

if __name__ == '__main__':   
    module_name = sys.modules[__name__].__file__

    logging.debug("running nose for package: %s", module_name)
    result = nose.run(argv=[sys.argv[0],
                            module_name,
                            '-s',
                            '--nologcapture',
                            '--set-env=prod'
                            ],
                      addplugins=[setEnvironment.EnvironmentSelector()],)
    logging.info("all tests ok: %s", result)

And when I ran it, I got:

$ python pg_test.py
This is env before: None
Environmnet set to prod
This is env after: True
env = True
.
----------------------------------------------------------------------
Ran 1 test in 0.001s

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