Youcef Kelanemer
Youcef Kelanemer

Reputation: 107

Python unit test using nosetests.exe prevent logging in files using logging module

I created a class for logging:

import logging, time


class QaLogger():

    def __init__(self, filename='LOG.log', logger_name='Qa_Automation'):
        logging.basicConfig(filename=filename, level=logging.INFO)
        self.logger = logging.getLogger(logger_name)
        self.logger.initialized = True

    def log(self, msg):
        localtime  = time.localtime()
        time_string = time.strftime("%Y-%m-%d-%H:%M:%S", localtime)
        self.logger.info(time_string + ": " + msg)

Then I use this to log output to files when I run my tests: Example:

    self.logger = QaLogger('qa_req_response.log', 'QA')
    self.logger.log('QA_LOGGING ')

This works fine when I run my tests using PyCharm IDE; The logging in files is done.

My issue is that it does not work when I run the unittests using nosetests.exe from command line like:

> C:\Python27\Scripts\nosetests.exe  .\TestFunction.py   --with-xunit

with or without --with-xunit

Logging is not done and log files remain empty.

How can I solve this issue?

Upvotes: 3

Views: 417

Answers (1)

m.brindley
m.brindley

Reputation: 1228

Try the --nologcapture option on the command line. Nose's logcapture plugin intercepts all logging by default if I recall correctly, which results in basicConfig not having the expected effect.

Upvotes: 3

Related Questions