编程语言
首页 > 编程语言> > 使用nastests.exe的Python单元测试可防止使用日志记录模块登录文件

使用nastests.exe的Python单元测试可防止使用日志记录模块登录文件

作者:互联网

我创建了一个用于记录的类:

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运行测试时,此方法工作正常;登录文件完成.

我的问题是,当我从以下命令行使用nasetests.exe运行单元测试时,它不起作用:

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

带有或不带有–with-xunit

日志未完成,日志文件仍然为空.

我该如何解决这个问题?

解决方法:

在命令行上尝试–nologcapture选项.如果我没记错的话,默认情况下,Nose的logcapture插件会拦截所有日志记录,这导致basicConfig达不到预期的效果.

标签:unit-testing,logging,command-line,nosetests,python
来源: https://codeday.me/bug/20191120/2043571.html