diff --git a/AUTHORS b/AUTHORS index 27c0b3ac408..e5445ab58d0 100644 --- a/AUTHORS +++ b/AUTHORS @@ -23,6 +23,7 @@ Alexander King Alexei Kozlenok algojogacor Alice Purcell +Alim Kozak Allan Feldman Aly Sivji Amir Elkess diff --git a/changelog/9244.improvement.rst b/changelog/9244.improvement.rst new file mode 100644 index 00000000000..21dc93632fa --- /dev/null +++ b/changelog/9244.improvement.rst @@ -0,0 +1 @@ +The ``help`` text of ini options registered with :func:`parser.addini() ` now substitutes ``%(default)s`` with the option's default value, mirroring the behavior already available for command-line options added with ``addoption``. diff --git a/src/_pytest/config/argparsing.py b/src/_pytest/config/argparsing.py index f70e27614ef..48ffdacbd17 100644 --- a/src/_pytest/config/argparsing.py +++ b/src/_pytest/config/argparsing.py @@ -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: diff --git a/src/_pytest/helpconfig.py b/src/_pytest/helpconfig.py index fdba02b35f4..8c1def78a28 100644 --- a/src/_pytest/helpconfig.py +++ b/src/_pytest/helpconfig.py @@ -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) diff --git a/testing/test_helpconfig.py b/testing/test_helpconfig.py index 7c2cb49d87e..42e1821466d 100644 --- a/testing/test_helpconfig.py +++ b/testing/test_helpconfig.py @@ -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()