Skip to content

Commit fb56f3d

Browse files
authored
Merge pull request #12889 from pytest-dev/patchback/backports/8.3.x/d8d607e937bf5a36815007322bf10239f3330475/pr-12886
2 parents 1eb6007 + 6fe6382 commit fb56f3d

File tree

4 files changed

+20
-11
lines changed

4 files changed

+20
-11
lines changed

Diff for: AUTHORS

+1
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ Dave Hunt
116116
David Díaz-Barquero
117117
David Mohr
118118
David Paul Röthlisberger
119+
David Peled
119120
David Szotten
120121
David Vierra
121122
Daw-Ran Liou

Diff for: 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`.

Diff for: src/_pytest/config/__init__.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -1682,11 +1682,12 @@ def _get_override_ini_value(self, name: str) -> str | None:
16821682
def getoption(self, name: str, default=notset, skip: bool = False):
16831683
"""Return command line option value.
16841684
1685-
:param name: Name of the option. You may also specify
1685+
:param name: Name of the option. You may also specify
16861686
the literal ``--OPT`` option instead of the "dest" option name.
1687-
:param default: Default value if no option of that name exists.
1688-
:param skip: If True, raise pytest.skip if option does not exists
1689-
or has a None value.
1687+
:param default: Fallback value if no option of that name is **declared** via :hook:`pytest_addoption`.
1688+
Note this parameter will be ignored when the option is **declared** even if the option's value is ``None``.
1689+
:param skip: If ``True``, raise :func:`pytest.skip` if option is undeclared or has a ``None`` value.
1690+
Note that even if ``True``, if a default was specified it will be returned instead of a skip.
16901691
"""
16911692
name = self._opt2dest.get(name, name)
16921693
try:

Diff for: 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)