Skip to content
Open
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 @@ -23,6 +23,7 @@ Alexander King
Alexei Kozlenok
algojogacor
Alice Purcell
Alim Kozak
Allan Feldman
Aly Sivji
Amir Elkess
Expand Down
1 change: 1 addition & 0 deletions changelog/9244.improvement.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The ``help`` text of ini options registered with :func:`parser.addini() <pytest.Parser.addini>` now substitutes ``%(default)s`` with the option's default value, mirroring the behavior already available for command-line options added with ``addoption``.
3 changes: 3 additions & 0 deletions src/_pytest/config/argparsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ def addini(

:param name:
Name of the configuration.
:param help:
Description of the option, shown in pytest's help.
``%(default)s`` in the text is replaced with the option's default value.
:param type:
Type of the configuration. Can be:

Expand Down
3 changes: 2 additions & 1 deletion src/_pytest/helpconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,10 @@ def showhelp(config: Config) -> None:
indent_len = 24 # based on argparse's max_help_position=24
indent = " " * indent_len
for name in config._parser._inidict:
help, type, _default = config._parser._inidict[name]
help, type, default = config._parser._inidict[name]
if help is None:
raise TypeError(f"help argument cannot be None for {name}")
help = help.replace("%(default)s", str(default))
spec = f"{name} ({type}):"
tw.write(f" {spec}")
spec_len = len(spec)
Expand Down
14 changes: 14 additions & 0 deletions testing/test_helpconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,20 @@ def pytest_addoption(parser):
result.stdout.fnmatch_lines(lines, consecutive=True)


def test_addini_default_substitution(pytester: Pytester) -> None:
"""``%(default)s`` in an ini help string is replaced by its default (#9244)."""
pytester.makeconftest(
"""
def pytest_addoption(parser):
parser.addini("test_ini", "Help text, defaults to %(default)s", default="hello")
"""
)
result = pytester.runpytest("--help")
assert result.ret == ExitCode.OK
result.stdout.fnmatch_lines(["*test_ini (string):*Help text, defaults to hello*"])
assert "%(default)s" not in result.stdout.str()


def test_parse_known_args_doesnt_quit_on_help(pytester: Pytester) -> None:
"""`parse_known_args` shouldn't exit on `--help`, unlike `parse`."""
config = pytester.parseconfig()
Expand Down
Loading