From 617bf8be5b0d5fa59dfb72a27c66f4f5f54f7e26 Mon Sep 17 00:00:00 2001 From: David Diaz Barquero Date: Tue, 23 Jun 2020 10:03:46 -0600 Subject: [PATCH] Add details to error message for junit (#7390) Co-authored-by: Bruno Oliveira --- changelog/7385.improvement.rst | 13 +++++++++++++ src/_pytest/junitxml.py | 12 +++++++++--- testing/test_junitxml.py | 12 +++++++----- 3 files changed, 29 insertions(+), 8 deletions(-) create mode 100644 changelog/7385.improvement.rst diff --git a/changelog/7385.improvement.rst b/changelog/7385.improvement.rst new file mode 100644 index 000000000..c02fee5da --- /dev/null +++ b/changelog/7385.improvement.rst @@ -0,0 +1,13 @@ +``--junitxml`` now includes the exception cause in the ``message`` XML attribute for failures during setup and teardown. + +Previously: + +.. code-block:: xml + + + +Now: + +.. code-block:: xml + + diff --git a/src/_pytest/junitxml.py b/src/_pytest/junitxml.py index 86e8fcf38..4df7535de 100644 --- a/src/_pytest/junitxml.py +++ b/src/_pytest/junitxml.py @@ -236,10 +236,16 @@ class _NodeReporter: self._add_simple(Junit.skipped, "collection skipped", report.longrepr) def append_error(self, report: TestReport) -> None: - if report.when == "teardown": - msg = "test teardown failure" + assert report.longrepr is not None + if getattr(report.longrepr, "reprcrash", None) is not None: + reason = report.longrepr.reprcrash.message else: - msg = "test setup failure" + reason = str(report.longrepr) + + if report.when == "teardown": + msg = 'failed on teardown with "{}"'.format(reason) + else: + msg = 'failed on setup with "{}"'.format(reason) self._add_simple(Junit.error, msg, report.longrepr) def append_skipped(self, report: TestReport) -> None: diff --git a/testing/test_junitxml.py b/testing/test_junitxml.py index f8a6a295f..5e5826b23 100644 --- a/testing/test_junitxml.py +++ b/testing/test_junitxml.py @@ -266,7 +266,7 @@ class TestPython: @pytest.fixture def arg(request): - raise ValueError() + raise ValueError("Error reason") def test_function(arg): pass """ @@ -278,7 +278,7 @@ class TestPython: tnode = node.find_first_by_tag("testcase") tnode.assert_attr(classname="test_setup_error", name="test_function") fnode = tnode.find_first_by_tag("error") - fnode.assert_attr(message="test setup failure") + fnode.assert_attr(message='failed on setup with "ValueError: Error reason"') assert "ValueError" in fnode.toxml() @parametrize_families @@ -290,7 +290,7 @@ class TestPython: @pytest.fixture def arg(): yield - raise ValueError() + raise ValueError('Error reason') def test_function(arg): pass """ @@ -301,7 +301,7 @@ class TestPython: tnode = node.find_first_by_tag("testcase") tnode.assert_attr(classname="test_teardown_error", name="test_function") fnode = tnode.find_first_by_tag("error") - fnode.assert_attr(message="test teardown failure") + fnode.assert_attr(message='failed on teardown with "ValueError: Error reason"') assert "ValueError" in fnode.toxml() @parametrize_families @@ -328,7 +328,9 @@ class TestPython: fnode = first.find_first_by_tag("failure") fnode.assert_attr(message="Exception: Call Exception") snode = second.find_first_by_tag("error") - snode.assert_attr(message="test teardown failure") + snode.assert_attr( + message='failed on teardown with "Exception: Teardown Exception"' + ) @parametrize_families def test_skip_contains_name_reason(self, testdir, run_and_parse, xunit_family):