我在用nosetests test.py
运行单元测试:
import unittest
import logging
class Test(unittest.TestCase):
def test_pass(self):
logging.getLogger('do_not_want').info('HIDE THIS')
logging.getLogger('test').info('TEST PASS')
self.assertEqual(True, True)
def test_fail(self):
logging.getLogger('do_not_want').info('HIDE THIS')
logging.getLogger('test').info('TEST FAIL')
self.assertEqual(True, False)
当测试失败时,它会打印出所有日志信息。我可以用--logging-filter
仅过滤掉一些记录器:
nosetests test.py --verbosity=2 --logging-filter=test
test_fail (test.Test) ... FAIL
test_pass (test.Test) ... ok
======================================================================
FAIL: test_fail (test.Test)
----------------------------------------------------------------------
Traceback (most recent call last):
File ".../test.py", line 14, in test_fail
self.assertEqual(True, False)
AssertionError: True != False
-------------------- >> begin captured logging << --------------------
test: INFO: TEST FAIL
--------------------- >> end captured logging << ---------------------
----------------------------------------------------------------------
Ran 2 tests in 0.001s
FAILED (failures=1)
但是,当测试通过时它不会显示任何内容。
我想在测试通过时看到一个特定记录器的输出。我发现我可以使用-s
显示所有 stdout / stderr 文本,这并不完全是我所需要的 - 它会打印所有内容。我尝试使用各种设置,例如--nologcapture
, --nocapture
, or --logging-filter
但我没能达到预期的效果。
nosetests --help
根本没有让这一点变得明显,但答案是--debug
旗帜。该标志将您希望从中接收消息的记录器的名称作为参数。
这是 OP 代码的稍微修改版本:
# test.py
import unittest
import logging
class Test(unittest.TestCase):
def test_pass(self):
logging.getLogger('hide.this').info('HIDE THIS')
logging.getLogger('show.this').info('TEST PASS')
self.assertEqual(True, True)
def test_fail(self):
logging.getLogger('hide.this').info('HIDE THIS')
logging.getLogger('show.this').info('TEST FAIL')
self.assertEqual(True, False)
对于这个例子,nosetests test.py --debug=show.this
应该可以解决问题。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)