From f9f092a5e6dc27d28b220fde75b348f086ef85c0 Mon Sep 17 00:00:00 2001 From: Claudio Madotto Date: Sun, 24 Nov 2019 15:20:02 +0100 Subject: [PATCH 1/3] Fix for issue #5430 - junit-xml: logs are not passed to junit report for tests failed not in a teardown phase --- src/_pytest/junitxml.py | 1 + testing/test_junitxml.py | 42 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/src/_pytest/junitxml.py b/src/_pytest/junitxml.py index 9cf22705e..21b54ef86 100644 --- a/src/_pytest/junitxml.py +++ b/src/_pytest/junitxml.py @@ -591,6 +591,7 @@ class LogXML: if report.when == "call": reporter.append_failure(report) self.open_reports.append(report) + reporter.write_captured_output(report) else: reporter.append_error(report) elif report.skipped: diff --git a/testing/test_junitxml.py b/testing/test_junitxml.py index 4c2f22a3d..0132db59d 100644 --- a/testing/test_junitxml.py +++ b/testing/test_junitxml.py @@ -1477,3 +1477,45 @@ def test_logging_passing_tests_disabled_does_not_log_test_output( node = dom.find_first_by_tag("testcase") assert len(node.find_by_tag("system-err")) == 0 assert len(node.find_by_tag("system-out")) == 0 + + +@parametrize_families +@pytest.mark.parametrize("junit_logging", ["no", "system-out", "system-err"]) +def test_logging_passing_tests_disabled_logs_output_for_failing_test_issue5430( + testdir, junit_logging, run_and_parse, xunit_family +): + testdir.makeini( + """ + [pytest] + junit_log_passing_tests=False + junit_family={family} + """.format( + family=xunit_family + ) + ) + testdir.makepyfile( + """ + import pytest + import logging + import sys + + def test_func(): + logging.warning('hello') + assert 0 + """ + ) + result, dom = run_and_parse( + "-o", "junit_logging=%s" % junit_logging, family=xunit_family + ) + assert result.ret == 1 + node = dom.find_first_by_tag("testcase") + if junit_logging == "system-out": + assert len(node.find_by_tag("system-err")) == 0 + assert len(node.find_by_tag("system-out")) == 1 + elif junit_logging == "system-err": + assert len(node.find_by_tag("system-err")) == 1 + assert len(node.find_by_tag("system-out")) == 0 + else: + assert junit_logging == "no" + assert len(node.find_by_tag("system-err")) == 0 + assert len(node.find_by_tag("system-out")) == 0 From d940d0b6570535a4e39fed31166fef67ed8769af Mon Sep 17 00:00:00 2001 From: Claudio Madotto Date: Sun, 24 Nov 2019 16:08:45 +0100 Subject: [PATCH 2/3] Create changelog file and update AUTHORS --- AUTHORS | 1 + changelog/5430.bugfix.rst | 1 + 2 files changed, 2 insertions(+) create mode 100644 changelog/5430.bugfix.rst diff --git a/AUTHORS b/AUTHORS index 6e2f472fe..cc9b14be3 100644 --- a/AUTHORS +++ b/AUTHORS @@ -61,6 +61,7 @@ Christian Theunert Christian Tismer Christopher Gilling Christopher Dignam +Claudio Madotto CrazyMerlyn Cyrus Maden Damian Skrzypczak diff --git a/changelog/5430.bugfix.rst b/changelog/5430.bugfix.rst new file mode 100644 index 000000000..734685063 --- /dev/null +++ b/changelog/5430.bugfix.rst @@ -0,0 +1 @@ +junitxml: Logs for failed test are now passed to junit report in case the test fails during call phase. From 2d24c062b6c1beb914399174c1fd9190f6093371 Mon Sep 17 00:00:00 2001 From: Claudio Madotto Date: Sun, 24 Nov 2019 16:33:17 +0100 Subject: [PATCH 3/3] Avoid duplicating system-out and system-error tags --- src/_pytest/junitxml.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/_pytest/junitxml.py b/src/_pytest/junitxml.py index 21b54ef86..206e44d96 100644 --- a/src/_pytest/junitxml.py +++ b/src/_pytest/junitxml.py @@ -591,7 +591,8 @@ class LogXML: if report.when == "call": reporter.append_failure(report) self.open_reports.append(report) - reporter.write_captured_output(report) + if not self.log_passing_tests: + reporter.write_captured_output(report) else: reporter.append_error(report) elif report.skipped: