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 changelog/14381.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed ``-V`` (short form of ``--version``) to properly display the current version.
7 changes: 5 additions & 2 deletions src/_pytest/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,12 @@ def main(

:returns: An exit code.
"""
# Handle a single `--version` argument early to avoid starting up the entire pytest infrastructure.
# Handle a single `--version`/`-V` argument early to avoid starting up the entire pytest infrastructure.
new_args = sys.argv[1:] if args is None else args
if isinstance(new_args, Sequence) and new_args.count("--version") == 1:
if (
isinstance(new_args, Sequence)
and (new_args.count("--version") + new_args.count("-V")) == 1
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't that missbehave if someone passes both argument variants at once

):
sys.stdout.write(f"pytest {__version__}\n")
return ExitCode.OK

Expand Down
7 changes: 4 additions & 3 deletions testing/test_helpconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ def test_version_verbose(pytester: Pytester, pytestconfig, monkeypatch) -> None:
result.stdout.fnmatch_lines(["*registered third-party plugins:", "*at*"])


def test_version_less_verbose(pytester: Pytester) -> None:
"""Single ``--version`` parameter should display only the pytest version, without loading plugins (#13574)."""
@pytest.mark.parametrize("flag", ["--version", "-V"])
def test_version_less_verbose(pytester: Pytester, flag: str) -> None:
"""Single ``--version`` or ``-V`` should display only the pytest version, without loading plugins (#13574)."""
pytester.makeconftest("print('This should not be printed')")
result = pytester.runpytest_subprocess("--version")
result = pytester.runpytest_subprocess(flag)
assert result.ret == ExitCode.OK
assert result.stdout.str().strip() == f"pytest {pytest.__version__}"

Expand Down
Loading