# Test::Unit and one-time setup and teardown

**URL:** <https://rubytalk.org/t/test-unit-and-one-time-setup-and-teardown/35721>\
**Category:** ruby-talk\
**Created:** [27 February 2007 20:25 UTC](https://rubytalk.org/t/test-unit-and-one-time-setup-and-teardown/35721 "2007-02-27T20:25:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ara\_Vartanian](https://avatars.discourse-cdn.com/v4/letter/a/a87d85/32.png) [@Ara\_Vartanian](https://rubytalk.org/u/Ara_Vartanian)\
**Post date:** [27 February 2007 20:25 UTC](https://rubytalk.org/t/test-unit-and-one-time-setup-and-teardown/35721/1 "2007-02-27T20:25:27Z")

</div>

Does anyone know of a way of performing one-time setup and teardown per Test::Unit::TestCase?

I have an integration test with an external component where, unfortunately, mocking out a server response is not feasible. So I need to mount a servlet to interact with my test. It would be pointless and slow to do this once for every test method. I know other unit testing frameworks define such callbacks. Does Test::Unit? If not, is there a clever way of getting around it?

I am using Rails's 'test/helper', so I'm not instantiating my own test harness. Obviously, I can setup any needed resources in the class scope of the unit test itself, BUT that still doesn't give me a callback for teardown.

Any ideas?

Thanks for your help.

-- Ara Vartanian

> **···**
>
> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> Yahoo! Music Unlimited  
> Access over 1 million songs.  
> [http://music.yahoo.com/unlimited](http://music.yahoo.com/unlimited)

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [27 February 2007 21:46 UTC](https://rubytalk.org/t/test-unit-and-one-time-setup-and-teardown/35721/2 "2007-02-27T21:46:34Z")

</div>

> Does anyone know of a way of performing one-time setup and teardown per Test::Unit::TestCase?
> 
> I have an integration test with an external component where, unfortunately, mocking out a server response is not feasible. So I need to mount a servlet to interact with my test. It would be pointless and slow to do this once for every test method. I know other unit testing frameworks define such callbacks. Does Test::Unit? If not, is there a clever way of getting around it?
> 
> I am using Rails's 'test/helper', so I'm not instantiating my own test harness. Obviously, I can setup any needed resources in the class scope of the unit test itself, BUT that still doesn't give me a callback for teardown.
> 
> Any ideas?
> 
> Thanks for your help.
> 
> -- Ara Vartanian

Déjà vu ???  
As I pointed out in the previous thread I do my setup in some constant  
accessible way, e.g. in a module or class or on script level.

Let us assume that I have a testobject that supports setup and teardown  
...  
MyTestObj = Testobject.new  
MyTestObj.setup

class Tester \< test::unit::Testcase  
etc

for teardown you have to experiment with at\_exit but I hope, please  
test it though  
that

at\_exit do  
&nbsp;&nbsp;MyTestObj.teardown  
end

is \*not\* executed before the execution of the Testsuit by the  
testrunner, not sure alas.  
You can still write a pseudo testcase for the teardown but I agree  
that this is a pain.

HTH  
Robert

> **···**
>
> On 2/27/07, Ara Vartanian \<ara\_vartanian@yahoo.com\> wrote:
> 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > Yahoo! Music Unlimited  
> > Access over 1 million songs.  
> > [http://music.yahoo.com/unlimited](http://music.yahoo.com/unlimited)
> 
> --  
> We have not succeeded in answering all of our questions.  
> In fact, in some ways, we are more confused than ever.  
> But we feel we are confused on a higher level and about more important things.  
> -Anonymous

---

<div class="post-metadata">

**Author:** ![Eric\_Hodel1](https://avatars.discourse-cdn.com/v4/letter/e/a9adbd/32.png) [@Eric\_Hodel1](https://rubytalk.org/u/Eric_Hodel1)\
**Post date:** [27 February 2007 23:17 UTC](https://rubytalk.org/t/test-unit-and-one-time-setup-and-teardown/35721/3 "2007-02-27T23:17:07Z")

</div>

> Does anyone know of a way of performing one-time setup and teardown per Test::Unit::TestCase?

Do it at class level and store it in a class variable or constant.

> I have an integration test with an external component where, unfortunately, mocking out a server response is not feasible. So I need to mount a servlet to interact with my test. It would be pointless and slow to do this once for every test method. I know other unit testing frameworks define such callbacks. Does Test::Unit? If not, is there a clever way of getting around it?

I don't believe you. I've tested complex interactions with a stub'd Net::HTTP. See rc\_rest/uri\_stub.rb and rc\_rest/net\_http\_stub.rb.

> I am using Rails's 'test/helper', so I'm not instantiating my own test harness. Obviously, I can setup any needed resources in the class scope of the unit test itself, BUT that still doesn't give me a callback for teardown.

at\_exit {  
&nbsp;&nbsp;&nbsp;at\_exit {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# your teardown  
&nbsp;&nbsp;&nbsp;}  
}

> **···**
>
> On Feb 27, 2007, at 12:25, Ara Vartanian wrote:

---

<div class="post-metadata">

**Author:** ![Mat\_Schaffer](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@Mat\_Schaffer](https://rubytalk.org/u/Mat_Schaffer)\
**Post date:** [28 February 2007 03:54 UTC](https://rubytalk.org/t/test-unit-and-one-time-setup-and-teardown/35721/4 "2007-02-28T03:54:52Z")

</div>

Have you tried BEGIN {} and END {}? But perhaps there's something more graceful.  
-Mat

> **···**
>
> On Feb 27, 2007, at 3:25 PM, Ara Vartanian wrote:
> 
> > Does anyone know of a way of performing one-time setup and teardown per Test::Unit::TestCase?
> > 
> > I have an integration test with an external component where, unfortunately, mocking out a server response is not feasible. So I need to mount a servlet to interact with my test. It would be pointless and slow to do this once for every test method. I know other unit testing frameworks define such callbacks. Does Test::Unit? If not, is there a clever way of getting around it?
> > 
> > I am using Rails's 'test/helper', so I'm not instantiating my own test harness. Obviously, I can setup any needed resources in the class scope of the unit test itself, BUT that still doesn't give me a callback for teardown.
> > 
> > Any ideas?

---

<div class="post-metadata">

**Author:** ![Pit](https://avatars.discourse-cdn.com/v4/letter/p/f14d63/32.png) [@Pit](https://rubytalk.org/u/Pit)\
**Post date:** [28 February 2007 08:24 UTC](https://rubytalk.org/t/test-unit-and-one-time-setup-and-teardown/35721/5 "2007-02-28T08:24:00Z")

</div>

Eric Hodel schrieb:

> at\_exit {  
> &nbsp;&nbsp;at\_exit {  
> &nbsp;&nbsp;&nbsp;&nbsp;# your teardown  
> &nbsp;&nbsp;}  
> }

Nice trick, Eric! Thanks for sharing.

Regards,  
Pit
