Skip to content

Commit d8d607e

Browse files
nicoddemussus-pe
andauthored
Improve pytest.Config.getoption docstring (#12886)
Closes #10558. --------- Co-authored-by: suspe <[email protected]>
1 parent a4e40bc commit d8d607e

File tree

4 files changed

+20
-11
lines changed

4 files changed

+20
-11
lines changed

AUTHORS

+1
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ Dave Hunt
118118
David Díaz-Barquero
119119
David Mohr
120120
David Paul Röthlisberger
121+
David Peled
121122
David Szotten
122123
David Vierra
123124
Daw-Ran Liou

changelog/10558.doc.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix ambiguous docstring of :func:`pytest.Config.getoption`.

src/_pytest/config/__init__.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -1697,11 +1697,12 @@ def _get_override_ini_value(self, name: str) -> str | None:
16971697
def getoption(self, name: str, default=notset, skip: bool = False):
16981698
"""Return command line option value.
16991699
1700-
:param name: Name of the option. You may also specify
1700+
:param name: Name of the option. You may also specify
17011701
the literal ``--OPT`` option instead of the "dest" option name.
1702-
:param default: Default value if no option of that name exists.
1703-
:param skip: If True, raise pytest.skip if option does not exists
1704-
or has a None value.
1702+
:param default: Fallback value if no option of that name is **declared** via :hook:`pytest_addoption`.
1703+
Note this parameter will be ignored when the option is **declared** even if the option's value is ``None``.
1704+
:param skip: If ``True``, raise :func:`pytest.skip` if option is undeclared or has a ``None`` value.
1705+
Note that even if ``True``, if a default was specified it will be returned instead of a skip.
17051706
"""
17061707
name = self._opt2dest.get(name, name)
17071708
try:

testing/test_config.py

+13-7
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@ def test_config_trace(self, pytester: Pytester) -> None:
636636
assert len(values) == 1
637637
assert values[0] == "hello [config]\n"
638638

639-
def test_config_getoption(self, pytester: Pytester) -> None:
639+
def test_config_getoption_declared_option_name(self, pytester: Pytester) -> None:
640640
pytester.makeconftest(
641641
"""
642642
def pytest_addoption(parser):
@@ -648,6 +648,18 @@ def pytest_addoption(parser):
648648
assert config.getoption(x) == "this"
649649
pytest.raises(ValueError, config.getoption, "qweqwe")
650650

651+
config_novalue = pytester.parseconfig()
652+
assert config_novalue.getoption("hello") is None
653+
assert config_novalue.getoption("hello", default=1) is None
654+
assert config_novalue.getoption("hello", default=1, skip=True) == 1
655+
656+
def test_config_getoption_undeclared_option_name(self, pytester: Pytester) -> None:
657+
config = pytester.parseconfig()
658+
with pytest.raises(ValueError):
659+
config.getoption("x")
660+
assert config.getoption("x", default=1) == 1
661+
assert config.getoption("x", default=1, skip=True) == 1
662+
651663
def test_config_getoption_unicode(self, pytester: Pytester) -> None:
652664
pytester.makeconftest(
653665
"""
@@ -675,12 +687,6 @@ def pytest_addoption(parser):
675687
with pytest.raises(pytest.skip.Exception):
676688
config.getvalueorskip("hello")
677689

678-
def test_getoption(self, pytester: Pytester) -> None:
679-
config = pytester.parseconfig()
680-
with pytest.raises(ValueError):
681-
config.getvalue("x")
682-
assert config.getoption("x", 1) == 1
683-
684690
def test_getconftest_pathlist(self, pytester: Pytester, tmp_path: Path) -> None:
685691
somepath = tmp_path.joinpath("x", "y", "z")
686692
p = tmp_path.joinpath("conftest.py")

0 commit comments

Comments
 (0)