EDIT: since for my report I need access to the functions of the test item (and test results), I was able to move the logic to pytest_runtest_makereport(item, __multicall__) . The trick is to perform multicast, which returns a report object:
@pytest.mark.tryfirst def pytest_runtest_makereport(item, call, __multicall__): report = __multicall__.execute()
Bruno's answer gave me the motivation I need for a more thorough analysis of this function :)
So how does it work:
def pytest_runtest_logreport(report): if report.failed: report.longrepr.sections.append(("Header", "Message", "-")) report.sections.append(("Captured stdout", "This is added to stdout")) report.sections.append(("Captured stderr", "This is added to stderr")) report.sections.append(("Custom Section", "This can only be seen in the console - the xml won't have it."))
The longrepr attribute is only available in case of failures. A 3-tuple is required, the last value is the character used to decorate the decoration / surrounding heading. It will appear in the "Failure" section of the report:
Custom sections will create additional sections of results that will be printed to the console. . But they will not use junitxml:
There are only 2 sections in the junitxml report: out and err. To add custom text to it, you must create sections called "Captured std", and only those that fall into the xml file. Any other name will create a custom section that will only be displayed on the console.
Here is the result of junitxml using the code above, with some reformatting for this post:
<?xml version="1.0" encoding="utf-8" ?> <testsuite errors="0" failures="1" name="pytest" skips="0" tests="1" time="0.646"> <testcase classname="test_reporting" name="test_fail" time="0.000999927520752"> <failure message="test failure"> @ut def test_fail(): > assert 0, "It failed" E AssertionError: It failed E assert 0 test_reporting.py:346: AssertionError ----------------------------------- Header ------------------------------------ Message </failure> <system-out>This is added to stdout</system-out> <system-err>This is added to stderr</system-err> </testcase> </testsuite>