[英]Increasing logging verbosity of class being unit-tested in Python
I have a Python class that uses the logging module to provide some debug output: 我有一个Python类,它使用日志记录模块来提供一些调试输出:
File someclass.py: 文件someclass.py:
import logging
class SomeClass:
def do_stuff(self):
# do some things
logging.debug("I just did some stuff")
# do some more stuff
return True
I do unit testing on this class with the unittest module 我使用unittest模块对这个类进行单元测试
File test_someclass.py 文件test_someclass.py
import unittest
from someclass import SomeClass
class SomeClassTests(unittest.TestCase):
def test_do_stuff(self):
obj = SomeClass()
self.assertFalse(obj.do_stuff())
def main():
unittest.main()
if __name__ == '__main__':
main()
What I want to do is show the debug messages while I am running the unit tests. 我想要做的是在运行单元测试时显示调试消息。 I tried to set the verbosity to debug from the unit test module:
我试图从单元测试模块设置详细程度调试:
import logging
# ....
def main():
unittest.main()
logging.basicConfig(level=logging.DEBUG)
This didn't work. 这没用。 What would be the way to achieve this?
实现这个目标的方法是什么? Even better would be enabling DEBUG verbosity for only one test.
更好的是只为一次测试启用DEBUG详细程度。
UPDATE: 更新:
Apparently it works when running it from the Python shell, but not in PyDev (it probably uses a different test runner). 显然它在从Python shell运行时有效,但在PyDev中运行它(它可能使用不同的测试运行器)。
call unittest.main()
from your main()
. 从
main()
调用unittest.main()
main()
。
def main():
logging.basicConfig(level=logging.DEBUG)
unittest.main()
My output shows: 我的输出显示:
DEBUG:root:I just did some stuff
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
The basic example on unittests docs at: https://docs.python.org/2/library/unittest.html shows the simple way of calling and running a unit test from main. 关于单元测试文档的基本示例: https : //docs.python.org/2/library/unittest.html显示了从main调用和运行单元测试的简单方法。
If you want to output debug messages on failures only, using nose
test runner would be the easiest way to go since nose
captures stdout and print it out on failures. 如果您只想在故障时输出调试消息,那么使用
nose
测试运行器将是最简单的方法,因为nose
捕获stdout并在出现故障时将其打印出来。 It works out of the box: 它开箱即用:
$ nosetests test.py
F
======================================================================
FAIL: test_stuff (test.SomeClass)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/../../test.py", line 7, in test_stuff
self.assertFalse(True)
AssertionError: True is not false
-------------------- >> begin captured stdout << ---------------------
I just did some stuff
--------------------- >> end captured stdout << ----------------------
----------------------------------------------------------------------
Ran 1 test in 0.001s
FAILED (failures=1)
where test.py
contains: 其中
test.py
包含:
from unittest import TestCase
class SomeClass(TestCase):
def test_stuff(self):
print "I just did some stuff"
self.assertFalse(True)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.