PyDev单元测试:如何捕获记录到日志中的文本. [英] PyDev unittesting: How to capture text logged to a logging.Logger in "Captured Output"

查看:77
本文介绍了PyDev单元测试:如何捕获记录到日志中的文本.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PyDev进行我的Python应用程序的开发和单元测试. 至于单元测试,除了没有内容记录到日志记录框架这一事实之外,一切都可以正常工作.记录器未被PyDev的捕获的输出"捕获.

I am using PyDev for development and unit-testing of my Python application. As for unit-testing, everything works great except the fact that no content is logged to the logging framework. The logger is not captured by the "Captured output" of PyDev.

我已经将记录的所有内容转发到标准输出,如下所示:

I'm already forwarding everything logged to the standard output like this:

import sys
logger = logging.getLogger()
logger.level = logging.DEBUG
logger.addHandler(logging.StreamHandler(sys.stdout))

尽管如此,捕获的输出"不会显示记录到记录器的内容.

Nevertheless the "Captured output" does not display the stuff logged to loggers.

这是一个示例unittest脚本: test.py

Here's an example unittest-script: test.py

import sys
import unittest
import logging

logger = logging.getLogger()
logger.level = logging.DEBUG
logger.addHandler(logging.StreamHandler(sys.stdout))

class TestCase(unittest.TestCase):
    def testSimpleMsg(self):
        print("AA")
        logging.getLogger().info("BB")

控制台输出为:

Finding files... done.
Importing test modules ... done.

testSimpleMsg (itf.lowlevel.tests.hl7.TestCase) ... AA
2011-09-19 16:48:00,755 - root - INFO - BB
BB
ok

----------------------------------------------------------------------
Ran 1 test in 0.001s

OK

但是该测试的捕获的输出是:

======================== CAPTURED OUTPUT =========================
AA

有人知道如何在执行此测试期间捕获记录到logging.Logger的所有内容吗?

Does anybody know how to capture everything that is logged to a logging.Logger during the execution of this test?

推荐答案

问题是unittest运行程序在测试开始之前替换了sys.stdout/sys.stderr,并且StreamHandler仍在写入原始内容sys.stdout.

The issue is that the unittest runner replaces sys.stdout/sys.stderr before the testing starts, and the StreamHandler is still writing to the original sys.stdout.

如果您将'current'sys.stdout分配给处理程序,它应该可以工作(请参见下面的代码).

If you assign the 'current' sys.stdout to the handler, it should work (see the code below).

import sys
import unittest
import logging

logger = logging.getLogger()
logger.level = logging.DEBUG
stream_handler = logging.StreamHandler(sys.stdout)
logger.addHandler(stream_handler)

class TestCase(unittest.TestCase):
    def testSimpleMsg(self):
        stream_handler.stream = sys.stdout
        print("AA")
        logging.getLogger().info("BB")

尽管如此,一种更好的方法是在测试过程中添加/删除处理程序:

Although, a better approach would be adding/removing the handler during the test:

import sys
import unittest
import logging

logger = logging.getLogger()
logger.level = logging.DEBUG

class TestCase(unittest.TestCase):
    def testSimpleMsg(self):
        stream_handler = logging.StreamHandler(sys.stdout)
        logger.addHandler(stream_handler)
        try:
            print("AA")
            logging.getLogger().info("BB")
        finally:
            logger.removeHandler(stream_handler)

这篇关于PyDev单元测试:如何捕获记录到日志中的文本.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆