diff options
author | Pablo Galindo <Pablogsal@gmail.com> | 2020-05-17 00:22:00 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-17 00:22:00 (GMT) |
commit | af8e5f84d909487a66558d086cb1754f49221236 (patch) | |
tree | c0d6c52eb8ac8048cb0098613bc2a47630249479 /Lib/test/test_exceptions.py | |
parent | 837f9e42e3a1ad03b340661afe85e67d2719334f (diff) | |
download | cpython-af8e5f84d909487a66558d086cb1754f49221236.zip cpython-af8e5f84d909487a66558d086cb1754f49221236.tar.gz cpython-af8e5f84d909487a66558d086cb1754f49221236.tar.bz2 |
Use subTest in test_exceptions for better error reporting (GH-20140)
Diffstat (limited to 'Lib/test/test_exceptions.py')
-rw-r--r-- | Lib/test/test_exceptions.py | 55 |
1 files changed, 29 insertions, 26 deletions
diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py index efd77fd..196f31e 100644 --- a/Lib/test/test_exceptions.py +++ b/Lib/test/test_exceptions.py @@ -33,16 +33,17 @@ class BrokenStrException(Exception): class ExceptionTests(unittest.TestCase): def raise_catch(self, exc, excname): - try: - raise exc("spam") - except exc as err: - buf1 = str(err) - try: - raise exc("spam") - except exc as err: - buf2 = str(err) - self.assertEqual(buf1, buf2) - self.assertEqual(exc.__name__, excname) + with self.subTest(exc=exc, excname=excname): + try: + raise exc("spam") + except exc as err: + buf1 = str(err) + try: + raise exc("spam") + except exc as err: + buf2 = str(err) + self.assertEqual(buf1, buf2) + self.assertEqual(exc.__name__, excname) def testRaising(self): self.raise_catch(AttributeError, "AttributeError") @@ -133,13 +134,14 @@ class ExceptionTests(unittest.TestCase): # these code fragments def ckmsg(src, msg): - try: - compile(src, '<fragment>', 'exec') - except SyntaxError as e: - if e.msg != msg: - self.fail("expected %s, got %s" % (msg, e.msg)) - else: - self.fail("failed to get expected SyntaxError") + with self.subTest(src=src, msg=msg): + try: + compile(src, '<fragment>', 'exec') + except SyntaxError as e: + if e.msg != msg: + self.fail("expected %s, got %s" % (msg, e.msg)) + else: + self.fail("failed to get expected SyntaxError") s = '''if 1: try: @@ -179,15 +181,16 @@ class ExceptionTests(unittest.TestCase): ckmsg(s, "inconsistent use of tabs and spaces in indentation", TabError) def check(self, src, lineno, offset, encoding='utf-8'): - with self.assertRaises(SyntaxError) as cm: - compile(src, '<fragment>', 'exec') - self.assertEqual(cm.exception.lineno, lineno) - self.assertEqual(cm.exception.offset, offset) - if cm.exception.text is not None: - if not isinstance(src, str): - src = src.decode(encoding, 'replace') - line = src.split('\n')[lineno-1] - self.assertIn(line, cm.exception.text) + with self.subTest(source=src, lineno=lineno, offset=offset): + with self.assertRaises(SyntaxError) as cm: + compile(src, '<fragment>', 'exec') + self.assertEqual(cm.exception.lineno, lineno) + self.assertEqual(cm.exception.offset, offset) + if cm.exception.text is not None: + if not isinstance(src, str): + src = src.decode(encoding, 'replace') + line = src.split('\n')[lineno-1] + self.assertIn(line, cm.exception.text) def testSyntaxErrorOffset(self): check = self.check |