[TIP] setUp and tearDown behavior

Olemis Lang olemis at gmail.com
Tue Jan 19 05:53:09 PST 2010


On Mon, Jan 18, 2010 at 9:37 PM, Alfredo Deza <arufuredosan at gmail.com> wrote:
> I am building some tests for a Python installer that copies some files and
> changes some permissions.
>
> Nothing weird there.
>
> However, in the test class I am starting with a setUp that basically calls
> the installer (so the tests can see if everything is where it should be),
> this is followed by a few assertions and finally a tearDown is called where
> everything gets uninstalled.
>
> When running the tests with nosetests, I see that setUp and tearDown are
> called for *every* method in my test class (e.g. installs => runs test
> method => uninstalls ....)
>

That's the way it should be. In XUnit the calls are performed like this

setUp
test_1
tearDown
setUp
test_2
tearDown
setUp
test_3
tearDown
...

> Isn't setUp supposed to be run once at the beginning of the class? or is
> this expected?
>

In order to do this you need to use Shared Fixture Setup testing
pattern. For instance I recently included support for that in `dutest`
(doctest + unittest) to solve problems similar to yours. In this case
I create and setup a Trac environment just once before running all
Trac-related test cases, and tear it down once everything's been done
.

In JUnit>=4 jargon what you'd need is a BeforeClass and AfterClass

> In case this is expected, is there a way to do it just once for all the
> methods?
>

Using standard `unittest` module ? well ... no.

-- 
Regards,

Olemis.

Blog ES: http://simelo-es.blogspot.com/
Blog EN: http://simelo-en.blogspot.com/

Featured article:
Removing PyOOP test modules.  -
http://flioops.hg.sourceforge.net/hgweb/flioops/dutest/rev/acc55ed8a1c0



More information about the testing-in-python mailing list