使用鼻子測試時,變量是否可以從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'
]
}
)
當您直接導入插件時,錯誤的堆棧跟蹤將會很有幫助。 – Oleksiy
我將env作爲全局變量添加,所以我不再收到錯誤信息,但仍然無法使用正確的值將變量導入安裝程序。請參閱編輯代碼 – dbJones