Mercurial > hg
diff tests/test-ctxmanager.py @ 32279:68c43a416585
tests: use context manager form of assertRaises
Support for using unittest.TestCase.assertRaises as a context
manager was added in Python 2.7. This form is more readable,
especially for complex tests.
While I was here, I also restored the use of assertRaisesRegexp,
which was removed in c6921568cd20 for Python 2.6 compatibility.
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Sat, 13 May 2017 11:52:44 -0700 |
parents | 441491aba8c3 |
children |
line wrap: on
line diff
--- a/tests/test-ctxmanager.py Sat May 13 11:42:42 2017 -0700 +++ b/tests/test-ctxmanager.py Sat May 13 11:52:44 2017 -0700 @@ -55,23 +55,21 @@ def test_raise_on_enter(self): trace = [] addtrace = trace.append - def go(): + with self.assertRaises(ctxerror): with util.ctxmanager(ctxmgr('a', addtrace), lambda: raise_on_enter('b', addtrace)) as c: c.enter() addtrace('unreachable') - self.assertRaises(ctxerror, go) self.assertEqual(trace, [('enter', 'a'), ('raise', 'b'), ('exit', 'a')]) def test_raise_on_exit(self): trace = [] addtrace = trace.append - def go(): + with self.assertRaises(ctxerror): with util.ctxmanager(ctxmgr('a', addtrace), lambda: raise_on_exit('b', addtrace)) as c: c.enter() addtrace('running') - self.assertRaises(ctxerror, go) self.assertEqual(trace, [('enter', 'a'), ('enter', 'b'), 'running', ('raise', 'b'), ('exit', 'a')])