使用鼻子测试时,变量是否可以从cmd移动到我的模块?
场景:我正在运行需要针对网站的生产和沙箱版本运行的selenium测试(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
的print语句
Whiles env
在setEnvironment.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 :(得分: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),用直接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)
当我跑步时,我得到了:
$ 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