在Python Pyramid单元测试中查看日志输出

时间:2014-01-02 21:47:35

标签: python testing logging pyramid

我不确定这是否是一个IntelliJ的东西(使用内置的测试运行器)但我有一个类,其日志输出我想出现在我正在运行的测试用例中。我希望示例代码是足够的范围,如果不是我可以编辑以包含更多。

基本上,log.info()类中的Matching()调用在运行时从不显示在我的测试运行器控制台中。我是否需要在扩展TestCase的类上配置?

以下是 matching.py 中的课程:

class Matching(object):
"""
The main compliance matching logic.
"""

request_data = None

def __init__(self, matching_request):
    """
    Set matching request information.
    """

    self.request_data = matching_request

def can_matching_run(self):
    raise Exception("Not implemented yet.")

def run_matching(self):
    log.info("Matching started at {0}".format(datetime.now()))

以下是测试

class MatchingServiceTest(IntegrationTestBase):

def __do_matching(self, client_name, date_range):
    """
    Pull control records from control table, and compare against program generated
    matching data from teh non-control table.

    The ``client_name`` dictates which model to use.  Data is compared within
    a mock ``date_range``.
    """

    from matching import Matching, MatchingRequest

    # Run the actual matching service for client.
    match_request = MatchingRequest(client_name, date_range)
    matcher = Matching(match_request)
    matcher.run_matching()

1 个答案:

答案 0 :(得分:1)

好吧,我没有看到你在哪里初始化log对象,但我认为你在某个地方做了这个并且你为它添加了一个处理程序(StreamHandlerFileHandler等。)

这意味着在测试期间不会发生这种情况。所以你必须在测试中。由于你没有发布这部分代码,我无法给出一个确切的解决方案:

import logging
log = logging.getLogger("your-logger-name")
log.addHandler(logging.StreamHandler())
log.setLevel(logging.DEBUG)

虽然测试一般不应该打印到stdout。最好使用FileHandler,你应该设计你的测试,以便在出现问题时它们会失败。这就是自动化测试的重点。因此您不必手动检查输出。如果它们失败,您可以检查日志以查看它们是否包含有用的调试信息。

希望这有帮助。

了解有关记录here的更多信息。