2014-04-08 50 views
1

使用鼻子測試時,變量是否可以從cmd移動到我的模塊?使用鼻子插件將布爾值傳遞給我的包

情景:我正在與硒需要對網站(www.sandbox.myurl.com和www.myurl.com)

的生產和沙盒版本上運行測試,我寫了一個定製的鼻子插件這讓我設置爲對

編輯的代碼

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 = True 

    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 

運行的環境,我的包,我有一個@setup功能通過適當的URL到的webdriver在運行測試套件之前。

在模塊的頂部用它我setup(),我有

from setEnvironment import env 

我附帶的env在設置功能

田地env獲取setEnvironment設置的值print語句。 py作爲True,它被導入爲None,這是env的原始任務。

如何獲取變量以成功導入@setup

SETUP.PY

這是我跑,每次我做出調整到setEnvironment腳本。

from setuptools import setup 

setup(
    name='Custom nose plugins', 
    version='0.6.0', 
    description = 'setup Prod v. Sandbox environment', 
    py_modules = ['setEnvironment'], 
    entry_points = { 
     'nose.plugins': [ 
      'setEnvironment = setEnvironment:EnvironmentSelector' 
      ] 
     } 
    ) 
+0

當您直接導入插件時,錯誤的堆棧跟蹤將會很有幫助。 – Oleksiy

+0

我將env作爲全局變量添加,所以我不再收到錯誤信息,但仍然無法使用正確的值將變量導入安裝程序。請參閱編輯代碼 – dbJones

回答

0

它看起來像你這樣做的方式變量的值在導入時分配。 嘗試是這樣的:

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

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

你也有一些小的輸入錯誤代碼。以下應工作(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 

這裏是我的測試代碼(pg_test.py),與直蟒蛇運行:

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) 

當我跑了,我得到了:

$ 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 
+0

我試過這個,但它不起作用。在'setEnvironment'中,我將全局變量改爲'env = None'。在set.Environment.config()我添加了打印語句,以顯示全局變量正在我的鼻子插件中得到更新。但是在我的setup()中,即使我包含'from setEnvironment import env'' env'仍然是None,這是'env'的原始任務 – dbJones

+0

這就是爲什麼你應該按照我的建議去做,不要使用'從setEnv..'開始,並在實際需要時使用'import setEnvironment'和遲後的變量訪問'setEnvironment.env'。 – Oleksiy

+0

也許我不明白Python足以看出爲什麼這種類型的導入會有所作爲,但即使嘗試對該字母的建議,變量也無法正確傳遞。 – dbJones