cpython: 32407a677215 (original) (raw)
Mercurial > cpython
changeset 89980:32407a677215 3.4
backport: #20145: assert[Raises|Warns]Regex now raise TypeError on bad regex. Previously a non-string, non-regex second argument and missing callable argument could cause the test to appear to always pass. Initial patch by Kamilla Holanda. [#20145]
R David Murray rdmurray@bitdance.com | |
---|---|
date | Tue, 25 Mar 2014 15:31:50 -0400 |
parents | aa2a05fe46ae |
children | 8f72f8359987 31e42208eb99 |
files | Lib/unittest/case.py Lib/unittest/test/test_case.py Misc/ACKS Misc/NEWS |
diffstat | 4 files changed, 17 insertions(+), 1 deletions(-)[+] [-] Lib/unittest/case.py 2 Lib/unittest/test/test_case.py 12 Misc/ACKS 1 Misc/NEWS 3 |
line wrap: on
line diff
--- a/Lib/unittest/case.py +++ b/Lib/unittest/case.py @@ -143,7 +143,7 @@ class _AssertRaisesBaseContext(_BaseTest self.obj_name = str(callable_obj) else: self.obj_name = None
if isinstance(expected_regex, (bytes, str)):[](#l1.7)
if expected_regex is not None:[](#l1.8) expected_regex = re.compile(expected_regex)[](#l1.9) self.expected_regex = expected_regex[](#l1.10) self.msg = None[](#l1.11)
--- a/Lib/unittest/test/test_case.py +++ b/Lib/unittest/test/test_case.py @@ -1126,6 +1126,18 @@ test case self.assertRaisesRegex, Exception, 'x', lambda: None)
- def testAssertRaisesRegexInvalidRegex(self):
# Issue 20145.[](#l2.8)
class MyExc(Exception):[](#l2.9)
pass[](#l2.10)
self.assertRaises(TypeError, self.assertRaisesRegex, MyExc, lambda: True)[](#l2.11)
- def testAssertWarnsRegexInvalidRegex(self):
# Issue 20145.[](#l2.14)
class MyWarn(Warning):[](#l2.15)
pass[](#l2.16)
self.assertRaises(TypeError, self.assertWarnsRegex, MyWarn, lambda: True)[](#l2.17)
+ def testAssertRaisesRegexMismatch(self): def Stub(): raise Exception('Unexpected')
--- a/Misc/ACKS +++ b/Misc/ACKS @@ -546,6 +546,7 @@ Stefan Hoffmeister Albert Hofkamp Tomas Hoger Jonathan Hogg +Kamilla Holanda Steve Holden Akintayo Holder Thomas Holenstein
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -24,6 +24,9 @@ Core and Builtins
Library
-------
+- Issue #20145: assertRaisesRegex
and assertWarnsRegex
now raise a