Pytest: junit-xml: logs are not passed to junit report for tests failed not in a teardown phase

Created on 10 Jun 2019  路  7Comments  路  Source: pytest-dev/pytest

Logs for failed test are passed to junit report only in case the test fails during teardown phase.
Seems as regression related to the change #5052.

$ pip list
Package            Version
------------------ -------
atomicwrites       1.3.0  
attrs              19.1.0 
importlib-metadata 0.17   
more-itertools     7.0.0  
packaging          19.0   
pathlib2           2.3.3  
pip                19.1.1 
pluggy             0.12.0 
py                 1.8.0  
pyparsing          2.4.0  
pytest             4.6.2  
setuptools         41.0.1 
six                1.12.0 
wcwidth            0.1.7  
wheel              0.33.4 
zipp               0.5.1

Example test_me.py:

import pytest
import logging

logger = logging.getLogger(__name__)


def test_passed():
    logging.info('logs from passed test')
    assert True


def test_failed():
    logging.info('no logs from test failed not in a teardown phase')
    assert False


@pytest.fixture
def faulty_fixture():
    yield
    assert False


def test_failed_during_teardown(faulty_fixture):
    logging.info('logs from test failed during teardown')
    assert True

The command: python -m pytest -l -v test_me.py --junit-xml=report.xml --log-level info produces the following junit report:

<?xml version="1.0" encoding="utf-8"?><testsuite errors="1" failures="1" name="pytest" skipped="0" tests="4" time="0.031"><testcase classname="test_me" file="test_me.py" line="6" name="test_passed" time="0.001"></testcase><testcase classname="test_me" file="test_me.py" line="11" name="test_failed" time="0.001"><failure message="assert False">def test_failed():
        logging.info(&apos;no logs from test failed not in a teardown phase&apos;)
&gt;       assert False
E       assert False


test_me.py:14: AssertionError</failure></testcase><testcase classname="test_me" file="test_me.py" line="22" name="test_failed_during_teardown" time="0.001"><error message="test teardown failure">@pytest.fixture
    def faulty_fixture():
        yield
&gt;       assert False
E       assert False


test_me.py:20: AssertionError</error><system-err>#x1B[32mINFO    #x1B[0m root:test_me.py:24 logs from test failed during teardown</system-err></testcase></testsuite>
junitxml bug

Most helpful comment

@thisch sure, but I can't do that asap: I think I will have time in a few days if it's acceptable for such an issue

All 7 comments

Thx @andkononykhin for this report!
I can reproduce this issue and i think that the following patch should fix your issue:

diff --git a/src/_pytest/junitxml.py b/src/_pytest/junitxml.py
index ea33e606c..8b5decd1f 100644
--- a/src/_pytest/junitxml.py
+++ b/src/_pytest/junitxml.py
@@ -585,6 +585,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:

@andkononykhin Do you want to contribute and create a PR that fixes this issue?

@thisch sure, but I can't do that asap: I think I will have time in a few days if it's acceptable for such an issue

Hi @andkononykhin , can you create the PR, please? The patch solves the issue and let me avoid implementing an hook and defining a fixture to bypass the problem.

Hello @zupermanzupereroe. I tried the patch suggested by @thisch but it didn't work for me. Unfortunately I haven't had any time to dig into that deeper. Hopefully I will have soon or you may contribute yourself if it can't wait.

Hello @andkononykhin, I think I can work on the PR in the next weeks!

Hello @andkononykhin , @thisch I've opened the PR https://github.com/pytest-dev/pytest/pull/6274 to fix the problem. Please feel free to leave a comment if there's something wrong with the proposal solution!

Fixed by #6274

Was this page helpful?
0 / 5 - 0 ratings