Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(267941)

Delta Between Two Patch Sets: Lib/unittest/case.py

Issue 18937: add unittest assertion for logging
Left Patch Set: Created 5 years, 11 months ago
Right Patch Set: Created 5 years, 11 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Doc/library/unittest.rst ('k') | Lib/unittest/test/test_case.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 """Test case implementation""" 1 """Test case implementation"""
2 2
3 import sys 3 import sys
4 import functools 4 import functools
5 import difflib 5 import difflib
6 import logging 6 import logging
7 import pprint 7 import pprint
8 import re 8 import re
9 import warnings 9 import warnings
10 import collections 10 import collections
(...skipping 701 matching lines...) Expand 10 before | Expand all | Expand 10 after
712 This allows you to inspect the warning after the assertion:: 712 This allows you to inspect the warning after the assertion::
713 713
714 with self.assertWarns(SomeWarning) as cm: 714 with self.assertWarns(SomeWarning) as cm:
715 do_something() 715 do_something()
716 the_warning = cm.warning 716 the_warning = cm.warning
717 self.assertEqual(the_warning.some_attribute, 147) 717 self.assertEqual(the_warning.some_attribute, 147)
718 """ 718 """
719 context = _AssertWarnsContext(expected_warning, self, callable_obj) 719 context = _AssertWarnsContext(expected_warning, self, callable_obj)
720 return context.handle('assertWarns', callable_obj, args, kwargs) 720 return context.handle('assertWarns', callable_obj, args, kwargs)
721 721
722 def assertLogs(self, logger_name=None, level=None): 722 def assertLogs(self, logger=None, level=None):
723 """Fail unless a log message of level *level* or higher is emitted 723 """Fail unless a log message of level *level* or higher is emitted
724 on *logger_name* or its children. If omitted, *level* defaults to 724 on *logger_name* or its children. If omitted, *level* defaults to
725 INFO and *logger_name* defaults to the root logger. 725 INFO and *logger* defaults to the root logger.
726 726
727 This method must be used as a context manager, and will yield 727 This method must be used as a context manager, and will yield
728 a recording object with two attributes: `output` and `records`. 728 a recording object with two attributes: `output` and `records`.
729 At the end of the context manager, the `output` attribute will 729 At the end of the context manager, the `output` attribute will
730 be a list of the matching formatted log messages and the 730 be a list of the matching formatted log messages and the
731 `records` attribute will be a list of the corresponding LogRecord 731 `records` attribute will be a list of the corresponding LogRecord
732 objects. 732 objects.
733 733
734 Example:: 734 Example::
735 735
736 with self.assertLogs('foo', level='INFO') as cm: 736 with self.assertLogs('foo', level='INFO') as cm:
737 logging.getLogger('foo').info('first message') 737 logging.getLogger('foo').info('first message')
738 logging.getLogger('foo.bar').error('second message') 738 logging.getLogger('foo.bar').error('second message')
739 self.assertEqual(cm.output, ['INFO:foo:first message', 739 self.assertEqual(cm.output, ['INFO:foo:first message',
740 'ERROR:foo.bar:second message']) 740 'ERROR:foo.bar:second message'])
741 """ 741 """
742 return _AssertLogsContext(self, logger_name, level) 742 return _AssertLogsContext(self, logger, level)
743 743
744 def _getAssertEqualityFunc(self, first, second): 744 def _getAssertEqualityFunc(self, first, second):
745 """Get a detailed comparison function for the types of the two args. 745 """Get a detailed comparison function for the types of the two args.
746 746
747 Returns: A callable accepting (first, second, msg=None) that will 747 Returns: A callable accepting (first, second, msg=None) that will
748 raise a failure exception if first != second with a useful human 748 raise a failure exception if first != second with a useful human
749 readable error message for those types. 749 readable error message for those types.
750 """ 750 """
751 # 751 #
752 # NOTE(gregory.p.smith): I considered isinstance(first, type(second)) 752 # NOTE(gregory.p.smith): I considered isinstance(first, type(second))
(...skipping 629 matching lines...) Expand 10 before | Expand all | Expand 10 after
1382 return "{} {}".format(self.test_case.id(), self._subDescription()) 1382 return "{} {}".format(self.test_case.id(), self._subDescription())
1383 1383
1384 def shortDescription(self): 1384 def shortDescription(self):
1385 """Returns a one-line description of the subtest, or None if no 1385 """Returns a one-line description of the subtest, or None if no
1386 description has been provided. 1386 description has been provided.
1387 """ 1387 """
1388 return self.test_case.shortDescription() 1388 return self.test_case.shortDescription()
1389 1389
1390 def __str__(self): 1390 def __str__(self):
1391 return "{} {}".format(self.test_case, self._subDescription()) 1391 return "{} {}".format(self.test_case, self._subDescription())
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+