[TIP] setUp and tearDown behavior

Red Forks redforks at gmail.com
Mon Jan 18 19:29:51 PST 2010


On Tue, Jan 19, 2010 at 11:14 AM, Alfredo Deza <arufuredosan at gmail.com>wrote:

> Red
>
> Can you try an give me some examples? I think I am not following you
> correctly...
>
>
> On Mon, Jan 18, 2010 at 10:05 PM, Red Forks <redforks at gmail.com> wrote:
>
>>
>>
>> On Tue, Jan 19, 2010 at 10:37 AM, 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 ....)
>>>
>>> Isn't setUp supposed to be run once at the beginning of the class? or is
>>> this expected?
>>>
>>> In case this is expected, is there a way to do it *just once *for all
>>> the methods?
>>>
>>>
>>>
>>> _______________________________________________
>>> testing-in-python mailing list
>>> testing-in-python at lists.idyll.org
>>> http://lists.idyll.org/listinfo/testing-in-python
>>>
>>>
>> Maybe you need module level setup / teapDown func.
>>
>
>
http://somethingaboutorange.com/mrl/projects/nose/0.11.0/writing_tests.html

Method 1: using module level setup / teardown:

def setup():
   # setup codes here

def teardown():
   # teardown codes here

class YourTestClass(TestCase):
    pass

Method 2: class level setup / teardown

class YourTestClass(TestCase):
    def setup_class(self):
       # setup codes

    def teardown_class(self):
       # teardown codes


Note: All this is nosetest extension, i.e. can only run under nosetest.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20100119/81826842/attachment.htm>


More information about the testing-in-python mailing list