使用nosetests.exe进行Python单元测试,防止在使用logging模块时将日志记录到文件中

3
我创建了一个用于日志记录的类:
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)

我使用这个工具来将测试输出记录到文件中: 示例:

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

当我在PyCharm IDE中运行测试时,这个功能可以正常工作;日志记录已经完成。

我的问题在于,当我通过命令行运行nosetests.exe来运行单元测试时,它无法正常工作,如下所示:

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

使用或不使用--with-xunit

未执行日志记录,日志文件为空。

如何解决此问题?

1个回答

3

在命令行上尝试使用--nologcapture选项。如果我没记错的话,Nose的logcapture插件默认拦截所有日志记录,这导致basicConfig没有预期的效果。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接