- restore compatibility to old getvalueorskip behaviour

- introduce a better NOTSET representation to improve docs
This commit is contained in:
holger krekel 2014-05-14 07:36:31 +02:00
parent b61ed2cf7e
commit d6281b4206
2 changed files with 20 additions and 3 deletions

View File

@ -560,7 +560,11 @@ class CmdOptions(object):
def __repr__(self):
return "<CmdOptions %r>" %(self.__dict__,)
notset = object()
class Notset:
def __repr__(self):
return "<NOTSET>"
notset = Notset()
FILE_OR_DIR = 'file_or_dir'
class Config(object):
""" access to configuration values, pluginmanager and plugin hooks. """
@ -798,11 +802,15 @@ class Config(object):
:arg name: name of the option. You may also specify
the literal ``--OPT`` option instead of the "dest" option name.
:arg default: default value if no option of that name exists.
:arg skip: if True raise pytest.skip if not option exists.
:arg skip: if True raise pytest.skip if option does not exists
or has a None value.
"""
name = self._opt2dest.get(name, name)
try:
return getattr(self.option, name)
val = getattr(self.option, name)
if val is None and skip:
raise AttributeError(name)
return val
except AttributeError:
if default is not notset:
return default

View File

@ -117,6 +117,15 @@ class TestConfigAPI:
verbose = config.getvalueorskip("verbose")
assert verbose == config.option.verbose
def test_config_getvalueorskip_None(self, testdir):
testdir.makeconftest("""
def pytest_addoption(parser):
parser.addoption("--hello")
""")
config = testdir.parseconfig()
pytest.raises(pytest.skip.Exception,
"config.getvalueorskip('hello')")
def test_getoption(self, testdir):
config = testdir.parseconfig()
with pytest.raises(ValueError):