GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
Test::Classy::Base(3) User Contributed Perl Documentation Test::Classy::Base(3)

Test::Classy::Base

  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);
  }

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

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';
    }
  }

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.

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.

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.

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

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

dumps the content of arguments with Data::Dump::dump as a diagnostic message.

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' };

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.

Kenichi Ishigaki, <ishigaki@cpan.org>

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.

2014-11-07 perl v5.32.1

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.