在 PyCharm 中进行单元测试时如何查看日志消息? [英] How can I see log messages when unit testing in PyCharm?

查看:120
本文介绍了在 PyCharm 中进行单元测试时如何查看日志消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确定这是一个简单的修复,但我想在运行单元测试时在 PyCharm 控制台中查看日志消息.我正在测试的模块有自己的记录器,通常我会设置一个根记录器来捕获某个级别的调试消息,并将其他日志通过管道传输到一个文件.但我无法弄清楚这如何与单元测试配合使用.

I'm sure this is a simple fix, but I'd like to view log messages in the PyCharm console while running a unit test. The modules I'm testing have their own loggers, and normally I'd set a root logger to catch the debugging messages at a certain level, and pipe the other logs to a file. But I can't figure out how this works with unit tests.

我正在使用 unittest2 模块,并使用 PyCharm 的自动测试发现(可能基于鼻子,但我不知道).

I'm using the unittest2 module, and using PyCharm's automatic test discovery (which probably is based on nose, but I don't know).

我试过欺骗运行配置,但似乎没有直接的方法来做到这一点.

I've tried fooling with the run configurations, but there doesn't seem to be a straightforward way to do this.

PyCharm 文档在这里也不是特别有用,如果你们在那里工作的话.

The PyCharm documentation isn't particularly helpful here either, if any of you work there.

在编辑中:似乎控制台捕获了关键级别的日志消息.我想知道是否有办法对其进行配置以捕获调试级别的消息.

In edit: It DOES appear that the console catches critical level log messages. I want to know if there is a way to configure this to catch debug level messages.

这篇文章(Pycharm 单元测试交互式调试命令行不't work) 建议将 -s 选项添加到构建配置中,这不会产生预期的结果.

This post (Pycharm unit test interactive debug command line doesn't work) suggests adding the -s option to the build configuration, which does not produce the desired result.

推荐答案

我找到的唯一解决方案是在文件顶部进行正常的日志记录设置,其中包含测试(假设您只是运行一个测试)或测试类):logging.basicConfig(level=logging.DEBUG).确保将其放在大多数 import 语句之前,或者已经设置了这些模块的默认日志记录(这很难弄清楚!).

The only solution I've found is to do the normal logging setup at the top of the file with tests in it (assuming you're just running one test or test class): logging.basicConfig(level=logging.DEBUG). Make sure you put that before most import statements or the default logging for those modules will have already been set (that was a hard one to figure out!).

这篇关于在 PyCharm 中进行单元测试时如何查看日志消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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