简体   繁体   中英

Error using a YAML config file with Python nosetests

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

My YAML File looks like this

general:
  user : admin
  passwd : admin

I run the test like this

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

I get the following error:

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'

I am confused because on Python CLI i can read the file without any issues.. ?!

The docs suggest that nose-testconfig assumes a INI file on default. You'll need to explicitly tell nose that it's a YAML file:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM