Skip to content

Commit

Permalink
junit_logging options (follow up to #6469) (#6473)
Browse files Browse the repository at this point in the history
junit_logging options (follow up to #6469)
  • Loading branch information
nicoddemus committed Jan 16, 2020
2 parents f2659f7 + 9298f7e commit 3789bb5
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 112 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ Ilya Konstantinov
Ionuț Turturică
Iwan Briquemont
Jaap Broekhuizen
Jakub Mitoraj
Jan Balster
Janne Vanhala
Jason R. Coombs
Expand Down
1 change: 1 addition & 0 deletions changelog/6469.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
New options have been added to the :confval:`junit_logging` option: ``log``, ``out-err``, and ``all``.
12 changes: 10 additions & 2 deletions doc/en/reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1164,9 +1164,17 @@ passed multiple times. The expected format is ``name=value``. For example::
.. confval:: junit_logging

.. versionadded:: 3.5
.. versionchanged:: 5.4
``log``, ``all``, ``out-err`` options added.

Configures if stdout/stderr should be written to the JUnit XML file. Valid values are
``system-out``, ``system-err``, and ``no`` (the default).
Configures if captured output should be written to the JUnit XML file. Valid values are:

* ``log``: write only ``logging`` captured output.
* ``system-out``: write captured ``stdout`` contents.
* ``system-err``: write captured ``stderr`` contents.
* ``out-err``: write both captured ``stdout`` and ``stderr`` contents.
* ``all``: write captured ``logging``, ``stdout`` and ``stderr`` contents.
* ``no`` (the default): no captured output is written.

.. code-block:: ini
Expand Down
71 changes: 24 additions & 47 deletions src/_pytest/junitxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,51 +167,28 @@ def write_captured_output(self, report):
content_out = report.capstdout
content_log = report.caplog
content_err = report.capstderr

if content_log or content_out:
if content_log and self.xml.logging == "system-out":
if content_out:
# syncing stdout and the log-output is not done yet. It's
# probably not worth the effort. Therefore, first the captured
# stdout is shown and then the captured logs.
content = "\n".join(
[
" Captured Stdout ".center(80, "-"),
content_out,
"",
" Captured Log ".center(80, "-"),
content_log,
]
)
else:
content = content_log
else:
content = content_out

if content:
tag = getattr(Junit, "system-out")
self.append(tag(bin_xml_escape(content)))

if content_log or content_err:
if content_log and self.xml.logging == "system-err":
if content_err:
content = "\n".join(
[
" Captured Stderr ".center(80, "-"),
content_err,
"",
" Captured Log ".center(80, "-"),
content_log,
]
)
else:
content = content_log
else:
content = content_err

if content:
tag = getattr(Junit, "system-err")
self.append(tag(bin_xml_escape(content)))
if self.xml.logging == "no":
return
content_all = ""
if self.xml.logging in ["log", "all"]:
content_all = self._prepare_content(content_log, " Captured Log ")
if self.xml.logging in ["system-out", "out-err", "all"]:
content_all += self._prepare_content(content_out, " Captured Out ")
self._write_content(report, content_all, "system-out")
content_all = ""
if self.xml.logging in ["system-err", "out-err", "all"]:
content_all += self._prepare_content(content_err, " Captured Err ")
self._write_content(report, content_all, "system-err")
content_all = ""
if content_all:
self._write_content(report, content_all, "system-out")

def _prepare_content(self, content, header):
return "\n".join([header.center(80, "-"), content, ""])

def _write_content(self, report, content, jheader):
tag = getattr(Junit, jheader)
self.append(tag(bin_xml_escape(content)))

def append_pass(self, report):
self.add_stats("passed")
Expand Down Expand Up @@ -408,9 +385,9 @@ def pytest_addoption(parser):
parser.addini(
"junit_logging",
"Write captured log messages to JUnit report: "
"one of no|system-out|system-err",
"one of no|log|system-out|system-err|out-err|all",
default="no",
) # choices=['no', 'stdout', 'stderr'])
)
parser.addini(
"junit_log_passing_tests",
"Capture log information for passing tests to JUnit report: ",
Expand Down
11 changes: 7 additions & 4 deletions testing/acceptance_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1297,7 +1297,11 @@ def test_simple():
"""
)
result = testdir.runpytest_subprocess(
testpath, "--capture=tee-sys", "--junitxml=output.xml"
testpath,
"--capture=tee-sys",
"--junitxml=output.xml",
"-o",
"junit_logging=all",
)

# ensure stdout/stderr were 'live printed'
Expand All @@ -1307,6 +1311,5 @@ def test_simple():
# now ensure the output is in the junitxml
with open(os.path.join(testdir.tmpdir.strpath, "output.xml"), "r") as f:
fullXml = f.read()

assert "<system-out>@this is stdout@\n</system-out>" in fullXml
assert "<system-err>@this is stderr@\n</system-err>" in fullXml
assert "@this is stdout@\n" in fullXml
assert "@this is stderr@\n" in fullXml

0 comments on commit 3789bb5

Please sign in to comment.