Add logs in junit xml from pytest_generate_tests method

Viewed 1393

I'm working with Jenkins which parses the Junit XML. Any developer would also look at the Jenkins failure for triaging purpose. That means all the logs should be available in Junit xml.

The issue is that the method pytest_generate_tests has a very big implementation and requires log messages for troubleshooting purpose.

However, the log messages added in pytest_generate_tests does not appear in Junit xml. Is there a way to include the log messages in Junit xml if the test discovery was failed?

The command I'm providing

pytest -vv --log-level=INFO --junit-xml=sample.xml

pytest.ini (added junit_logging in a hope that Junit_xml will include the logs)

[pytest]
junit_logging = all

my sample Junit.xml (Without any logs related to pytest_generate_tests )

<?xml version="1.0" encoding="utf-8"?>
<testsuites>
<testsuite errors="1" failures="0" hostname="W117-JayJ" name="pytest" skipped="0" tests="1" time="0.960" timestamp="2020-04-12T10:27:31.178617">
<testcase classname="tests.test_my_addon" file="tests/test_my_addon.py" name="Test_App" time="0.000"><error message="collection failure">pyparallel\lib\site-packages\pluggy\hooks.py:286: in __call__
    return self._hookexec(self, self.get_hookimpls(), kwargs

    . . . a Big stack trace . . .
    raise Exception(&quot;This is a random Failure &quot;)
E   Exception: This is a random Failure </error>
</testcase>
</testsuite>
</testsuites>

There's no log messages attatched in the Junit xml.

I can look at the logs in the console after "collecting ..."

========================================================================================== test session starts ==========================================================================================
platform win32 -- Python 3.7.4, pytest-5.4.1, py-1.8.1, pluggy-0.13.1
rootdir: C:\Jay\Work\Automation\pytest-sample-addon\new_dev_environment, inifile: pytest.ini, testpaths: tests
plugins: lovely-pytest-docker-0.1.0, forked-1.1.3, xdist-1.31.0, sample-addon-0.1.0
collecting ... generating testcases for app. fixture=app_positive_cases
generating field tests..
collected 0 items / 1 error

Is there a way to handle the logs properly in the console and add it to Junit.xml as well?

1 Answers

I had some success in a similar situation by trying:

junit_logging = system-out

Trying it with all didn't work, but system-out did. Try this and see if it yields logs.

Related