homepage

This issue tracker has been migrated to GitHub , and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author chris.jerdonek
Recipients chris.jerdonek
Date 2012年07月14日.15:33:51
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1342280032.27.0.345733068822.issue15351@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2012年07月14日 15:33:52chris.jerdoneksetrecipients: + chris.jerdonek
2012年07月14日 15:33:52chris.jerdoneksetmessageid: <1342280032.27.0.345733068822.issue15351@psf.upfronthosting.co.za>
2012年07月14日 15:33:51chris.jerdoneklinkissue15351 messages
2012年07月14日 15:33:51chris.jerdonekcreate

AltStyle によって変換されたページ (->オリジナル) /