Skip to content

Commit b10f289

Browse files
authored
Add the junit_log_passing_tests ini flag to skip logging output for passing tests. (#5052)
Add the junit_log_passing_tests ini flag to skip logging output for passing tests.
2 parents b0f0908 + da3f836 commit b10f289

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

changelog/4559.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added the ``junit_log_passing_tests`` ini value which can be used to enable and disable logging passing test output in the Junit XML file.

src/_pytest/junitxml.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,9 @@ def _add_simple(self, kind, message, data=None):
167167
self.append(node)
168168

169169
def write_captured_output(self, report):
170+
if not self.xml.log_passing_tests and report.passed:
171+
return
172+
170173
content_out = report.capstdout
171174
content_log = report.caplog
172175
content_err = report.capstderr
@@ -414,6 +417,12 @@ def pytest_addoption(parser):
414417
"one of no|system-out|system-err",
415418
default="no",
416419
) # choices=['no', 'stdout', 'stderr'])
420+
parser.addini(
421+
"junit_log_passing_tests",
422+
"Capture log information for passing tests to JUnit report: ",
423+
type="bool",
424+
default=True,
425+
)
417426
parser.addini(
418427
"junit_duration_report",
419428
"Duration time to report: one of total|call",
@@ -437,6 +446,7 @@ def pytest_configure(config):
437446
config.getini("junit_logging"),
438447
config.getini("junit_duration_report"),
439448
config.getini("junit_family"),
449+
config.getini("junit_log_passing_tests"),
440450
)
441451
config.pluginmanager.register(config._xml)
442452

@@ -472,12 +482,14 @@ def __init__(
472482
logging="no",
473483
report_duration="total",
474484
family="xunit1",
485+
log_passing_tests=True,
475486
):
476487
logfile = os.path.expanduser(os.path.expandvars(logfile))
477488
self.logfile = os.path.normpath(os.path.abspath(logfile))
478489
self.prefix = prefix
479490
self.suite_name = suite_name
480491
self.logging = logging
492+
self.log_passing_tests = log_passing_tests
481493
self.report_duration = report_duration
482494
self.family = family
483495
self.stats = dict.fromkeys(["error", "passed", "failure", "skipped"], 0)

testing/test_junitxml.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,3 +1332,30 @@ def test_skip():
13321332
snode = node.find_first_by_tag("skipped")
13331333
assert "1 <> 2" in snode.text
13341334
snode.assert_attr(message="1 <> 2")
1335+
1336+
1337+
def test_logging_passing_tests_disabled_does_not_log_test_output(testdir):
1338+
testdir.makeini(
1339+
"""
1340+
[pytest]
1341+
junit_log_passing_tests=False
1342+
junit_logging=system-out
1343+
"""
1344+
)
1345+
testdir.makepyfile(
1346+
"""
1347+
import pytest
1348+
import logging
1349+
import sys
1350+
1351+
def test_func():
1352+
sys.stdout.write('This is stdout')
1353+
sys.stderr.write('This is stderr')
1354+
logging.warning('hello')
1355+
"""
1356+
)
1357+
result, dom = runandparse(testdir)
1358+
assert result.ret == 0
1359+
node = dom.find_first_by_tag("testcase")
1360+
assert len(node.find_by_tag("system-err")) == 0
1361+
assert len(node.find_by_tag("system-out")) == 0

0 commit comments

Comments
 (0)