我正在使用 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
或 --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
应该可以解决问题。
关于python - 即使测试通过,也让 Nose 测试运行器显示日志记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32565562/