In python, is there a good idiom for using context managers in setup/teardown
问题 I am finding that I am using plenty of context managers in Python. However, I have been testing a number of things using them, and I am often needing the following: class MyTestCase(unittest.TestCase): def testFirstThing(self): with GetResource() as resource: u = UnderTest(resource) u.doStuff() self.assertEqual(u.getSomething(), 'a value') def testSecondThing(self): with GetResource() as resource: u = UnderTest(resource) u.doOtherStuff() self.assertEqual(u.getSomething(), 'a value') When this