in reply to How do I use Test::Unit::Setup?
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.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: How do I use Test::Unit::Setup?
by adrianh (Chancellor) on Jul 13, 2004 at 13:34 UTC | |
by mvc (Scribe) on Jul 16, 2004 at 17:56 UTC | |
Re^2: How do I use Test::Unit::Setup?
by DrWhy (Chaplain) on Jul 13, 2004 at 15:57 UTC | |
by adrianh (Chancellor) on Jul 13, 2004 at 18:46 UTC | |
by DrWhy (Chaplain) on Jul 13, 2004 at 22:20 UTC |