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

标签 python testing logging pyramid

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

基本上,Matching() 类中的 log.info() 调用在运行时从未出现在我的测试运行器控制台中。我需要在扩展 TestCase 的类上配置什么吗?

这是 ma​​tching.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()

最佳答案

好吧,我没看到你在哪里初始化了 log 对象,但我假设你在某个地方做了那个,并且你向它添加了一个 Handler (StreamHandler, FileHandler等)

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

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

尽管测试通常不应将任何内容打印到标准输出。最好使用 FileHandler,并且您应该以这样的方式设计您的测试,如果出现问题,它们就会失败。这就是自动化测试的重点。因此您不必手动检查输出。如果失败,您可以检查日志以查看它们是否包含有用的调试信息。

希望这对您有所帮助。

阅读更多关于日志记录的信息 here .

关于python - 在 Python Pyramid 单元测试中查看日志输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20892545/

相关文章:

performance - 如何在 Atom N270 处理器中禁用预取器

ruby - 明确测试库/ gem 是否由 rspec 中的 stub 定义?

php - MVC 模式 : what needs to be created first?

java - 为什么不受信任的代码无法更改 Java Logging 下的日志级别?

python - 如何在 Google App Engine 上防止 "ImportError: No module named oauth2client.client"?

javascript - 使用 selenium Python 单击 javascript 按钮

python - 如何解决类型错误: RelatedManager object is not iterable

jquery - Django-cors-headers 不工作

java - 即使使用 slf4j,您是否应该保护您的日志记录?

git - 如何让 Git Extensions 浏览器显示所有提交,如 gitk --all