NAME

Test::Classy::Base

SYNOPSIS

package MyApp::Test::ForSomething;
use Test::Classy::Base;

__PACKAGE__->mk_classdata('model');

sub initialize {
  my $class = shift;

  eval { require 'Some::Model'; };
  $class->skip_the_rest('Some::Model is required') if $@;

  my $model = Some::Model->connect;

  $class->model($model);
}

sub mytest : Test {
  my $class = shift;
  ok $class->model->find('something'), $class->test_name." works";
}

sub finalize {
  my $class = shift;
  $class->model->disconnect if $class->model;
  $class->model(undef);
}

DESCRIPTION

This is a base class for actual tests. See Test::Classy for basic usage.

CLASS METHODS

skip_the_rest

If you called this with a reason why you want to skip (unsupported OS or lack of modules, for example), all the remaining tests in the package will be skipped.

initialize

This is called before the tests runs. You might want to set up database or something like that here. You can store initialized thingy as a class data (via Class::Data::Inheritable), or as a package-wide variable, maybe. Note that you can set up thingy in a test script and pass it as an argument for each of the tests instead.

finalize

This method is (hopefully) called when all the tests in the package are done. You might also want provide END/DESTROY to clean up thingy when the tests should be bailed out.

test_name

returns the name of the test running currently. Handy to write a meaningful test message.

NOTES FOR INHERITING TESTS

You may want to let tests inherit some base class (especially to reuse common initialization/finalization). You can use good old base.pm (or parent.pm) to do this, though you'll need to use Test::More and the likes explicitly as base.pm doesn't export things:

package MyApp::Test::Base;
use Test::Classy::Base;
use MyApp::Model;

__PACKAGE__->mk_classdata('model');

sub initialize {
  my $class = shift;

  $class->model( MyApp::Model->new );
}

package MyApp::Test::Specific;
use base qw( MyApp::Test::Base );
use Test::More;  # you'll need this.

sub test : Test { ok shift->model->does_fine; }

You also can add 'base' option while using your base class. In this case, all the methods will be exported.

package MyApp::Test::Specific;
use MyApp::Test::Base 'base';

sub test : Test { ok shift->model->does_fine; }

When your base class has some common tests to be inherited, and you don't want them to be tested in the base class, add 'ignore' option when you use Test::Classy::Base:

package MyApp::Test::AnotherBase;
use Test::Classy::Base 'ignore';

sub not_for_base : Test { pass 'for children only' };

AUTHOR

Kenichi Ishigaki, <ishigaki@cpan.org>

COPYRIGHT AND LICENSE

Copyright (C) 2008 by Kenichi Ishigaki.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.