Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ Dave Hunt
David Díaz-Barquero
David Mohr
David Paul Röthlisberger
David Peled
David Szotten
David Vierra
Daw-Ran Liou
Expand Down
1 change: 1 addition & 0 deletions changelog/10558.doc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix ambiguous docstring of `pytest.Config.getoption`.
7 changes: 4 additions & 3 deletions src/_pytest/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1699,9 +1699,10 @@ def getoption(self, name: str, default=notset, skip: bool = False):

:param name: Name of the option. You may also specify
the literal ``--OPT`` option instead of the "dest" option name.
:param default: Default value if no option of that name exists.
:param skip: If True, raise pytest.skip if option does not exists
or has a None value.
:param default: Fallback value if no option of that name is declared.
Note this parameter will be ignored when the option is declared even if the option's value is None.
:param skip: If True, raise pytest.skip if option is undeclared or has a None value.
Note that even if True, if a default was specified it will be returned instead of a skip.
"""
name = self._opt2dest.get(name, name)
try:
Expand Down
20 changes: 13 additions & 7 deletions testing/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ def test_config_trace(self, pytester: Pytester) -> None:
assert len(values) == 1
assert values[0] == "hello [config]\n"

def test_config_getoption(self, pytester: Pytester) -> None:
def test_config_getoption_declared_option_name(self, pytester: Pytester) -> None:
pytester.makeconftest(
"""
def pytest_addoption(parser):
Expand All @@ -648,6 +648,18 @@ def pytest_addoption(parser):
assert config.getoption(x) == "this"
pytest.raises(ValueError, config.getoption, "qweqwe")

config_novalue = pytester.parseconfig()
assert config_novalue.getoption("hello") is None
assert config_novalue.getoption("hello", default=1) is None
assert config_novalue.getoption("hello", default=1, skip=True) == 1

def test_config_getoption_undeclared_option_name(self, pytester: Pytester) -> None:
config = pytester.parseconfig()
with pytest.raises(ValueError):
config.getoption("x")
assert config.getoption("x", default=1) == 1
assert config.getoption("x", default=1, skip=True) == 1

def test_config_getoption_unicode(self, pytester: Pytester) -> None:
pytester.makeconftest(
"""
Expand Down Expand Up @@ -675,12 +687,6 @@ def pytest_addoption(parser):
with pytest.raises(pytest.skip.Exception):
config.getvalueorskip("hello")

def test_getoption(self, pytester: Pytester) -> None:
config = pytester.parseconfig()
with pytest.raises(ValueError):
config.getvalue("x")
assert config.getoption("x", 1) == 1

def test_getconftest_pathlist(self, pytester: Pytester, tmp_path: Path) -> None:
somepath = tmp_path.joinpath("x", "y", "z")
p = tmp_path.joinpath("conftest.py")
Expand Down
Loading