"finally" for unit test

Peter Otten __peter__ at web.de
Fri Mar 23 12:30:53 CET 2007


killkolor wrote:

> I have a unittest framework that tests a single function that in turn
> works with files (takes input and outputs in the same file, no return
> values).
> In the unittest class I assign a member with all the names of my
> testfiles and a testdirectory. The tests call the function (which
> opens and writes to the file) and then opens the file to see if
> everything is in order. The problem now is that after each testrun I
> have to copy "fresh" files into the testdirectory, since of course the
> function already run on all the files and made the changes. So I
> implemented a buffering in the unittest functions: buffer the file,
> call the function, make the test, write the buffered file back. This
> works fine for unittests that do not fail. If a unittest fails though
> the function stops and the writing back is never done. Is there
> something like a finally for unittest functions? 

TestCase.tearDown()

http://docs.python.org/lib/testcase-objects.html#l2h-5002

> Or could I use 
> another approach to buffer and write back my files (for each unittest
> function)?

Rather than restoring the file I would just delete it and use

TestCase.setUp() to make a fresh copy.

Peter



More information about the Python-list mailing list