New submission from Chris Jerdonek <chris.jerdo...@gmail.com>: The setUp() and tearDown() methods of unittest.TestCase are of course extremely useful. But sometimes one has set up and tear down functionality that one would like to apply in the form of an existing context manager (and that may be from an outside source).
There is currently no clear or clean way to do this. It would be nice if unittest exposed a way to do this. The closest I have been able to come is overriding TestCase.run() as follows: class MyTest(unittest.TestCase): def run(self, result=None): with my_context_manager() as foo: # Do stuff. super(MyTest, self).run(result) But this is not ideal because the context manager is surrounding more than it should (various test initialization code internal to unittest, etc). Also, ideally the API would let one apply a context manager either before or after setUp() and tearDown(), or both. Here is one idea for an API. unittest.TestCase could expose a setUpContext() context manager that wraps the user-defined setUp() and tearDown(), and also a runTestMethod() method that runs the test method code by itself (i.e. currently the following line of unittest/case.py: `self._executeTestPart(testMethod, outcome, isTest=True)`). To use the API, the user could override a simple method called something like runTest(): def runTest(self): with setUpContext(): self.runTestMethod() The user would, in the override, be free to insert additional context managers before or after setUpContext(), or both. ---------- components: Library (Lib) messages: 165454 nosy: cjerdonek priority: normal severity: normal status: open title: Add to unittest.TestCase support for using context managers type: enhancement _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue15351> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com