summaryrefslogtreecommitdiff
path: root/tests/template_tests
diff options
context:
space:
mode:
authorClaude Paroz <claude@2xlibre.net>2018-04-29 11:02:51 +0200
committerTim Graham <timograham@gmail.com>2018-05-07 09:34:02 -0400
commit523e04dfebf622b922579c3daf139368c4e16031 (patch)
treee948836f60a3560190b514b38bcc5bae088763f3 /tests/template_tests
parent607970f31cc07c317f2ebb684c8f3ccc36a95b3e (diff)
Captured logging in tests with self.assertLogs().
Diffstat (limited to 'tests/template_tests')
-rw-r--r--tests/template_tests/test_logging.py53
1 files changed, 19 insertions, 34 deletions
diff --git a/tests/template_tests/test_logging.py b/tests/template_tests/test_logging.py
index db4c9454be..568f5a5f1e 100644
--- a/tests/template_tests/test_logging.py
+++ b/tests/template_tests/test_logging.py
@@ -4,29 +4,7 @@ from django.template import Engine, Variable, VariableDoesNotExist
from django.test import SimpleTestCase
-class TestHandler(logging.Handler):
- def __init__(self):
- super().__init__()
- self.log_record = None
-
- def emit(self, record):
- self.log_record = record
-
-
-class BaseTemplateLoggingTestCase(SimpleTestCase):
- def setUp(self):
- self.test_handler = TestHandler()
- self.logger = logging.getLogger('django.template')
- self.original_level = self.logger.level
- self.logger.addHandler(self.test_handler)
- self.logger.setLevel(self.loglevel)
-
- def tearDown(self):
- self.logger.removeHandler(self.test_handler)
- self.logger.level = self.original_level
-
-
-class VariableResolveLoggingTests(BaseTemplateLoggingTestCase):
+class VariableResolveLoggingTests(SimpleTestCase):
loglevel = logging.DEBUG
def test_log_on_variable_does_not_exist_silent(self):
@@ -52,31 +30,38 @@ class VariableResolveLoggingTests(BaseTemplateLoggingTestCase):
def __getitem__(self, item):
return self.__dict__[item]
- Variable('article').resolve(TestObject())
+ with self.assertLogs('django.template', self.loglevel) as cm:
+ Variable('article').resolve(TestObject())
+ self.assertEqual(len(cm.records), 1)
+ log_record = cm.records[0]
self.assertEqual(
- self.test_handler.log_record.getMessage(),
+ log_record.getMessage(),
"Exception while resolving variable 'article' in template 'template_name'."
)
- self.assertIsNotNone(self.test_handler.log_record.exc_info)
- raised_exception = self.test_handler.log_record.exc_info[1]
+ self.assertIsNotNone(log_record.exc_info)
+ raised_exception = log_record.exc_info[1]
self.assertEqual(str(raised_exception), 'Attribute does not exist.')
def test_log_on_variable_does_not_exist_not_silent(self):
- with self.assertRaises(VariableDoesNotExist):
- Variable('article.author').resolve({'article': {'section': 'News'}})
+ with self.assertLogs('django.template', self.loglevel) as cm:
+ with self.assertRaises(VariableDoesNotExist):
+ Variable('article.author').resolve({'article': {'section': 'News'}})
+ self.assertEqual(len(cm.records), 1)
+ log_record = cm.records[0]
self.assertEqual(
- self.test_handler.log_record.getMessage(),
+ log_record.getMessage(),
"Exception while resolving variable 'author' in template 'unknown'."
)
- self.assertIsNotNone(self.test_handler.log_record.exc_info)
- raised_exception = self.test_handler.log_record.exc_info[1]
+ self.assertIsNotNone(log_record.exc_info)
+ raised_exception = log_record.exc_info[1]
self.assertEqual(
str(raised_exception),
"Failed lookup for key [author] in {'section': 'News'}"
)
def test_no_log_when_variable_exists(self):
- Variable('article.section').resolve({'article': {'section': 'News'}})
- self.assertIsNone(self.test_handler.log_record)
+ with self.assertRaisesMessage(AssertionError, 'no logs'):
+ with self.assertLogs('django.template', self.loglevel):
+ Variable('article.section').resolve({'article': {'section': 'News'}})