Skip to content

Commit

Permalink
Add captured log msgs to junit xml file
Browse files Browse the repository at this point in the history
For each tests this adds the captured log msgs to the respective
'system-out' tags in the junit xml output file.
  • Loading branch information
twmr committed Jan 26, 2018
1 parent a580990 commit b6bb49a
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 6 deletions.
29 changes: 24 additions & 5 deletions _pytest/junitxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,30 @@ def _add_simple(self, kind, message, data=None):
self.append(node)

def write_captured_output(self, report):
for capname in ('out', 'err'):
content = getattr(report, 'capstd' + capname)
if content:
tag = getattr(Junit, 'system-' + capname)
self.append(tag(bin_xml_escape(content)))
content_out = report.capstdout
content_log = report.caplog
content_err = report.capstderr

if content_log or content_out:
if content_out and content_log:
# syncing stdout and the log-output is not done yet. It's
# probably not worth the effort. Therefore, first the capture
# stdout is shown and then the captured logs.
content = '\n'.join([
' Captured Stdout '.center(80, '-'),
content_out,
'',
' Captured Log '.center(80, '-'),
content_log])
elif content_out:
content = content_out
elif content_log:
content = content_log
tag = getattr(Junit, 'system-out')
self.append(tag(bin_xml_escape(content)))
if content_err:
tag = getattr(Junit, 'system-err')
self.append(tag(bin_xml_escape(content_err)))

def append_pass(self, report):
self.add_stats('passed')
Expand Down
8 changes: 8 additions & 0 deletions _pytest/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,14 @@ def longreprtext(self):
exc = tw.stringio.getvalue()
return exc.strip()

@property
def caplog(self):
"""Return captured log lines, if log capturing is enabled
.. versionadded:: 3.4
"""
return '\n'.join(content for (prefix, content) in self.get_sections('Captured log'))

@property
def capstdout(self):
"""Return captured text from stdout, if capturing is enabled
Expand Down
1 change: 1 addition & 0 deletions changelog/3156.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Captured log messages are added to the ``<system-out>`` tags in the generated junit xml file.
8 changes: 7 additions & 1 deletion testing/test_junitxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,10 +330,14 @@ def test_internal_error(self, testdir):

def test_failure_function(self, testdir):
testdir.makepyfile("""
import logging
import sys
def test_fail():
print ("hello-stdout")
sys.stderr.write("hello-stderr\\n")
logging.info('info msg')
logging.warning('warning msg')
raise ValueError(42)
""")

Expand All @@ -344,7 +348,7 @@ def test_fail():
tnode = node.find_first_by_tag("testcase")
tnode.assert_attr(
file="test_failure_function.py",
line="1",
line="3",
classname="test_failure_function",
name="test_fail")
fnode = tnode.find_first_by_tag("failure")
Expand All @@ -353,6 +357,8 @@ def test_fail():
systemout = fnode.next_siebling
assert systemout.tag == "system-out"
assert "hello-stdout" in systemout.toxml()
assert "warning msg" in systemout.toxml()
assert "info msg" not in systemout.toxml()
systemerr = systemout.next_siebling
assert systemerr.tag == "system-err"
assert "hello-stderr" in systemerr.toxml()
Expand Down

0 comments on commit b6bb49a

Please sign in to comment.