diff --git a/django/test/utils.py b/django/test/utils.py
index ed5ab59..b97d98f 100644
a
|
b
|
|
1 | 1 | from __future__ import with_statement |
2 | 2 | |
| 3 | from contextlib import contextmanager |
| 4 | import logging |
3 | 5 | import warnings |
| 6 | |
4 | 7 | from django.conf import settings, UserSettingsHolder |
5 | 8 | from django.core import mail |
6 | 9 | from django.test.signals import template_rendered, setting_changed |
… |
… |
class override_settings(object):
|
221 | 224 | setting_changed.send(sender=settings._wrapped.__class__, |
222 | 225 | setting=key, value=new_value) |
223 | 226 | |
| 227 | |
| 228 | @contextmanager |
| 229 | def capture_logging(logger_name=''): |
| 230 | """ |
| 231 | Temporarily redirect logging to a buffer (list) |
| 232 | """ |
| 233 | logger = logging.getLogger(logger_name) |
| 234 | saved_handlers = logger.handlers[:] |
| 235 | for handler in logger.handlers: |
| 236 | logger.removeHandler(handler) |
| 237 | capture_handler = logging.handlers.MemoryHandler(100) |
| 238 | logger.addHandler(capture_handler) |
| 239 | |
| 240 | try: |
| 241 | yield capture_handler.buffer |
| 242 | finally: |
| 243 | logger.removeHandler(capture_handler) |
| 244 | capture_handler.close() |
| 245 | for handler in saved_handlers: |
| 246 | logger.addHandler(handler) |
diff --git a/docs/topics/testing.txt b/docs/topics/testing.txt
index 829e059..43892e9 100644
a
|
b
|
under MySQL with MyISAM tables)::
|
1837 | 1837 | def test_transaction_behavior(self): |
1838 | 1838 | # ... conditional test code |
1839 | 1839 | |
| 1840 | Capturing logging during tests |
| 1841 | ------------------------------ |
| 1842 | |
| 1843 | .. versionadded:: 1.5 |
| 1844 | |
| 1845 | .. currentmodule:: django.test |
| 1846 | |
| 1847 | .. function:: capture_logging(logger_name='') |
| 1848 | |
| 1849 | Django provides the ``capture_logging`` context manager as a way to allow you |
| 1850 | to capture the logging output during some tests. You can then access the |
| 1851 | logged content in the captured variable, as a list of |
| 1852 | :py:class:`LogRecord <logging.LogRecord>` objects. Here's a usage example:: |
| 1853 | |
| 1854 | from django.test.utils import capture_logging |
| 1855 | |
| 1856 | class MyTests(TestCase): |
| 1857 | def test_containing_logging(self): |
| 1858 | with capture_logging('logger.name') as log: |
| 1859 | # ... Do stuff that output logging content |
| 1860 | # ... Now output is a StringIO instance that contains logging output |
| 1861 | self.assertEqual(log[0].msg, "First logging content") |
| 1862 | |
| 1863 | |
1840 | 1864 | Live test server |
1841 | 1865 | ---------------- |
1842 | 1866 | |
diff --git a/tests/regressiontests/test_utils/tests.py b/tests/regressiontests/test_utils/tests.py
index 7f4208c..9e5c2bc 100644
a
|
b
|
|
1 | 1 | # -*- coding: utf-8 -*- |
2 | 2 | from __future__ import with_statement, absolute_import |
| 3 | import logging |
3 | 4 | |
4 | 5 | from django.forms import EmailField, IntegerField |
5 | 6 | from django.http import HttpResponse |
… |
… |
__test__ = {"API_TEST": r"""
|
562 | 563 | '<foo bbb="2.0" aaa="1.0">Hello</foo><bar ddd="4.0" ccc="3.0"></bar>' |
563 | 564 | |
564 | 565 | """} |
| 566 | |
| 567 | |
| 568 | class CaptureLoggingTests(SimpleTestCase): |
| 569 | def test_capture_logging(self): |
| 570 | from django.test.utils import capture_logging |
| 571 | |
| 572 | logger = logging.getLogger('django.request') |
| 573 | previous_handlers = logger.handlers[:] |
| 574 | with capture_logging('django.request') as log: |
| 575 | logger.error("This is a test log output") |
| 576 | self.assertEqual(log[0].msg, "This is a test log output") |
| 577 | |
| 578 | # Test that original handlers are restored |
| 579 | self.assertEqual(logger.handlers, previous_handlers) |
| 580 | |
| 581 | def test_capture_root_logger(self): |
| 582 | from django.test.utils import capture_logging |
| 583 | |
| 584 | with capture_logging() as log: |
| 585 | logging.warn("This is a test log output") |
| 586 | self.assertEqual((log[0].levelno, log[0].msg), |
| 587 | (logging.WARNING, "This is a test log output")) |