# Per suite unit test setup and teardown

**URL:** <https://www.ruby-forum.com/t/per-suite-unit-test-setup-and-teardown/147100>\
**Category:** Ruby\
**Created:** [August 28, 2008, 1:43pm UTC](https://www.ruby-forum.com/t/per-suite-unit-test-setup-and-teardown/147100 "2008-08-28T13:43:59Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![tekhne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/90ced4/32.png) [@tekhne](https://www.ruby-forum.com/u/tekhne)\
**Post date:** [August 28, 2008, 1:43pm UTC](https://www.ruby-forum.com/t/per-suite-unit-test-setup-and-teardown/147100/1 "2008-08-28T13:43:59Z")

</div>

I am putting some tests together that need to access an external service  
which I can use setup() and teardown() to control. The problem is that  
these methods are run before and after each test, this slows things down  
quite a bit. Ideally there would be one setup before all the tests are  
run and one teardown at the end. Nunit has this functionality and I’m  
sure I’ve seen it in other frameworks.

Is there a way to get until test to do this or will I have to end up  
putting all my tests in a single test method?

---

<div class="post-metadata">

**Author:** ![tekhne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/90ced4/32.png) [@tekhne](https://www.ruby-forum.com/u/tekhne)\
**Post date:** [August 29, 2008, 4:26pm UTC](https://www.ruby-forum.com/t/per-suite-unit-test-setup-and-teardown/147100/2 "2008-08-29T16:26:53Z")

</div>

Peter H. wrote:

> 

Prior to all test executions… you could put the setup at the outer  
level of the script file.

---

<div class="post-metadata">

**Author:** ![tekhne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/90ced4/32.png) [@tekhne](https://www.ruby-forum.com/u/tekhne)\
**Post date:** [August 29, 2008, 4:56pm UTC](https://www.ruby-forum.com/t/per-suite-unit-test-setup-and-teardown/147100/3 "2008-08-29T16:56:46Z")

</div>

Ron F. wrote:

> > 
> 
> Prior to all test executions… you could put the setup at the outer  
> level of the script file.

Putting the global setup code outside the class does work but there is  
no way to get the global teardown to work. Even putting it in an END  
block (a horrible Perlism) doesn’t work.

Then you come to test suites, which results in all the global startup  
code being run before any of the tests.

I think I will have to look into the code for unit test, how hard can it  
be?

---

<div class="post-metadata">

**Author:** ![tekhne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/90ced4/32.png) [@tekhne](https://www.ruby-forum.com/u/tekhne)\
**Post date:** [September 9, 2008, 12:12pm UTC](https://www.ruby-forum.com/t/per-suite-unit-test-setup-and-teardown/147100/4 "2008-09-09T12:12:47Z")

</div>

2008/8/29 Peter H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Putting the global setup code outside the class does work but there is no  
> way to get the global teardown to work. Even putting it in an END block (a  
> horrible Perlism) doesn’t work.

Peter, you have to double the horror to get what you want:

END {  
END {  
puts “this is run after the tests”  
}  
}

I think I learned this trick from Ara.

Regards,  
Pit

---

<div class="post-metadata">

**Author:** ![tekhne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/90ced4/32.png) [@tekhne](https://www.ruby-forum.com/u/tekhne)\
**Post date:** [September 9, 2008, 8:50pm UTC](https://www.ruby-forum.com/t/per-suite-unit-test-setup-and-teardown/147100/5 "2008-09-09T20:50:34Z")

</div>

2008/9/9 Daniel B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> They were added to test-unit 2.0:

Thank you for the hint, Daniel. I missed the new release.

Regards,  
Pit

---

<div class="post-metadata">

**Author:** ![tekhne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/90ced4/32.png) [@tekhne](https://www.ruby-forum.com/u/tekhne)\
**Post date:** [September 10, 2008, 12:21pm UTC](https://www.ruby-forum.com/t/per-suite-unit-test-setup-and-teardown/147100/6 "2008-09-10T12:21:53Z")

</div>

> Peter, you have to double the horror to get what you want:
> 
> END {  
> END {  
> puts “this is run after the tests”  
> }  
> }
> 
> I think I learned this trick from Ara.

Why is a single END not enough? What is happening here?

RG

---

<div class="post-metadata">

**Author:** ![tekhne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/90ced4/32.png) [@tekhne](https://www.ruby-forum.com/u/tekhne)\
**Post date:** [September 10, 2008, 12:30pm UTC](https://www.ruby-forum.com/t/per-suite-unit-test-setup-and-teardown/147100/7 "2008-09-10T12:30:07Z")

</div>

Daniel B. wrote:

> 

Oh thanks for that. Will get downloading asap.

---

<div class="post-metadata">

**Author:** ![tekhne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/90ced4/32.png) [@tekhne](https://www.ruby-forum.com/u/tekhne)\
**Post date:** [September 9, 2008, 4:21pm UTC](https://www.ruby-forum.com/t/per-suite-unit-test-setup-and-teardown/147100/8 "2008-09-09T16:21:13Z")

</div>

On Aug 28, 5:38 am, Peter H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I am putting some tests together that need to access an external service  
> which I can use setup() and teardown() to control. The problem is that  
> these methods are run before and after each test, this slows things down  
> quite a bit. Ideally there would be one setup before all the tests are  
> run and one teardown at the end. Nunit has this functionality and I’m  
> sure I’ve seen it in other frameworks.
> 
> Is there a way to get until test to do this or will I have to end up  
> putting all my tests in a single test method?

They were added to test-unit 2.0:

[http://rubyforge.org/forum/forum.php?forum\_id=25263](http://rubyforge.org/forum/forum.php?forum_id=25263)

gem install test-unit

Regards,

Dan
