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 r.david.murray
Recipients chris.jerdonek, michael.foord, r.david.murray
Date 2012-07-14.20:05:55
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1342296356.23.0.510790929915.issue15351@psf.upfronthosting.co.za>
In-reply-to
Content
Well, if you want to invoke the context in setup/teardown for some reason (as opposed to in the test methods themselves), you can do this:

  def setUp(self):
      self.foo = MyContextManager.__enter__()
      self.addCleanup(MyContextManager.__exit__())

Personally I rarely do this (except occasionally the mock equivalent, see below), preferring to call the context manager explicitly in the test method itself, often factored out into a test helper.

I think we as a community are still learning how best to use context managers and what the optimum design of context manager is.  There is some thought that a context manager should always provide a non-context way of getting at the functionality of the enter and exit methods.  For example, the mock context managers have start() and stop() methods.  There has also been a small amount of discussion of making more use of context managers in unittest itself, possibly along the lines you suggest.

I think this may be an area in which we are not yet ready to settle on an API.  Michael may have a different opinion, of course ;)
History
Date User Action Args
2012-07-14 20:05:56r.david.murraysetrecipients: + r.david.murray, michael.foord, chris.jerdonek
2012-07-14 20:05:56r.david.murraysetmessageid: <1342296356.23.0.510790929915.issue15351@psf.upfronthosting.co.za>
2012-07-14 20:05:55r.david.murraylinkissue15351 messages
2012-07-14 20:05:55r.david.murraycreate