Skip to content

Commit b37c720

Browse files
Add --no-subtest-reports CLI opt (#199)
Fixes #198. --------- Signed-off-by: Giampaolo Rodola <g.rodola@gmail.com> Co-authored-by: Bruno Oliveira <bruno@pytest.org>
1 parent 4106e3b commit b37c720

File tree

3 files changed

+80
-1
lines changed

3 files changed

+80
-1
lines changed

CHANGELOG.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
CHANGELOG
22
=========
33

4+
0.14.3 (UNRELEASED)
5+
-------------------
6+
7+
*XXXX-XX-XX*
8+
9+
* Added experimental ``--no-subtest-reports`` CLI option. This disables
10+
subtests output unless it's a failed subtest. (`#198`_)
11+
12+
.. _#198: https://github.com/pytest-dev/pytest-subtests/pull/198
13+
414
0.14.2
515
------
616

src/pytest_subtests/plugin.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ def pytest_addoption(parser: pytest.Parser) -> None:
4646
default=False,
4747
help="Disables subtest output 'dots' in non-verbose mode (EXPERIMENTAL)",
4848
)
49+
group.addoption(
50+
"--no-subtests-reports",
51+
action="store_true",
52+
dest="no_subtests_reports",
53+
default=False,
54+
help="Disables subtest output unless it's a failed subtest (EXPERIMENTAL)",
55+
)
4956

5057

5158
@attr.s
@@ -459,9 +466,12 @@ def pytest_report_teststatus(
459466

460467
outcome = report.outcome
461468
description = report.sub_test_description()
469+
no_output = ("", "", "")
462470

463471
if hasattr(report, "wasxfail"):
464-
if outcome == "skipped":
472+
if config.option.no_subtests_reports and outcome != "skipped":
473+
return no_output
474+
elif outcome == "skipped":
465475
category = "xfailed"
466476
short = "y" # x letter is used for regular xfail, y for subtest xfail
467477
status = "SUBXFAIL"
@@ -476,6 +486,9 @@ def pytest_report_teststatus(
476486
return None
477487
short = "" if config.option.no_subtests_shortletter else short
478488
return f"subtests {category}", short, f"{description} {status}"
489+
490+
if config.option.no_subtests_reports and outcome != "failed":
491+
return no_output
479492
elif report.passed:
480493
short = "" if config.option.no_subtests_shortletter else ","
481494
return f"subtests {outcome}", short, f"{description} SUBPASS"

tests/test_subtests.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,62 @@ def test_typing_exported(subtests: SubTests) -> None:
156156
expected_lines += ["* 1 passed *"]
157157
result.stdout.fnmatch_lines(expected_lines)
158158

159+
def test_no_subtests_reports(
160+
self, pytester: pytest.Pytester, mode: Literal["normal", "xdist"]
161+
) -> None:
162+
pytester.makepyfile(
163+
"""
164+
import pytest
165+
166+
def test_foo(subtests):
167+
for i in range(5):
168+
with subtests.test(msg="custom", i=i):
169+
pass
170+
"""
171+
)
172+
# Without `--no-subtests-reports`, subtests are reported normally.
173+
result = pytester.runpytest("-v")
174+
result.stdout.fnmatch_lines(
175+
[
176+
"*collected 1 item*",
177+
"test_no_subtests_reports.py::test_foo * (i=0) SUBPASS*",
178+
"*test_no_subtests_reports.py::test_foo PASSED*",
179+
"* 1 passed, 5 subtests passed in*",
180+
]
181+
)
182+
183+
# With `--no-subtests-reports`, passing subtests are no longer reported.
184+
result = pytester.runpytest("-v", "--no-subtests-reports")
185+
result.stdout.fnmatch_lines(
186+
[
187+
"*collected 1 item*",
188+
"*test_no_subtests_reports.py::test_foo PASSED*",
189+
"* 1 passed in*",
190+
]
191+
)
192+
result.stdout.no_fnmatch_line("*SUBPASS*")
193+
194+
# Rewrite the test file so the tests fail. Even with the flag, failed subtests are still reported.
195+
pytester.makepyfile(
196+
"""
197+
import pytest
198+
199+
def test_foo(subtests):
200+
for i in range(5):
201+
with subtests.test(msg="custom", i=i):
202+
assert False
203+
"""
204+
)
205+
result = pytester.runpytest("-v", "--no-subtests-reports")
206+
result.stdout.fnmatch_lines(
207+
[
208+
"*collected 1 item*",
209+
"test_no_subtests_reports.py::test_foo * (i=0) SUBFAIL*",
210+
"*test_no_subtests_reports.py::test_foo PASSED*",
211+
"* 5 failed, 1 passed in*",
212+
]
213+
)
214+
159215

160216
class TestSubTest:
161217
"""

0 commit comments

Comments
 (0)