Unable to print logging output using xmlrunner for unittest

Viewed 227

I am trying to use xmlrunner to generate a xml of my test result with unittest. I would like the report to include my logging output. But it looks like it doesn't work.

For example, in this code, the logging output is not showing in the xml report.

import unittest
import logging
import xmlrunner
import sys


class Test(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        logging.getLogger().debug("setUpClass")

    def test_step_001(self):
        logging.getLogger().debug("test_step_001")

    @classmethod
    def tearDownClass(cls):
        logging.getLogger().debug("tearDownClass")


if __name__ == '__main__':
    logging.basicConfig(level=logging.DEBUG, format="%(asctime)s %(levelname)s %(message)s", stream=sys.stdout)
    unittest.main(testRunner=xmlrunner.XMLTestRunner(output='test-reports'))
1 Answers

I just worked on this. Its very stupid. You would setup your loghandler inside your setup function. This creates a hierarchical seperation which runs internally in xmlrunner.

    @classmethod
    def setUpClass(cls):
        handler = logging.StreamHandler(sys.stdout)
        handler.setLevel(logging.INFO)
        log.addHandler(handler)
        log.debug("setUpClass")
Related