...use of Test::Unit::Setup would allow me to do that; call a single fixture set_up() once...

Yep. Except you don't call set_up(), the framework does. Once for each test method run. It is a template method, and if you have an inheritance hierarchy of test classes, you may need to call SUPER::set_up() when implementing this method.

The simplest example I could find is this abstract base class, which is inherited by all model tests in Rui (some framework). It helps with creating a fresh Implementation Under Test before running each test method:

package Rui::Model::tests::ModelTestCase;

use base 'Test::Unit::TestCase';

sub set_up    { shift->makeModel      }
sub tear_down { delete shift->{model} }

# protected utility factory method for subclasses
sub makeModel {
   my ($self, %params) = @_;
   my $modelClass = $self->getModelClass;
   return $self->{model} = $modelClass->new(%params);
}

# template method for subclasses
sub getModelClass { die "abstract method called" }

As you see this is not a test class, but a useful base class. Instead of doing this at the start of each test method that creates models:

sub test_foo {
   my $self = shift;
   my $model = SomeClass->new;
   # arrange, apply, and assert here, all on our IUT: $model
}

You can do this instead:


use base 'Rui::Model::tests::ModelTestCase';

sub getModelClass { 'SomeClass' }

sub test_foo {
   my $self = shift;
   # $self->{model} already has a fresh IUT in it
   # arrange, apply, and assert here, all on our IUT: $model
}

Make sure you look at Test::Class, as it supports most of the features in Test::Unit, but plays better with Perl testing modules, and has a nicer API. It is also a living project compared with Test::Unit. I switched, and am mostly happy.

Of course with the Aspect module, such things become even easier, but this code was just an example showing where set_up()/tear_down() are useful: in creating a fresh fixture. Look in XUL-Node for examples of using Test::Class together with aspects.


In reply to Re: How do I use Test::Unit::Setup? by mvc
in thread How do I use Test::Unit::Setup? by DrWhy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.