我正在处理一个相当庞大的嵌入式 Python 项目。目前,测试隐藏在 make 调用后面,该调用在工作站上设置 PYTHONPATH 和 LD_LIBRARY_PATH,以便测试可以完成。是否可以在 nose 配置中指定它,以便用户只需要在目录中调用 nosetests?
否则我应该只在测试文件中包含一些样板来操作所需的路径吗?
最佳答案
No nose 当前没有任何能力从配置文件设置环境变量:
def _configTuples(self, cfg, filename):
config = []
if self._config_section in cfg.sections():
for name, value in cfg.items(self._config_section):
config.append((name, value, filename))
return config
def _readFromFilenames(self, filenames):
config = []
for filename in filenames:
cfg = ConfigParser.RawConfigParser()
try:
cfg.read(filename)
except ConfigParser.Error, exc:
raise ConfigError("Error reading config file %r: %s" %
(filename, str(exc)))
config.extend(self._configTuples(cfg, filename))
return config
从配置文件中指定的任何配置选项都将作为元组直接存储在列表中。事实上,如果您尝试传递一些 nose 不接受的值,那么它会抛出错误。
def _applyConfigurationToValues(self, parser, config, values):
for name, value, filename in config:
if name in option_blacklist:
continue
try:
self._processConfigValue(name, value, values, parser)
except NoSuchOptionError, exc:
self._file_error(
"Error reading config file %r: "
"no such option %r" % (filename, exc.name),
name=name, filename=filename)
except optparse.OptionValueError, exc:
msg = str(exc).replace('--' + name, repr(name), 1)
self._file_error("Error reading config file %r: "
"%s" % (filename, msg),
name=name, filename=filename)
查看出现 NoSuchOptionError 的部分。
您确实有一个选项,我已经稍微涉足了;使用 nose-testconfig它允许您在某种文件中指定测试配置选项,如果您的值无法识别,nose 不会抛出错误。
或者您可以将一些@setup 或@teardown 方法放入您的测试中。我会非常谨慎地添加任何类型的 setupTests.sh 脚本,因为这只会增加运行测试的额外复杂性。
关于python - 是否可以在 noses setup.cfg 中设置环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17575367/