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

标签 python unit-testing logging command-line nosetests

我创建了一个日志类:

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

日志记录未完成,日志文件仍为空。

我该如何解决这个问题?

最佳答案

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

关于使用 nosetests.exe 的 Python 单元测试阻止使用日志记录模块登录文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30009216/

相关文章:

c++ - 如何测试从 std::istream 读取类?

java - 如何只转义消息而不是 log4j 中的所有行?

node.js - 在 Node.js Express 日志文件中发现奇怪的 http 请求

python - python写文件处理编码

python - 使用 Python Beautifulsoup 抓取天使列表配置文件描述

node.js - 如何在不修改源代码的情况下获得 Mocha 测试的代码覆盖率?

c# - 使用具有预加载功能的内存数据库对 EF Core 进行单元测试

logging - Android Lollipop 内核中的记录器驱动程序

python - uwsgi - 从日志文件中删除 "address space usage"消息

python - 对 map 对象进行成员资格测试时出现意外结果