我試圖編寫一段Python代碼的單元測試,該代碼在特定條件下通過logger.warn('...')
發出警告。我如何斷言此警告已被記錄?我注意到assertLogged
直到至少Python 3.4纔可用,不幸的是我在2.7。Python 2.7單元測試:Assert Logger警告拋出
10
A
回答
2
在您的單元測試設置中,添加一個記錄處理程序,用於緩存記錄,並在拆卸過程中將其刪除。您可以將其用作基準a couple of utility classes, TestHandler
and Matcher
,它們是Python測試基礎結構的一部分。 (鏈接是Python的默認分支,但這些類應該可以在其他Python版本中使用)。有關如何使用這些類的信息,請參閱this post。
2
Python 3.4添加到單元測試的確切功能。請參閱TestCase.assertLogs。這個API是很容易使用:
with self.assertLogs('foo', level='INFO') as cm:
logging.getLogger('foo').info('first message')
logging.getLogger('foo.bar').error('second message')
self.assertEqual(cm.output, ['INFO:foo:first message',
'ERROR:foo.bar:second message'])
現在,這個問題被標記python2.7
,但它會顯示在搜索類似的標題python + unittest + logging
。它是很容易回端口功能來Python2.7,所以這裏是:
# logger_test.py
# this file contains the base class containing the newly added method
# assertLogs
import collections
import logging
_LoggingWatcher = collections.namedtuple("_LoggingWatcher",
["records", "output"])
class _BaseTestCaseContext(object):
def __init__(self, test_case):
self.test_case = test_case
def _raiseFailure(self, standardMsg):
msg = self.test_case._formatMessage(self.msg, standardMsg)
raise self.test_case.failureException(msg)
class _CapturingHandler(logging.Handler):
"""
A logging handler capturing all (raw and formatted) logging output.
"""
def __init__(self):
logging.Handler.__init__(self)
self.watcher = _LoggingWatcher([], [])
def flush(self):
pass
def emit(self, record):
self.watcher.records.append(record)
msg = self.format(record)
self.watcher.output.append(msg)
class _AssertLogsContext(_BaseTestCaseContext):
"""A context manager used to implement TestCase.assertLogs()."""
LOGGING_FORMAT = "%(levelname)s:%(name)s:%(message)s"
def __init__(self, test_case, logger_name, level):
_BaseTestCaseContext.__init__(self, test_case)
self.logger_name = logger_name
if level:
self.level = logging._levelNames.get(level, level)
else:
self.level = logging.INFO
self.msg = None
def __enter__(self):
if isinstance(self.logger_name, logging.Logger):
logger = self.logger = self.logger_name
else:
logger = self.logger = logging.getLogger(self.logger_name)
formatter = logging.Formatter(self.LOGGING_FORMAT)
handler = _CapturingHandler()
handler.setFormatter(formatter)
self.watcher = handler.watcher
self.old_handlers = logger.handlers[:]
self.old_level = logger.level
self.old_propagate = logger.propagate
logger.handlers = [handler]
logger.setLevel(self.level)
logger.propagate = False
return handler.watcher
def __exit__(self, exc_type, exc_value, tb):
self.logger.handlers = self.old_handlers
self.logger.propagate = self.old_propagate
self.logger.setLevel(self.old_level)
if exc_type is not None:
# let unexpected exceptions pass through
return False
if len(self.watcher.records) == 0:
self._raiseFailure(
"no logs of level {} or higher triggered on {}"
.format(logging.getLevelName(self.level), self.logger.name))
class LogTestCase(unittest.TestCase):
def assertLogs(self, logger=None, level=None):
"""Fail unless a log message of level *level* or higher is emitted
on *logger_name* or its children. If omitted, *level* defaults to
INFO and *logger* defaults to the root logger.
This method must be used as a context manager, and will yield
a recording object with two attributes: `output` and `records`.
At the end of the context manager, the `output` attribute will
be a list of the matching formatted log messages and the
`records` attribute will be a list of the corresponding LogRecord
objects.
Example::
with self.assertLogs('foo', level='INFO') as cm:
logging.getLogger('foo').info('first message')
logging.getLogger('foo.bar').error('second message')
self.assertEqual(cm.output, ['INFO:foo:first message',
'ERROR:foo.bar:second message'])
"""
return _AssertLogsContext(self, logger, level)
現在在你的單元測試模塊,你可以使用這個類:
#test_my_module
from logger_test import LogTestCase
class TestMyModule(LogTestCase):
def test_some_feature(self):
with self.assertLogs('foo', level='INFO') as cm:
logging.getLogger('foo').info('first message')
logging.getLogger('foo.bar').error('second message')
self.assertEqual(cm.output, ['INFO:foo:first message',
'ERROR:foo.bar:second message'])
相關問題
- 1. 使用Assert的單元測試案例
- 2. 如何將單元測試與assert()
- 3. Python:對象拋出參數警告
- 4. 當從1.5遷移到1.7時出現單元測試警告
- 5. 單元測試課 - 拋出錯誤嗎?
- 6. 單元測試拋出ObjectDisposedException與ObjectContext
- 7. 警告測試
- 8. Python單元測試HTML報告
- 9. Python單元測試以失敗告終
- 10. Angular 2材料在業障測試中拋出警告
- 11. 如何用Python的unittest測試一個警告是否被拋出?
- 12. mkdir()拋出警告消息
- 13. QCoreApplication中的ASSERT失敗:簡單的QT單元測試失敗?
- 14. shell_exec拋出警告php
- 15. 硒RC測試+單元測試拋出一個賽格故障
- 16. ASP.NET MVC 3測試版:TryUpdateModel在單元測試中拋出NullreferenceException
- 17. 在JavaScript中「拋出」警告?
- 18. 單元測試101 - 從我的單元內拋出異常
- 19. Python的單元測試報告通過測試
- 20. Python - 單元測試
- 21. Python單元測試
- 22. Redux警告只出現在測試中
- 23. 單元測試/ Python中
- 24. 如何在Python參數化setUpClass()(2.7)單元測試
- 25. Python單元測試突然退出
- 26. assert vs ==在Python中測試代碼?
- 27. 可以在PHPUnit中測試單元測試的日誌警告嗎?
- 28. HttpContextBaseExtensions.GetOwinContext在嘗試運行時拋出NullReferenceException單元測試
- 29. VS2010中的單元測試3.5項目會產生CS1685警告
- 30. C#單元測試警告而不是失敗