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

Delta Between Two Patch Sets: Doc/library/unittest.rst

Issue 18937: add unittest assertion for logging
Left Patch Set: Created 6 years, 2 months ago
Right Patch Set: Created 6 years, 2 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:
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | Lib/unittest/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
(no file at all)
1 :mod:`unittest` --- Unit testing framework 1 :mod:`unittest` --- Unit testing framework
2 ========================================== 2 ==========================================
3 3
4 .. module:: unittest 4 .. module:: unittest
5 :synopsis: Unit testing framework for Python. 5 :synopsis: Unit testing framework for Python.
6 .. moduleauthor:: Steve Purcell <stephen_purcell@yahoo.com> 6 .. moduleauthor:: Steve Purcell <stephen_purcell@yahoo.com>
7 .. sectionauthor:: Steve Purcell <stephen_purcell@yahoo.com> 7 .. sectionauthor:: Steve Purcell <stephen_purcell@yahoo.com>
8 .. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org> 8 .. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
9 .. sectionauthor:: Raymond Hettinger <python@rcn.com> 9 .. sectionauthor:: Raymond Hettinger <python@rcn.com>
10 10
(...skipping 1013 matching lines...) Expand 10 before | Expand all | Expand 10 after
1024 or:: 1024 or::
1025 1025
1026 with self.assertWarnsRegex(RuntimeWarning, 'unsafe frobnicating'): 1026 with self.assertWarnsRegex(RuntimeWarning, 'unsafe frobnicating'):
1027 frobnicate('/etc/passwd') 1027 frobnicate('/etc/passwd')
1028 1028
1029 .. versionadded:: 3.2 1029 .. versionadded:: 3.2
1030 1030
1031 .. versionchanged:: 3.3 1031 .. versionchanged:: 3.3
1032 Added the *msg* keyword argument when used as a context manager. 1032 Added the *msg* keyword argument when used as a context manager.
1033 1033
1034 .. method:: assertLogs(logger=None, level=None)
1035
1036 A context manager to test that at least one message is logged on
1037 the *logger* or one of its children, with at least the given
1038 *level*.
1039
1040 If given, *logger* should be a :class:`logging.Logger` object or a
1041 :class:`str` giving the name of a logger. The default is the root
1042 logger, which will catch all messages.
1043
1044 If given, *level* should be either a numeric logging level or
1045 its string equivalent (for example either ``"ERROR"`` or
1046 :attr:`logging.ERROR`). The default is :attr:`logging.INFO`.
1047
1048 The test passes if at least one message emitted inside the ``with``
1049 block matches the *logger* and *level* conditions, otherwise it fails.
1050
1051 The object returned by the context manager is a recording helper
1052 which keeps tracks of the matching log messages. It has two
1053 attributes:
1054
1055 .. attribute:: records
1056
1057 A list of :class:`logging.LogRecord` objects of the matching
1058 log messages.
1059
1060 .. attribute:: output
1061
1062 A list of :class:`str` objects with the formatted output of
1063 matching messages.
1064
1065 Example::
1066
1067 with self.assertLogs('foo', level='INFO') as cm:
1068 logging.getLogger('foo').info('first message')
1069 logging.getLogger('foo.bar').error('second message')
1070 self.assertEqual(cm.output, ['INFO:foo:first message',
1071 'ERROR:foo.bar:second message'])
1072
1073 .. versionadded:: 3.4
1074
1034 1075
1035 There are also other methods used to perform more specific checks, such as: 1076 There are also other methods used to perform more specific checks, such as:
1036 1077
1037 +---------------------------------------+--------------------------------+--- -----------+ 1078 +---------------------------------------+--------------------------------+--- -----------+
1038 | Method | Checks that | Ne w in | 1079 | Method | Checks that | Ne w in |
1039 +=======================================+================================+=== ===========+ 1080 +=======================================+================================+=== ===========+
1040 | :meth:`assertAlmostEqual(a, b) | ``round(a-b, 7) == 0`` | | 1081 | :meth:`assertAlmostEqual(a, b) | ``round(a-b, 7) == 0`` | |
1041 | <TestCase.assertAlmostEqual>` | | | 1082 | <TestCase.assertAlmostEqual>` | | |
1042 +---------------------------------------+--------------------------------+--- -----------+ 1083 +---------------------------------------+--------------------------------+--- -----------+
1043 | :meth:`assertNotAlmostEqual(a, b) | ``round(a-b, 7) != 0`` | | 1084 | :meth:`assertNotAlmostEqual(a, b) | ``round(a-b, 7) != 0`` | |
(...skipping 1110 matching lines...) Expand 10 before | Expand all | Expand 10 after
2154 2195
2155 .. function:: removeHandler(function=None) 2196 .. function:: removeHandler(function=None)
2156 2197
2157 When called without arguments this function removes the control-c handler 2198 When called without arguments this function removes the control-c handler
2158 if it has been installed. This function can also be used as a test decorator 2199 if it has been installed. This function can also be used as a test decorator
2159 to temporarily remove the handler whilst the test is being executed:: 2200 to temporarily remove the handler whilst the test is being executed::
2160 2201
2161 @unittest.removeHandler 2202 @unittest.removeHandler
2162 def test_signal_handling(self): 2203 def test_signal_handling(self):
2163 ... 2204 ...
LEFTRIGHT

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