使用带有Python测试的YAML配置文件时出错

时间:2014-11-02 17:58:47

标签: python parsing yaml config nose

def __init__():
    with open("config.yaml", 'r') as yamlfile:
        cfg = yaml.load(yamlfile)
        self.obj = TestMe(cfg.get("general", "user"), cfg.get("general", "passwd")

我的YAML文件看起来像这样

general:
  user : admin
  passwd : admin

我像这样运行测试

"nosetests test.py --exe --with-xunit --tc-file config.yaml"

我收到以下错误:

Traceback (most recent call last):
  File "/usr/local/bin/nosetests", line 9, in <module>
    load_entry_point('nose==1.3.3', 'console_scripts', 'nosetests')()
  File "/usr/local/lib/python2.7/site-packages/nose/core.py", line 121, in __init__
    **extra_args)
  File "/usr/local/lib/python2.7/unittest/main.py", line 94, in __init__
    self.parseArgs(argv)
  File "/usr/local/lib/python2.7/site-packages/nose/core.py", line 145, in parseArgs
    self.config.configure(argv, doc=self.usage())
  File "/usr/local/lib/python2.7/site-packages/nose/config.py", line 346, in configure
    self.plugins.configure(options, self)
  File "/usr/local/lib/python2.7/site-packages/nose/plugins/manager.py", line 284, in configure
    cfg(options, config)
  File "/usr/local/lib/python2.7/site-packages/nose/plugins/manager.py", line 99, in __call__
    return self.call(*arg, **kw)
  File "/usr/local/lib/python2.7/site-packages/nose/plugins/manager.py", line 167, in simple
    result = meth(*arg, **kw)
  File "/usr/local/lib/python2.7/site-packages/testconfig.py", line 129, in configure
    options.testconfigencoding)
  File "/usr/local/lib/python2.7/site-packages/testconfig.py", line 40, in load_ini
    tmpconfig.readfp(f)
  File "/usr/local/lib/python2.7/ConfigParser.py", line 324, in readfp
    self._read(fp, filename)
  File "/usr/local/lib/python2.7/ConfigParser.py", line 512, in _read
    raise MissingSectionHeaderError(fpname, lineno, line)
ConfigParser.MissingSectionHeaderError: File contains no section headers.
file: test_config.yaml, line: 1
u'general:\n'

我很困惑,因为在Python CLI上我可以毫无问题地阅读文件..?!

1 个答案:

答案 0 :(得分:1)

docs建议nose-testconfig默认使用INI文件。您需要明确告诉鼻子它是一个YAML文件:

nosetests test.py --exe --with-xunit --tc-file config.yaml --tc-format yaml