Skip to content

Commit 3789bb5

Browse files
authored
junit_logging options (follow up to #6469) (#6473)
junit_logging options (follow up to #6469)
2 parents f2659f7 + 9298f7e commit 3789bb5

File tree

6 files changed

+175
-112
lines changed

6 files changed

+175
-112
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ Ilya Konstantinov
122122
Ionuț Turturică
123123
Iwan Briquemont
124124
Jaap Broekhuizen
125+
Jakub Mitoraj
125126
Jan Balster
126127
Janne Vanhala
127128
Jason R. Coombs

changelog/6469.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
New options have been added to the :confval:`junit_logging` option: ``log``, ``out-err``, and ``all``.

doc/en/reference.rst

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,9 +1164,17 @@ passed multiple times. The expected format is ``name=value``. For example::
11641164
.. confval:: junit_logging
11651165

11661166
.. versionadded:: 3.5
1167+
.. versionchanged:: 5.4
1168+
``log``, ``all``, ``out-err`` options added.
11671169

1168-
Configures if stdout/stderr should be written to the JUnit XML file. Valid values are
1169-
``system-out``, ``system-err``, and ``no`` (the default).
1170+
Configures if captured output should be written to the JUnit XML file. Valid values are:
1171+
1172+
* ``log``: write only ``logging`` captured output.
1173+
* ``system-out``: write captured ``stdout`` contents.
1174+
* ``system-err``: write captured ``stderr`` contents.
1175+
* ``out-err``: write both captured ``stdout`` and ``stderr`` contents.
1176+
* ``all``: write captured ``logging``, ``stdout`` and ``stderr`` contents.
1177+
* ``no`` (the default): no captured output is written.
11701178

11711179
.. code-block:: ini
11721180

src/_pytest/junitxml.py

Lines changed: 24 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -167,51 +167,28 @@ def write_captured_output(self, report):
167167
content_out = report.capstdout
168168
content_log = report.caplog
169169
content_err = report.capstderr
170-
171-
if content_log or content_out:
172-
if content_log and self.xml.logging == "system-out":
173-
if content_out:
174-
# syncing stdout and the log-output is not done yet. It's
175-
# probably not worth the effort. Therefore, first the captured
176-
# stdout is shown and then the captured logs.
177-
content = "\n".join(
178-
[
179-
" Captured Stdout ".center(80, "-"),
180-
content_out,
181-
"",
182-
" Captured Log ".center(80, "-"),
183-
content_log,
184-
]
185-
)
186-
else:
187-
content = content_log
188-
else:
189-
content = content_out
190-
191-
if content:
192-
tag = getattr(Junit, "system-out")
193-
self.append(tag(bin_xml_escape(content)))
194-
195-
if content_log or content_err:
196-
if content_log and self.xml.logging == "system-err":
197-
if content_err:
198-
content = "\n".join(
199-
[
200-
" Captured Stderr ".center(80, "-"),
201-
content_err,
202-
"",
203-
" Captured Log ".center(80, "-"),
204-
content_log,
205-
]
206-
)
207-
else:
208-
content = content_log
209-
else:
210-
content = content_err
211-
212-
if content:
213-
tag = getattr(Junit, "system-err")
214-
self.append(tag(bin_xml_escape(content)))
170+
if self.xml.logging == "no":
171+
return
172+
content_all = ""
173+
if self.xml.logging in ["log", "all"]:
174+
content_all = self._prepare_content(content_log, " Captured Log ")
175+
if self.xml.logging in ["system-out", "out-err", "all"]:
176+
content_all += self._prepare_content(content_out, " Captured Out ")
177+
self._write_content(report, content_all, "system-out")
178+
content_all = ""
179+
if self.xml.logging in ["system-err", "out-err", "all"]:
180+
content_all += self._prepare_content(content_err, " Captured Err ")
181+
self._write_content(report, content_all, "system-err")
182+
content_all = ""
183+
if content_all:
184+
self._write_content(report, content_all, "system-out")
185+
186+
def _prepare_content(self, content, header):
187+
return "\n".join([header.center(80, "-"), content, ""])
188+
189+
def _write_content(self, report, content, jheader):
190+
tag = getattr(Junit, jheader)
191+
self.append(tag(bin_xml_escape(content)))
215192

216193
def append_pass(self, report):
217194
self.add_stats("passed")
@@ -408,9 +385,9 @@ def pytest_addoption(parser):
408385
parser.addini(
409386
"junit_logging",
410387
"Write captured log messages to JUnit report: "
411-
"one of no|system-out|system-err",
388+
"one of no|log|system-out|system-err|out-err|all",
412389
default="no",
413-
) # choices=['no', 'stdout', 'stderr'])
390+
)
414391
parser.addini(
415392
"junit_log_passing_tests",
416393
"Capture log information for passing tests to JUnit report: ",

testing/acceptance_test.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,7 +1297,11 @@ def test_simple():
12971297
"""
12981298
)
12991299
result = testdir.runpytest_subprocess(
1300-
testpath, "--capture=tee-sys", "--junitxml=output.xml"
1300+
testpath,
1301+
"--capture=tee-sys",
1302+
"--junitxml=output.xml",
1303+
"-o",
1304+
"junit_logging=all",
13011305
)
13021306

13031307
# ensure stdout/stderr were 'live printed'
@@ -1307,6 +1311,5 @@ def test_simple():
13071311
# now ensure the output is in the junitxml
13081312
with open(os.path.join(testdir.tmpdir.strpath, "output.xml"), "r") as f:
13091313
fullXml = f.read()
1310-
1311-
assert "<system-out>@this is stdout@\n</system-out>" in fullXml
1312-
assert "<system-err>@this is stderr@\n</system-err>" in fullXml
1314+
assert "@this is stdout@\n" in fullXml
1315+
assert "@this is stderr@\n" in fullXml

0 commit comments

Comments
 (0)