[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

test/unit setup method

ptkwt

6/2/2006 12:36:00 AM

Is there a way to initialize a unit test once and only once? I know that
you can define a #setup method, but it gets run for every test_* method in
the class.

I know I could do:

class TestStuff < Test::Unit::TestCase

def setup
unless @setup
@setup = true
#do stuff here only once
end
#do stuff out here for every test_* method
end

def test_foo
#setup will be called first
#test stuff
end

def test_foo_two
#setup will be called first
#test stuff
end
end


....but I'm wondering if there's a built-in way of doing this sort of
initialization already?

Phil
3 Answers

Andrew McDonagh

6/2/2006 9:22:00 PM

0

Phil Tomson wrote:
> Is there a way to initialize a unit test once and only once? I know that
> you can define a #setup method, but it gets run for every test_* method in
> the class.
>
> I know I could do:
>
> class TestStuff < Test::Unit::TestCase
>
> def setup
> unless @setup
> @setup = true
> #do stuff here only once
> end
> #do stuff out here for every test_* method
> end
>
> def test_foo
> #setup will be called first
> #test stuff
> end
>
> def test_foo_two
> #setup will be called first
> #test stuff
> end
> end
>
>
> ...but I'm wondering if there's a built-in way of doing this sort of
> initialization already?
>
> Phil

Why would you want to initialize only once? from a testing point of
view, you want each test case (method) to be independently runnable.
Creating dependencies between them makes them very fragile to changes.

ptkwt

6/5/2006 7:01:00 PM

0

In article <e5qa5t$qem$2@news.freedom2surf.net>,
Andrew McDonagh <news@andmc.com> wrote:
>Phil Tomson wrote:
>> Is there a way to initialize a unit test once and only once? I know that
>> you can define a #setup method, but it gets run for every test_* method in
>> the class.
>>
>> I know I could do:
>>
>> class TestStuff < Test::Unit::TestCase
>>
>> def setup
>> unless @setup
>> @setup = true
>> #do stuff here only once
>> end
>> #do stuff out here for every test_* method
>> end
>>
>> def test_foo
>> #setup will be called first
>> #test stuff
>> end
>>
>> def test_foo_two
>> #setup will be called first
>> #test stuff
>> end
>> end
>>
>>
>> ...but I'm wondering if there's a built-in way of doing this sort of
>> initialization already?
>>
>> Phil
>
>Why would you want to initialize only once? from a testing point of
>view, you want each test case (method) to be independently runnable.
>Creating dependencies between them makes them very fragile to changes.
>

I need to setup an xmlrpc server and it would be best (I think) to only
set it up once.

Phil

Andrew McDonagh

6/5/2006 7:42:00 PM

0

Phil Tomson wrote:
> In article <e5qa5t$qem$2@news.freedom2surf.net>,
> Andrew McDonagh <news@andmc.com> wrote:
>> Phil Tomson wrote:
>>> Is there a way to initialize a unit test once and only once? I know that
>>> you can define a #setup method, but it gets run for every test_* method in
>>> the class.
>>>
>>> I know I could do:
>>>
>>> class TestStuff < Test::Unit::TestCase
>>>
>>> def setup
>>> unless @setup
>>> @setup = true
>>> #do stuff here only once
>>> end
>>> #do stuff out here for every test_* method
>>> end
>>>
>>> def test_foo
>>> #setup will be called first
>>> #test stuff
>>> end
>>>
>>> def test_foo_two
>>> #setup will be called first
>>> #test stuff
>>> end
>>> end
>>>
>>>
>>> ...but I'm wondering if there's a built-in way of doing this sort of
>>> initialization already?
>>>
>>> Phil
>> Why would you want to initialize only once? from a testing point of
>> view, you want each test case (method) to be independently runnable.
>> Creating dependencies between them makes them very fragile to changes.
>>
>
> I need to setup an xmlrpc server and it would be best (I think) to only
> set it up once.
>
> Phil


Do you need to have a server - or do you need to test your codes
requests and responses to 'a' server?

In other words, fake (or mock) the server for each test, rather than
have a real server.