是否可以在noses setup.cfg中设置环境变量

时间:2013-07-10 15:54:27

标签: python nose nosetests

我正在使用一个相当庞大的嵌入式python项目。目前,测试隐藏在make调用之后,该调用在工作站上设置PYTHONPATH和LD_LIBRARY_PATH,因此测试可以完成。是否可以在nose配置中指定它,以便用户只需要在目录中调用 nosetests

否则我应该在测试文件中加入一些样板来操纵所需的路径吗?

1 个答案:

答案 0 :(得分:1)

没有鼻子没有任何当前能力从配置文件设置环境变量:

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

从配置文件指定的任何配置选项将直接存储为列表中的元组。事实上,如果你尝试传递一些鼻子不接受的值,那么就会抛出一个错误。

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允许您在某种类型的文件中指定测试配置选项,如果您的值无法识别,则不会抛出错误。

或者您可以在测试中添加一些 @setup @teardown 方法。我会非常谨慎地添加任何类型的setupTests.sh脚本,因为这只会增加额外的复杂性来运行测试。

相关问题