在nose的setup.cfg中设置环境变量是否可行?

3
我正在处理一个比较庞大的嵌入式Python项目。目前,测试被隐藏在一个make调用后面,它设置了工作站上的PYTHONPATH和LD_LIBRARY_PATH,以便测试可以完成。是否可能在nose配置中指定这个,这样用户只需要在目录中调用就可以了?
否则,我应该在测试文件中包含一些样板代码来操作所需的路径吗?

继续阅读后,看起来我需要设置一个在我的测试之前运行的测试夹具。然而,鼻子文档似乎没有任何明确的例子。 - stsquad
创建一个 runtests.sh 文件,设置环境并调用 nosetests - Joran Beasley
@JoranBeasley:那正是我想要避免的。主要是为了方便起见,因为集成开发环境似乎只针对直接运行nosetests进行优化。 - stsquad
如果你要操纵PYTHONPATH,那听起来你的问题不仅是正确设置测试环境这么简单。 - Greg
1个回答

1

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脚本,因为这只会增加运行测试的复杂性。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接