Skip to content

Add --no-subtest-reports CLI opt #199

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Aug 8, 2025
Merged
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
10 changes: 10 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
CHANGELOG
=========

0.14.3 (UNRELEASED)
-------------------

*XXXX-XX-XX*

* Added experimental ``--no-subtest-reports`` CLI option. This disables
subtests output unless it's a failed subtest. (`#198`_)

.. _#198: https://github.com/pytest-dev/pytest-subtests/pull/198

0.14.2
------

Expand Down
15 changes: 14 additions & 1 deletion src/pytest_subtests/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ def pytest_addoption(parser: pytest.Parser) -> None:
default=False,
help="Disables subtest output 'dots' in non-verbose mode (EXPERIMENTAL)",
)
group.addoption(
"--no-subtests-reports",
action="store_true",
dest="no_subtests_reports",
default=False,
help="Disables subtest output unless it's a failed subtest (EXPERIMENTAL)",
)


@attr.s
Expand Down Expand Up @@ -459,9 +466,12 @@ def pytest_report_teststatus(

outcome = report.outcome
description = report.sub_test_description()
no_output = ("", "", "")

if hasattr(report, "wasxfail"):
if outcome == "skipped":
if config.option.no_subtests_reports and outcome != "skipped":
return no_output
elif outcome == "skipped":
category = "xfailed"
short = "y" # x letter is used for regular xfail, y for subtest xfail
status = "SUBXFAIL"
Expand All @@ -476,6 +486,9 @@ def pytest_report_teststatus(
return None
short = "" if config.option.no_subtests_shortletter else short
return f"subtests {category}", short, f"{description} {status}"

if config.option.no_subtests_reports and outcome != "failed":
return no_output
elif report.passed:
short = "" if config.option.no_subtests_shortletter else ","
return f"subtests {outcome}", short, f"{description} SUBPASS"
Expand Down
56 changes: 56 additions & 0 deletions tests/test_subtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,62 @@ def test_typing_exported(subtests: SubTests) -> None:
expected_lines += ["* 1 passed *"]
result.stdout.fnmatch_lines(expected_lines)

def test_no_subtests_reports(
self, pytester: pytest.Pytester, mode: Literal["normal", "xdist"]
) -> None:
pytester.makepyfile(
"""
import pytest

def test_foo(subtests):
for i in range(5):
with subtests.test(msg="custom", i=i):
pass
"""
)
# Without `--no-subtests-reports`, subtests are reported normally.
result = pytester.runpytest("-v")
result.stdout.fnmatch_lines(
[
"*collected 1 item*",
"test_no_subtests_reports.py::test_foo * (i=0) SUBPASS*",
"*test_no_subtests_reports.py::test_foo PASSED*",
"* 1 passed, 5 subtests passed in*",
]
)

# With `--no-subtests-reports`, passing subtests are no longer reported.
result = pytester.runpytest("-v", "--no-subtests-reports")
result.stdout.fnmatch_lines(
[
"*collected 1 item*",
"*test_no_subtests_reports.py::test_foo PASSED*",
"* 1 passed in*",
]
)
result.stdout.no_fnmatch_line("*SUBPASS*")

# Rewrite the test file so the tests fail. Even with the flag, failed subtests are still reported.
pytester.makepyfile(
"""
import pytest

def test_foo(subtests):
for i in range(5):
with subtests.test(msg="custom", i=i):
assert False
"""
)
result = pytester.runpytest("-v", "--no-subtests-reports")
result.stdout.fnmatch_lines(
[
"*collected 1 item*",
"test_no_subtests_reports.py::test_foo * (i=0) SUBFAIL*",
"*test_no_subtests_reports.py::test_foo PASSED*",
"* 5 failed, 1 passed in*",
]
)


class TestSubTest:
"""
Expand Down
Loading