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

Discussion in 'Ruby' started by Michael Schuerig, Feb 26, 2007.

  1. 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

    --
    Michael Schuerig
    mailto:
    http://www.schuerig.de/michael/
    Michael Schuerig, Feb 26, 2007
    #1
    1. Advertising

  2. On 26/02/07, Michael Schuerig <> 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?
    >
    > Michael
    >
    > --
    > Michael Schuerig
    > mailto:
    > 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
    Farrel Lifson, Feb 26, 2007
    #2
    1. Advertising

  3. Michael Schuerig

    Robert Dober Guest

    On 2/26/07, Michael Schuerig <> 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?
    >
    > Michael
    >
    > --
    > Michael Schuerig
    > mailto:
    > 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
    <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
    --
    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
    Robert Dober, Feb 26, 2007
    #3
  4. On Monday 26 February 2007, Farrel Lifson wrote:
    > On 26/02/07, Michael Schuerig <> 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

    --
    Michael Schuerig
    mailto:
    http://www.schuerig.de/michael/
    Michael Schuerig, Feb 26, 2007
    #4
  5. On Monday 26 February 2007, Robert Dober wrote:
    > On 2/26/07, Michael Schuerig <> 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?


    > 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

    --
    Michael Schuerig
    mailto:
    http://www.schuerig.de/michael/
    Michael Schuerig, Feb 26, 2007
    #5
  6. On Tue, Feb 27, 2007 at 03:02:06AM +0900, Michael Schuerig 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
    Brian Candler, Feb 26, 2007
    #6
  7. Michael Schuerig

    Robert Dober Guest

    On 2/26/07, Michael Schuerig <> wrote:
    > On Monday 26 February 2007, Robert Dober wrote:
    > > On 2/26/07, Michael Schuerig <> 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?

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

    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
    > Michael
    >
    > --
    > Michael Schuerig
    > mailto:
    > http://www.schuerig.de/michael/
    >
    >



    --
    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
    Robert Dober, Feb 26, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joel VanderWerf
    Replies:
    2
    Views:
    101
    Joel VanderWerf
    Dec 18, 2003
  2. Nicholas Van Weerdenburg

    instance variable question in test/unit TestCase

    Nicholas Van Weerdenburg, Jul 14, 2004, in forum: Ruby
    Replies:
    5
    Views:
    342
    Eric Hodel
    Jul 14, 2004
  3. Michael Schuerig
    Replies:
    1
    Views:
    113
    Nathaniel Talbott
    Jul 9, 2005
  4. Scott
    Replies:
    1
    Views:
    119
    Timothy Hunter
    Aug 20, 2005
  5. Peter Hickman

    Per suite unit test setup and teardown

    Peter Hickman, Aug 28, 2008, in forum: Ruby
    Replies:
    7
    Views:
    132
    Peter Hickman
    Sep 10, 2008
Loading...

Share This Page