[TIP] Unittest Changes
Daniels, Scott
scott.daniels at hp.com
Mon Jul 21 07:49:11 PDT 2008
On Mon, 21 Jul 2008 at 09:54, "Jonathan Lange" <jml at mumak.net> wrote:
> On Mon, Jul 21, 2008 at 7:16 AM, Michael Foord <fuzzyman at voidspace.org.uk> wrote:
> > ...
> > Any other good ideas about ways that unittest could change that don't
> > either grow the API greatly or break backwards compatibility?
>
> The big one is addCleanup (again, implemented & used in pyunit3k, Twisted and bzrlib).
> The idea is that it allows you to dynamically create resources in tests without worry.
> Here's an example of usage:
>
> def makeTemporaryDirectory(self):
> d = tempfile.mkdtemp()
> self.addCleanup(shutil.rmtree, d)
> return d
>
> Here's a rough implementation:
>
> def addCleanup(self, f, *a, **kw):
> self._cleanup_stack.append((f, *a, **kw))
>
> def _runCleanups(self, test_result):
> for f, *a, **kw in self._cleanup_stack:
> try:
> f(*a, **kw)
> except:
> test_result.addError(self, sys.exc_info())
I presume you mean:
> def addCleanup(self, f, *a, **kw):
> self._cleanup_stack.append((f, a, kw))
And I'd suggest popping the stack back, rather than running forwards
> def _runCleanups(self, test_result):
> while self._cleanup_stack:
> f, a, kw = self._cleanup_stack.pop()
> try:
> f(*a, **kw)
> except:
> test_result.addError(self, sys.exc_info())
-Scott
scott.daniels at hp.com
More information about the testing-in-python
mailing list