简体   繁体   English

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

[英]Seeing log output in Python Pyramid unit test

I'm not sure if this is an IntelliJ thing or not (using the built-in test runner) but I have a class whose logging output I'd like to appear in the test case that I am running. 我不确定这是否是IntelliJ事情(使用内置的测试运行器),但是我有一个类,我希望将该类的日志记录输出显示在正在运行的测试用例中。 I hope the example code is enough scope, if not I can edit to include more. 我希望示例代码具有足够的范围,如果没有,我可以进行编辑以包括更多内容。

Basically the log.info() call in the Matching() class never shows up in my test runner console when running. 基本上, Matching()类中的log.info()调用在运行时从不会显示在我的测试运行器控制台中。 Is there something I need to configure on the class that extends TestCase ? 我需要在扩展TestCase的类上进行配置吗?

Here's the class in matching.py : 下面是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()))

Here is the test : 这是测试

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()

Well I do not see where you initialize the log object but I presume you do that somewhere and you add a Handler to it ( StreamHandler , FileHandler etc.) 好吧,我看不到在哪里初始化log对象,但我想您在某个地方进行了初始化,并向其中添加了Handler( StreamHandlerFileHandler等)。

This means that during your tests this does not occur. 这意味着在测试期间不会发生这种情况。 So you would have to that in test. 因此,您必须在测试中做到这一点。 Since you did not post that part of the code, I can't give an exact solution: 由于您没有发布该部分代码,因此我无法提供确切的解决方案:

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

Although test should generally not have anything printed out to stdout. 尽管测试通常不应该将任何内容打印到标准输出。 It's best to use a FileHandler , and you should design your tests in such a way that they will fail if something goes wrong. 最好使用FileHandler ,并且您应该以这样的方式设计测试:如果出现问题,它们将失败。 That's the whole point of automated tests. 这就是自动化测试的重点。 So you won't have to manually inspect the output. 因此,您不必手动检查输出。 If they fail, you can then check the log to see if they contain useful debugging information. 如果它们失败,则可以检查日志以查看它们是否包含有用的调试信息。

Hope this helps. 希望这可以帮助。

Read more on logging here . 此处阅读有关日志记录的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM