R. David Murray <[email protected]> added the comment:
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 ;)
----------
nosy: +michael.foord, r.david.murray
versions: +Python 3.4
_______________________________________
Python tracker <[email protected]>
<http://bugs.python.org/issue15351>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com