简体   繁体   English

Django - 测试失败时记录

[英]Django - logging when test fails

I have a lot of unit tests in Django and if a test fails, I often need to see the logs (in the console, if possible). 我在Django中有很多单元测试,如果测试失败,我经常需要查看日志(如果可能的话,在控制台中)。 I can't really use the log file because it gets really messy. 我真的不能使用日志文件,因为它变得非常混乱。
What I do right now is: activate console logging in the settings.py , and only run one specific test. 我现在所做的是:在settings.py激活控制台日志记录,并且只运行一个特定的测试。 I was hoping that there is a more convenient way to do it. 我希望有一个更方便的方法来做到这一点。 I only want to see what was being logged for the failing tests and not the complete log. 我只想查看失败测试的记录内容,而不是完整的日志。

Edit: even though an answer was posted — and I have accepted it — I'm not quite content. 编辑:即使答案已经发布 - 我接受了 - 我不太满意。 I want to see only the logging output of failing tests. 我想只看到失败测试的日志输出。 I was looking for a solution like that which PHPUnit provides. 我正在寻找像PHPUnit提供的解决方案。 It captures the complete output (for logging + print) and only writes it to stdout if the test fails. 它捕获完整的输出(用于记录+打印),并且只有在测试失败时才将其写入stdout

Set the root logger to use the console handler only when running tests. 设置根记录器仅在运行测试时使用控制台处理程序。 Easiest way to detect is to see if "test" is the second argv parameter. 最简单的检测方法是查看“test”是否是第二个argv参数。 Make sure all interesting loggers have "propagate": True so that they forward their logs to the root logger. 确保所有有趣的记录器都"propagate": True以便将日志转发到根记录器。

# settings.py
import sys

if sys.argv[1] == 'test':
    LOGGING['root'] = {
        'handlers': ['console'],
        'level': 'DEBUG',
    }

Well, I just figured out how to fix it. 好吧,我只是弄清楚如何解决它。
Apparently there is a -b option that "buffers the output" unless the test fails when running a unit tests. 显然有一个-b选项可以“缓冲输出”,除非在运行单元测试时测试失败。 It is very easy to use if your tests.py file executes unittest.main() . 如果tests.py文件执行unittest.main() ,则非常容易使用。 Just type python tests.py -b and the complete output will be buffered unless the test fails. 只需键入python tests.py -b ,即使测试失败,也会缓冲完整输出。

...........F
Stdout:
OK :)
OK :)

======================================================================
FAIL: testStr (__main__.MyTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "tests.py", line 93, in testStr
    self.fail()
AssertionError: None

Stdout:
OK :)
OK :)

----------------------------------------------------------------------
Ran 12 tests in 0.005s

In this case, every test printed something to stdout but only the very last one (the failing one was shown). 在这种情况下,每个测试都会向stdout打印一些东西,但只打印最后一个(显示失败的一个)。 Since the log can also be printed to the console (see @Thomas's answer), you will only see relevant logs. 由于日志也可以打印到控制台(请参阅@ Thomas的答案),您只能看到相关的日志。

The quick and dirty way to access your log output on the console is to monkey patch logging.. To overwrite logging.info for example: 在控制台上访问日志输出的快速而又脏的方法是使用补丁日志记录..例如,覆盖logging.info:

def monkey_print(*args):
   print args
setattr(logging, 'info', monkey_print)

Then just delete it when you're done. 然后在完成后删除它。 Quick and dirty. 又脏又脏。 It works with unittest too. 它也适用于单元测试。

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

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