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

**URL:** <https://www.ruby-forum.com/t/test-unit-and-one-time-setup-and-teardown/87058>\
**Category:** Ruby\
**Created:** [February 27, 2007, 9:26pm UTC](https://www.ruby-forum.com/t/test-unit-and-one-time-setup-and-teardown/87058 "2007-02-27T21:26:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ara-vartanian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b38774/32.png) [@ara-vartanian](https://www.ruby-forum.com/u/ara-vartanian)\
**Post date:** [February 27, 2007, 9:26pm UTC](https://www.ruby-forum.com/t/test-unit-and-one-time-setup-and-teardown/87058/1 "2007-02-27T21:26:33Z")

</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 V.

---

<div class="post-metadata">

**Author:** ![ara-vartanian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b38774/32.png) [@ara-vartanian](https://www.ruby-forum.com/u/ara-vartanian)\
**Post date:** [February 27, 2007, 10:47pm UTC](https://www.ruby-forum.com/t/test-unit-and-one-time-setup-and-teardown/87058/2 "2007-02-27T22:47:41Z")

</div>

On 2/27/07, Ara V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> – Ara V.

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  
etc

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

at\_exit do  
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

---

<div class="post-metadata">

**Author:** ![ara-vartanian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b38774/32.png) [@ara-vartanian](https://www.ruby-forum.com/u/ara-vartanian)\
**Post date:** [February 28, 2007, 4:55am UTC](https://www.ruby-forum.com/t/test-unit-and-one-time-setup-and-teardown/87058/3 "2007-02-28T04:55:23Z")

</div>

On Feb 27, 2007, at 3:25 PM, Ara V. wrote:

> 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?

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

---

<div class="post-metadata">

**Author:** ![ara-vartanian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b38774/32.png) [@ara-vartanian](https://www.ruby-forum.com/u/ara-vartanian)\
**Post date:** [February 28, 2007, 12:17am UTC](https://www.ruby-forum.com/t/test-unit-and-one-time-setup-and-teardown/87058/4 "2007-02-28T00:17:52Z")

</div>

On Feb 27, 2007, at 12:25, Ara V. wrote:

> 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 {  
at\_exit {  
# your teardown  
}  
}

---

<div class="post-metadata">

**Author:** ![ara-vartanian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b38774/32.png) [@ara-vartanian](https://www.ruby-forum.com/u/ara-vartanian)\
**Post date:** [February 28, 2007, 9:25am UTC](https://www.ruby-forum.com/t/test-unit-and-one-time-setup-and-teardown/87058/5 "2007-02-28T09:25:39Z")

</div>

Eric H. schrieb:

> at\_exit {  
> at\_exit {  
> # your teardown  
> }  
> }

Nice trick, Eric! Thanks for sharing.

Regards,  
Pit
