from __future__ import with_statement from contextlib import * import unittest @contextmanager def some_cm(): raise ValueError('error from some_cm()') @contextmanager def other_cm(): yield class TestContextManager(unittest.TestCase): def test_a(self): with other_cm(): try: with some_cm(): pass except: print 'catch' if __name__=='__main__': unittest.main()