我想从nose配置文件中获得一些选项。但是我不想自己解析这个文件,所以我尝试使用nose api
我不知道如何解释这里提到的信息:
import nose
def setup()
noseParser = nose.config.Config().getParser()
value = noseParser.get_option("debug-log")这就是我认为它应该工作的方式。但是value一直是None,并且没有引发异常。
我的用例:每次运行log时删除debug-log文件。
发布于 2011-12-14 04:14:27
根据您提供的链接,getParser()返回“命令行选项解析器”。我不确定,但你可以检查一下nose.config.Config().debugLog的设置。
发布于 2011-12-14 18:02:29
看一下nose代码,我看不到一个清晰的API来从配置文件中获取选项。我看到的是:
你可以从nose.config.user_config_files().
nose.config.all_config_files()获取配置文件,它没有使用任何自定义的解析类,只使用了ConfigParser.RawConfigParser.因此,直接解析配置文件也许并不是一个坏主意。
发布于 2013-02-05 15:20:19
我认为你最好的方法是使用write a custom plugin。这样,您就可以让nose为您做这项工作。听起来您想要做的是在运行完所有测试之后删除debug-log。为此,您需要一个实现finalize()方法的插件。在本例中,我还实现了选项(),以便可以启用/禁用插件并配置(),以查找debug-log的位置。Check out the full list of methods here。
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)一旦编写了插件,就必须将其注册到nose,并在执行时启用它。好了,are instructions for that here。您可能还想使用score属性来确保您的插件是最后运行的。
https://stackoverflow.com/questions/8495483
复制相似问题