0

I am new to unit testing in Python, So I am thinking to start with this snippet, how I can unit test this function?

def log():

    log_formatter = logging.Formatter('%(asctime)s %(levelname)s %(funcName)s(%(lineno)d) %(message)s')

    logFile = './df_logger.log'

    handler = RotatingFileHandler(logFile, mode='a', maxBytes=1*1024*1024*1024, 
                                 backupCount=2, encoding=None, delay=0)

    console_handler = logging.StreamHandler(sys.stdout)
    console_handler.setFormatter(log_formatter)
    handler.setFormatter(log_formatter)
    handler.setLevel(logging.INFO)

    logger = logging.getLogger('root')
    logger.setLevel(logging.INFO)

    logger.addHandler(handler)
    logger.addHandler(console_handler)
    return logger
ArnabGhosh
  • 35
  • 6

1 Answers1

0

This function returns a logger handler with some settings that you did inside of it.

I would mock the logging, and assert the settings. Here is an example:

How can I patch / mock logging.getlogger()

Ramon Medeiros
  • 2,272
  • 2
  • 24
  • 41