Skip to content

Commit

Permalink
add --override-ini option to overrides ini values
Browse files Browse the repository at this point in the history
Signed-off-by: Ted Xiao <xiao.xj@gmail.com>
  • Loading branch information
fengxx committed Jun 25, 2016
1 parent f2bb3df commit f5a90ab
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 8 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ Ryan Wooden
Samuele Pedroni
Stephan Obermann
Tareq Alayan
Ted Xiao
Simon Gomizelj
Stefano Taschini
Stefan Farmbauer
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@
`#1629`_. Thanks `@obestwalter`_ and `@davehunt`_ for the complete PR
(`#1633`_)

* New cli flag ``--override-ini`` or ``-o`` that overrides ini values from the
command line so that one can do e.g. "-o xfail_strict=True". This is useful for
specifying options for which there is no separate command-line flag, a complete
ini-options can be showed by py.test --help
Thanks `@blueyed`_ and `@fengxx`_ for the PR


**Changes**

* Fixtures marked with ``@pytest.fixture`` can now use ``yield`` statements exactly like
Expand Down
27 changes: 19 additions & 8 deletions _pytest/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -1003,14 +1003,16 @@ def _getini(self, name):
description, type, default = self._parser._inidict[name]
except KeyError:
raise ValueError("unknown configuration value: %r" %(name,))
try:
value = self.inicfg[name]
except KeyError:
if default is not None:
return default
if type is None:
return ''
return []
value = self._get_override_ini_value(name)
if value is None:
try:
value = self.inicfg[name]
except KeyError:
if default is not None:
return default
if type is None:
return ''
return []
if type == "pathlist":
dp = py.path.local(self.inicfg.config.path).dirpath()
l = []
Expand Down Expand Up @@ -1041,6 +1043,15 @@ def _getconftest_pathlist(self, name, path):
l.append(relroot)
return l

def _get_override_ini_value(self, name):
value = None
if self.getoption("override_ini", None):
for ini_config in self.option.override_ini:
(key, user_ini_value) = ini_config.split("=", 2)
if key == name:
return user_ini_value
return value

def getoption(self, name, default=notset, skip=False):
""" return command line option value.
Expand Down
6 changes: 6 additions & 0 deletions _pytest/helpconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ def pytest_addoption(parser):
group.addoption('--debug',
action="store_true", dest="debug", default=False,
help="store internal tracing debug information in 'pytestdebug.log'.")
# support for "--overwrite-ini ININAME=INIVALUE" to overrides ini values from the command line
# so that one can do e.g. "-o xfail_strict=True". This is useful for specifying options for which
# there is no separate command-line flag, you can find ini-options by py.test --help
group._addoption('-o', '--override-ini',
nargs='*', dest="override_ini",
help="overrides ini values which do not have a separate command-line flag")


@pytest.hookimpl(hookwrapper=True)
Expand Down
57 changes: 57 additions & 0 deletions testing/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -583,3 +583,60 @@ def test_with_specific_inifile(self, tmpdir):
inifile = tmpdir.ensure("pytest.ini")
rootdir, inifile, inicfg = determine_setup(inifile, [tmpdir])
assert rootdir == tmpdir

class TestOverrideIniArgs:
""" test --override-ini """
@pytest.mark.parametrize("name", "setup.cfg tox.ini pytest.ini".split())
def test_override_ini_names(self, testdir, name):
testdir.tmpdir.join(name).write(py.std.textwrap.dedent("""
[pytest]
custom = 1.0
"""))
testdir.makeconftest("""
def pytest_addoption(parser):
parser.addini("custom", "")
""")
testdir.makepyfile("""
import pytest
def test_pass():
ini_val = pytest.config.getini("custom")
print('\\ncustom_option:%s\\n' % ini_val)
""")

result = testdir.runpytest("--override-ini", "custom=2.0", "-s")
assert result.ret == 0
result.stdout.fnmatch_lines([
"custom_option:2.0"
])

result = testdir.runpytest("--override-ini", "custom=2.0",
"--override-ini=custom=3.0", "-s")
assert result.ret == 0
result.stdout.fnmatch_lines([
"custom_option:3.0"
])


def test_override_ini_pathlist(self, testdir):
testdir.makeconftest("""
def pytest_addoption(parser):
parser.addini("paths", "my new ini value", type="pathlist")
""")
testdir.makeini("""
[pytest]
paths=blah.py
""")
testdir.makepyfile("""
import pytest
import py.path
def test_pathlist():
config_paths = pytest.config.getini("paths")
print(config_paths)
for cpf in config_paths:
print('\\nuser_path:%s' % cpf.basename)
""")
result = testdir.runpytest("--override-ini", 'paths=foo/bar1.py foo/bar2.py', "-s")
result.stdout.fnmatch_lines([
"user_path:bar1.py",
"user_path:bar2.py"
])

0 comments on commit f5a90ab

Please sign in to comment.