2

我想从鼻子配置文件中获得一些选项。但是我不想自己解析文件,所以我尝试使用nose api

我不确定如何解释这边提到的信息:

import nose

def setup()
  noseParser = nose.config.Config().getParser()
  value = noseParser.get_option("debug-log")

这就是我认为它应该工作的方式。但value一直存在None,没有例外。

我的用例:每次鼻子运行时删除调试日志文件。

4

3 回答 3

1

根据您提供的链接 getParser() 返回“命令行选项解析器”。我不确定,但您可以检查nose.config.Config().debugLog设置的内容。

于 2011-12-13T20:14:27.020 回答
0

我认为您最好的方法是编写一个自定义插件。这样,您就可以让鼻子为您完成工作。听起来您想要做的是debug-log在所有测试运行后删除。为此,您需要一个实现 finalize() 方法的插件。在此示例中,我还实现了 options() 以便可以启用/禁用插件和 configure(),以查找调试日志的位置。 在此处查看完整的方法列表

from nose.plugins import Plugin
import os

class AutoDeleteDebugLog(Plugin):
    """
    Automatically deletes the error log after test execution.  This may not be
    wise, so think carefully.
    """
    def options(self, parser, env):
        "Register commandline options using Nose plugin API."
        parser.add_option('--with-autodeletedebuglog', action='store_true',
                          help='Delete the debug log file after execution.')
        self.debuglog = None

    def configure(self, options, conf):
        "Register commandline options using Nose plugin API."
        self.enabled = options.autodeletedebuglog
        self.debuglog = options.debugLog

    def finalize(self, result):
        "Delete debug log file after all results are printed."
        if os.path.isfile(self.debuglog):
            os.remove(self.debuglog)

一旦你写了你的插件,你必须用鼻子注册它,并在执行时启用它。这里有相关说明。您可能还想使用该score属性来确保您的插件最后运行。

于 2013-02-05T07:20:19.200 回答
0

查看鼻子代码,我没有看到从配置文件中获取选项的清晰 API。我看到的是:

  • nose.config.all_config_files()您可以从和获取配置文件nose.config.user_config_files()
  • 鼻子它没有使用任何自定义解析类,而只是ConfigParser.RawConfigParser.

因此,毕竟直接解析配置文件也许不是一个坏主意。

于 2011-12-14T10:02:28.780 回答