Kenichi Ishigaki > Test-Classy-0.09 > Test::Classy::Base

Download:
Test-Classy-0.09.tar.gz

Dependencies

Annotate this POD

View/Report Bugs
Source  

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_this_class('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->message('works');
  }

  sub half_baked : Tests(2) {
    my $class = shift;

    pass $class->message('this test');

    return $class->abort_this_test('for some reason');

    fail $class->message('this test');
  }

  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_this_class ( skip_the_rest -- deprecated )

If you called this with a reason why you want to skip (unsupported OS or lack of modules, for example), all the tests in the package will be skipped. Note that this is only useful in the initialize phase. You need to use good old 'skip' and 'Skip:' block when you want to skip some of the tests in a test unit.

  sub some_test : Tests(2) {
    my $class = shift;

    pass 'this test passes';

    Skip: {
      eval "require something";

      skip $@, 1 if $@;

      fail 'this may fail sometimes';
    }
  }

skip_this_test, abort_this_test

That said, 'skip' and 'Skip:' block may be a bit cumbersome especially when you just want to skip the rest of a test (as this is a unit test, you usually don't need to continue to test the rest of the unit test when you skip).

With 'skip_this_test' or 'abort_this_test', you can rewrite the above example like this:

  sub some_test : Tests(2) {
    my $class = shift;

    pass 'this test passes';

    eval "require something";

    return $class->abort_this_test($@) if $@;

    fail 'this may fail sometimes';
  }

Note that you need to 'return' actually to abort.

initialize

This is called before the tests run. 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.

message

prepends the last bit of the class name, and the test name currently running if any, to a message.

dump

dumps the content of arguments with Data::Dump::dump as a diagnostic 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_me' (or 'ignore') option when you use Test::Classy::Base:

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

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

CAVEATS ^

Beware if you want to inherit only some of the tests from a base class (to remove or replace others). All the tests with a Test(s) attribute will be counted while calculating the test plan (i.e. both the ones to replace and the ones to be replaced will be counted). The simplest remedy to avoid a plan error is to use no_plan obviously, but you may find it better to split the class into the mandatory one, and the one which may be skipped while initializing.

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.

syntax highlighting: