Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
andkononykhin opened this issue Jun 10, 2019 · 7 comments

Comments

@andkononykhin
Copy link

@andkononykhin andkononykhin commented Jun 10, 2019

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>
@thisch

This comment has been minimized.

Copy link
Contributor

@thisch thisch commented Jun 10, 2019

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?

@andkononykhin

This comment has been minimized.

Copy link
Author

@andkononykhin andkononykhin commented Jun 11, 2019

@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

@zupermanzupereroe

This comment has been minimized.

Copy link
Contributor

@zupermanzupereroe zupermanzupereroe commented Nov 21, 2019

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.

@andkononykhin

This comment has been minimized.

Copy link
Author

@andkononykhin andkononykhin commented Nov 21, 2019

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.

@zupermanzupereroe

This comment has been minimized.

Copy link
Contributor

@zupermanzupereroe zupermanzupereroe commented Nov 22, 2019

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

zupermanzupereroe added a commit to zupermanzupereroe/pytest that referenced this issue Nov 24, 2019
…it report for tests failed not in a teardown phase
zupermanzupereroe added a commit to zupermanzupereroe/pytest that referenced this issue Nov 25, 2019
…it report for tests failed not in a teardown phase
@zupermanzupereroe

This comment has been minimized.

Copy link
Contributor

@zupermanzupereroe zupermanzupereroe commented Nov 25, 2019

Hello @andkononykhin , @thisch I've opened the PR #6274 to fix the problem. Please feel free to leave a comment if there's something wrong with the proposal solution!

@nicoddemus

This comment has been minimized.

Copy link
Member

@nicoddemus nicoddemus commented Dec 12, 2019

Fixed by #6274

@nicoddemus nicoddemus closed this Dec 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants
You can’t perform that action at this time.