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

M

Michael Schuerig

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
 
F

Farrel Lifson

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

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

Farrel
 
R

Robert Dober

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
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
<init code>
def whatever...
end
def play_it_again_sam
end
def etcetc
end

class X < Test::Unit::TestCase

def testxxx...
DatabaseProxy.whatever

...
end

HTH
Robert
 
M

Michael Schuerig

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
 
M

Michael Schuerig

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
<init code>
def whatever...
end
def play_it_again_sam
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
 
B

Brian Candler

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
 
R

Robert Dober

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top