Python html logger

I use python logging moduleto enter console and text file. I use HTMLTestRunnerto enter the html file. I assume I'm using HTMLTestRunner 0.8.0

However, the problem is that it HTMLTestRunneronly prints expressions print, and the logging module does not print print statements. Is there a way that I can get instructions for logging inside the html file

Below is the htmlrunner code

runner = HTMLTestRunner.HTMLTestRunner(
                stream=outfile,
                title='Test Report',
                description='Regression Test Suite',
                verbosity=3
                )
result = runner.run(suite)

Edited: I use import unittest, and I use for logging import logging. For HTML runner I useimport HTMLTestRunner

I use logging.infoand to print instructions on the console logging.debug. If I use print instructions using python print, then I do not get this output in the console or in the logs. But for HTML logging, I only have printand operators stderr.

My request is: is there something that HTMLTestRunner.HTMLTestRunnerwill have instructions that are printed to the console using logging.debugandlogging.info

+4
source share
1 answer

The module HTMLTestRunneryou are using now is about 6 years old and is seriously out of date. There are better options - starting with html-testRunneror even better - pytestwith the HTML message plugin included .

, / HTMLTestRunner, , , ( ):

  • logger HTMLTestRunner :

    class HTMLTestRunner(Template_mixin):
        def __init__(self, stream=sys.stdout, verbosity=1, title=None, description=None, logger=None):  # CHANGE HERE
            # ...
    
            # CHANGE BELOW
            self.log_capture = None
            if logger:
                self.log_capture = StringIO.StringIO()
    
                ch = logging.StreamHandler(self.log_capture)
                ch.setLevel(logging.DEBUG)
    
                formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
                ch.setFormatter(formatter)
    
                logger.addHandler(ch)
    
  • HEADING_TMPL :

    HEADING_TMPL = """<div class='heading'>
    <h1>%(title)s</h1>
    %(parameters)s
    <p class='description'>%(description)s</p>
    Logging output:
    <p class='logs'>%(logs)s</p>
    </div>
    """ # variables: (title, parameters, description)
    
  • _generate_heading() logs placeholder:

    heading = self.HEADING_TMPL % dict(
        title = saxutils.escape(self.title),
        parameters = ''.join(a_lines),
        description = saxutils.escape(self.description),
        logs='<br>'.join(self.log_capture.getvalue().split("\n")) if self.log_capture else ""
    )
    
  • import logging

, , , logger , , HTMLTestRunner, :

import logging
import unittest

import HTMLTestRunner

logger = logging.getLogger()
logger.setLevel(logging.DEBUG)


class BasicTestCase(unittest.TestCase):
    def test_one(self):
        logger.info('Test message 1!')
        self.assertEqual(1, 1)

    def test_two(self):
        """Extended description"""
        logger.error('Test message 1!')
        self.assertEqual(2, 2)


if __name__ == '__main__':
    import sys
    logging.basicConfig(stream=sys.stderr)

    with open('report.html', 'w') as report_file:
        runner = HTMLTestRunner.HTMLTestRunner(
            stream=report_file,
            title='Test Report',
            description='Regression Test Suite',
            verbosity=3,
            logger=logger
        )

        suite = unittest.TestLoader().loadTestsFromTestCase(BasicTestCase)

        result = runner.run(suite)
        print(result)

report.html :

enter image description here

+5

Source: https://habr.com/ru/post/1691179/


All Articles