Skip to content

[PR #12886/d8d607e9 backport][8.3.x] Improve pytest.Config.getoption docstring #12889

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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 @@ -116,6 +116,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 :func:`pytest.Config.getoption`.
9 changes: 5 additions & 4 deletions src/_pytest/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1682,11 +1682,12 @@ def _get_override_ini_value(self, name: str) -> str | None:
def getoption(self, name: str, default=notset, skip: bool = False):
"""Return command line option value.

:param name: Name of the option. You may also specify
: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** via :hook:`pytest_addoption`.
Note this parameter will be ignored when the option is **declared** even if the option's value is ``None``.
:param skip: If ``True``, raise :func:`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