Skip to content

Commit e2bb4f8

Browse files
committed
Fix teardown error message in generated xUnit XML
It was "test setup failure" even error happens on test teardown.
1 parent e354455 commit e2bb4f8

3 files changed

Lines changed: 34 additions & 4 deletions

File tree

CHANGELOG.rst

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,16 @@
2020
* Report teardown output on test failure (`#442`_).
2121
Thanks `@matclab`_ or the PR.
2222

23-
*
23+
* Fix teardown error message in generated xUnit XML.
24+
Thanks `@gdyuldin`_ or the PR.
2425

2526
*
2627

2728

2829
.. _@cwitty: https://github.com/cwitty
2930
.. _@okulynyak: https://github.com/okulynyak
3031
.. _@matclab: https://github.com/matclab
32+
.. _@gdyuldin: https://github.com/gdyuldin
3133

3234
.. _#442: https://github.com/pytest-dev/pytest/issues/442
3335
.. _#1976: https://github.com/pytest-dev/pytest/issues/1976
@@ -98,7 +100,7 @@
98100
enabled. This allows proper post mortem debugging for all applications
99101
which have significant logic in their tearDown machinery (`#1890`_). Thanks
100102
`@mbyt`_ for the PR.
101-
103+
102104
* Fix use of deprecated ``getfuncargvalue`` method in the internal doctest plugin.
103105
Thanks `@ViviCoder`_ for the report (`#1898`_).
104106

@@ -395,7 +397,7 @@ time or change existing behaviors in order to make them less surprising/more use
395397

396398
* Refined logic for determining the ``rootdir``, considering only valid
397399
paths which fixes a number of issues: `#1594`_, `#1435`_ and `#1471`_.
398-
Updated the documentation according to current behavior. Thanks to
400+
Updated the documentation according to current behavior. Thanks to
399401
`@blueyed`_, `@davehunt`_ and `@matthiasha`_ for the PR.
400402

401403
* Always include full assertion explanation. The previous behaviour was hiding

_pytest/junitxml.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,12 @@ def append_collect_skipped(self, report):
156156
Junit.skipped, "collection skipped", report.longrepr)
157157

158158
def append_error(self, report):
159+
if getattr(report, 'when', None) == 'teardown':
160+
msg = "test teardown failure"
161+
else:
162+
msg = "test setup failure"
159163
self._add_simple(
160-
Junit.error, "test setup failure", report.longrepr)
164+
Junit.error, msg, report.longrepr)
161165
self._write_captured_output(report)
162166

163167
def append_skipped(self, report):

testing/test_junitxml.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,30 @@ def test_function(arg):
165165
fnode.assert_attr(message="test setup failure")
166166
assert "ValueError" in fnode.toxml()
167167

168+
def test_teardown_error(self, testdir):
169+
testdir.makepyfile("""
170+
import pytest
171+
172+
@pytest.fixture
173+
def arg():
174+
yield
175+
raise ValueError()
176+
def test_function(arg):
177+
pass
178+
""")
179+
result, dom = runandparse(testdir)
180+
assert result.ret
181+
node = dom.find_first_by_tag("testsuite")
182+
tnode = node.find_first_by_tag("testcase")
183+
tnode.assert_attr(
184+
file="test_teardown_error.py",
185+
line="6",
186+
classname="test_teardown_error",
187+
name="test_function")
188+
fnode = tnode.find_first_by_tag("error")
189+
fnode.assert_attr(message="test teardown failure")
190+
assert "ValueError" in fnode.toxml()
191+
168192
def test_skip_contains_name_reason(self, testdir):
169193
testdir.makepyfile("""
170194
import pytest

0 commit comments

Comments
 (0)