Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 837
Youcef Kelanemer Avatar asked Nov 27 '25 23:11

Youcef Kelanemer


1 Answers

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.

like image 100
m.brindley Avatar answered Nov 30 '25 11:11

m.brindley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!