![](/img/trans.png)
[英]Django: why i can't get the tracebacks (in case of error) when i run LiveServerTestCase tests?
[英]How can I force my Python unit tests to print tracebacks after error logs?
我试图对一些复杂的网络内容进行单元测试,并且每次出现错误时,我的网络库都会输出数百行日志消息。 我唯一感兴趣的是错误的实际回溯,但是我必须向上滚动所有这些日志消息才能看到回溯。
有什么方法可以强制Python的unittest模块在日志后输出回溯,而不是其他方式?
例如,我目前看到以下内容:
======================================================================
ERROR: my_test (tests.MyTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "my_code_file.py", line 39, in my_function
my_module.my_function()
File "my_code_file.py", line 78, in my_function
raise Exception("My exception here")
Exception: My exception here
-------------------- >> begin captured logging << --------------------
<<< hundreds of lines of logs from networking libraries/wiretraces/etc>>>
<<< hundreds of lines of logs from networking libraries/wiretraces/etc>>>
<<< hundreds of lines of logs from networking libraries/wiretraces/etc>>>
<<< hundreds of lines of logs from networking libraries/wiretraces/etc>>>
<<< hundreds of lines of logs from networking libraries/wiretraces/etc>>>
-------------------- >> end captured logging << --------------------
我希望看到这样的内容,以便在运行测试后可以快速看到错误:
======================================================================
-------------------- >> begin captured logging << --------------------
<<< hundreds of lines of logs from networking libraries/wiretraces/etc>>>
<<< hundreds of lines of logs from networking libraries/wiretraces/etc>>>
<<< hundreds of lines of logs from networking libraries/wiretraces/etc>>>
<<< hundreds of lines of logs from networking libraries/wiretraces/etc>>>
<<< hundreds of lines of logs from networking libraries/wiretraces/etc>>>
-------------------- >> end captured logging << --------------------
ERROR: my_test (tests.MyTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "my_code_file.py", line 39, in my_function
my_module.my_function()
File "my_code_file.py", line 78, in my_function
raise Exception("My exception here")
Exception: My exception here
如何使用logging
模块?
老实说,我不确定您将使用哪种功能以获得最佳效果。
但是,有logging.exception
EX:
import logging
try:
print blah
except Exception, e:
logging.exception("\n\n !!!!!! \n\n {} \n\n !!!!!! \n\n".format(e))
可以看到该异常的输出:
ERROR:root:
!!!!!!
name 'blah' is not defined
!!!!!!
Traceback (most recent call last):
File "<ipython-input-39-99d79696f33a>", line 2, in <module>
print blah
NameError: name 'blah' is not defined
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.