Fen*_*kso 24 python logging unit-testing nose
我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)
Run Code Online (Sandbox Code Playgroud)
当测试失败时,它会打印出所有日志记录信息.我可以使用--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)
Run Code Online (Sandbox Code Playgroud)
但是,测试通过时它没有显示任何内容.
我希望在测试通过时看到一个特定记录器的输出.我发现我可以-s
用来显示所有stdout/stderr文本,这不是我需要的 - 它会打印所有内容.我试着用各种设置,如播放--nologcapture
,--nocapture
或--logging-filter
但我没能得到预期的效果.
Bri*_*ion 23
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)
Run Code Online (Sandbox Code Playgroud)
对于这个例子,nosetests test.py --debug=show.this
应该做的伎俩.
归档时间: |
|
查看次数: |
6885 次 |
最近记录: |