# Test::Unit: setup per TestCase class or per suite

**URL:** <https://www.ruby-forum.com/t/test-setup-per-testcase-class-or-per-suite/86885>\
**Category:** Ruby\
**Created:** [February 26, 2007, 7:02pm UTC](https://www.ruby-forum.com/t/test-setup-per-testcase-class-or-per-suite/86885 "2007-02-26T19:02:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [February 26, 2007, 7:02pm UTC](https://www.ruby-forum.com/t/test-setup-per-testcase-class-or-per-suite/86885/1 "2007-02-26T19:02:36Z")

</div>

For a performance test I need to populate the database with a large  
number of records. The individual test cases only read the database  
contents. It is rather time consuming and, after all, unnecessary to do  
a full setup and teardown for each test case. I’d much prefer a clean  
way to do setup and teardown once for the entire TestCase subclass.  
AFAIR, in JUnit, there is a decorator for just this purpose. Is there  
something similar for Test::Unit?

Michael

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [February 26, 2007, 7:11pm UTC](https://www.ruby-forum.com/t/test-setup-per-testcase-class-or-per-suite/86885/2 "2007-02-26T19:11:37Z")

</div>

On 2/26/07, Michael S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> –  
> Michael S.  
> [mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [Michael Schürig | Sentenced to making sense](http://www.schuerig.de/michael/)

I always use constants for that kind of problems.  
For what you hide behind the constants is of course a matter of taste  
sometimes I do something like the following, but that is just like me.

class \<\< DatabaseProxy = Class.new  
  
def whatever…  
end  
def play\_it\_again\_sam  
end  
def etcetc  
end

class X \< Test::Unit::TestCase

def testxxx…  
DatabaseProxy.whatever

…  
end

HTH  
Robert

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [February 26, 2007, 7:11pm UTC](https://www.ruby-forum.com/t/test-setup-per-testcase-class-or-per-suite/86885/3 "2007-02-26T19:11:37Z")

</div>

On 26/02/07, Michael S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> –  
> Michael S.  
> [mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [Michael Schürig | Sentenced to making sense](http://www.schuerig.de/michael/)

You should be able to put the data you need initialised only once in  
the initialize method. I think.

Farrel

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [February 26, 2007, 8:13pm UTC](https://www.ruby-forum.com/t/test-setup-per-testcase-class-or-per-suite/86885/4 "2007-02-26T20:13:48Z")

</div>

On Monday 26 February 2007, Farrel L. wrote:

> On 26/02/07, Michael S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > For a performance test I need to populate the database with a large  
> > number of records. The individual test cases only read the database  
> > contents. It is rather time consuming and, after all, unnecessary  
> > to do a full setup and teardown for each test case.

> You should be able to put the data you need initialised only once in  
> the initialize method. I think.

I don’t think so. When I have

class MyPerformanceTest \< Test::Unit::TestCase  
def initialize  
…  
end

def test\_something  
…  
end

def test\_something\_else  
…  
end  
…  
end

for each of the individual test methods a fresh instance of  
MyPerformanceTest is created and initialized.

Michael

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [February 26, 2007, 9:14pm UTC](https://www.ruby-forum.com/t/test-setup-per-testcase-class-or-per-suite/86885/5 "2007-02-26T21:14:56Z")

</div>

On Tue, Feb 27, 2007 at 03:02:06AM +0900, Michael S. wrote:

> For a performance test I need to populate the database with a large  
> number of records. The individual test cases only read the database  
> contents. It is rather time consuming and, after all, unnecessary to do  
> a full setup and teardown for each test case. I’d much prefer a clean  
> way to do setup and teardown once for the entire TestCase subclass.

Options I can think of:

1. Use a class variable - or even a global variable - to record when the  
setup has been done, so it only gets done once.

2. Write all your tests which depend on this database as a single test.  
You  
can always call out to other methods if you like.

def test\_everything  
init\_database  
do\_1  
do\_2  
… etc  
end

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [February 26, 2007, 8:21pm UTC](https://www.ruby-forum.com/t/test-setup-per-testcase-class-or-per-suite/86885/6 "2007-02-26T20:21:49Z")

</div>

On Monday 26 February 2007, Robert D. wrote:

> On 2/26/07, Michael S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > For a performance test I need to populate the database with a large  
> > number of records. The individual test cases only read the database  
> > contents. It is rather time consuming and, after all, unnecessary  
> > to do a full setup and teardown for each test case. I’d much prefer  
> > a clean way to do setup and teardown once for the entire TestCase  
> > subclass. AFAIR, in JUnit, there is a decorator for just this  
> > purpose. Is there something similar for Test::Unit?

> ```
> end
> def etcetc
> 
> ```
> 
> end
> 
> class X \< Test::Unit::TestCase
> 
> def testxxx…  
> DatabaseProxy.whatever
> 
> …  
> end

You’ve lost me there. I don’t understand how this is supposed to work,  
in particular, how it achieves my aim of setting up and tearing down  
the database only once per suite.

Are you using the initialization of the constant to set up the database?  
I’m not sure that this approach plays nicely with multiple test suites  
run by rake.

Michael

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [February 26, 2007, 9:41pm UTC](https://www.ruby-forum.com/t/test-setup-per-testcase-class-or-per-suite/86885/7 "2007-02-26T21:41:00Z")

</div>

On 2/26/07, Michael S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > I always use constants for that kind of problems.  
> > def etcetc  
> > You’ve lost me there. I don’t understand how this is supposed to work,  
> > in particular, how it achieves my aim of setting up and tearing down  
> > the database only once per suite.
> 
> Are you using the initialization of the constant to set up the database?  
> I’m not sure that this approach plays nicely with multiple test suites  
> run by rake.

I was trying to impress the girls 😉

X = File.readlines(“/etc/passwd”)  
class Test…

def test  
X is here for you all the time

end

Is this better?

Cheers  
Robert
