summaryrefslogtreecommitdiffstats
path: root/Lib/unittest/case.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/unittest/case.py')
-rw-r--r--Lib/unittest/case.py22
1 files changed, 22 insertions, 0 deletions
diff --git a/Lib/unittest/case.py b/Lib/unittest/case.py
index 001b640..36daa61 100644
--- a/Lib/unittest/case.py
+++ b/Lib/unittest/case.py
@@ -332,6 +332,23 @@ class _AssertWarnsContext(_AssertRaisesBaseContext):
self._raiseFailure("{} not triggered".format(exc_name))
+class _AssertNotWarnsContext(_AssertWarnsContext):
+
+ def __exit__(self, exc_type, exc_value, tb):
+ self.warnings_manager.__exit__(exc_type, exc_value, tb)
+ if exc_type is not None:
+ # let unexpected exceptions pass through
+ return
+ try:
+ exc_name = self.expected.__name__
+ except AttributeError:
+ exc_name = str(self.expected)
+ for m in self.warnings:
+ w = m.message
+ if isinstance(w, self.expected):
+ self._raiseFailure(f"{exc_name} triggered")
+
+
class _OrderedChainMap(collections.ChainMap):
def __iter__(self):
seen = set()
@@ -811,6 +828,11 @@ class TestCase(object):
context = _AssertWarnsContext(expected_warning, self)
return context.handle('assertWarns', args, kwargs)
+ def _assertNotWarns(self, expected_warning, *args, **kwargs):
+ """The opposite of assertWarns. Private due to low demand."""
+ context = _AssertNotWarnsContext(expected_warning, self)
+ return context.handle('_assertNotWarns', args, kwargs)
+
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