root/branches/release-39/t/lib/Test/Class.pm @ 2488

Revision 2488, 54.1 kB (checked in by mpaschal, 18 months ago)

Convert DDL tests to Test::Class in anticipation of failing tests
BugzID: 79949

Line 
1use strict;
2use warnings;
3use 5.006;
4
5package Test::Class;
6
7use Attribute::Handlers;
8use Carp;
9use Class::ISA;
10use Devel::Symdump;
11use Storable qw(dclone);
12use Test::Builder;
13use Test::Class::MethodInfo;
14
15our $VERSION = '0.30';
16
17my $Check_block_has_run;
18{
19    no warnings 'void';
20    CHECK { $Check_block_has_run = 1 };
21}
22
23use constant NO_PLAN    => "no_plan";
24use constant SETUP              => "setup";
25use constant TEST               => "test";
26use constant TEARDOWN   => "teardown";
27use constant STARTUP    => "startup";
28use constant SHUTDOWN   => "shutdown";
29
30
31our     $Current_method = undef;
32sub current_method { $Current_method };
33
34
35my $Builder = Test::Builder->new;
36sub builder { $Builder };
37
38
39my $Tests = {};
40
41
42my %_Test;  # inside-out object field indexed on $self
43
44sub DESTROY {
45    my $self = shift;
46    delete $_Test{ $self };
47};
48
49sub _test_info {
50        my $self = shift;
51        return ref($self) ? $_Test{$self} : $Tests;
52};
53
54sub _method_info {
55        my ($self, $class, $method) = @_;
56        return( _test_info($self)->{$class}->{$method} );
57};
58
59sub _methods_of_class {
60        my ( $self, $class ) = @_;
61    my $test_info = _test_info($self) 
62        or die "Test::Class internals seem confused. Did you override "
63            . "new() in a sub-class or via multiple inheritence?\n";
64        return values %{ $test_info->{$class} };
65};
66
67sub _parse_attribute_args {
68    my $args = shift || '';
69        my $num_tests;
70        my $type;
71        $args =~ s/\s+//sg;
72        foreach my $arg (split /=>/, $args) {
73                if (Test::Class::MethodInfo->is_num_tests($arg)) {
74                        $num_tests = $arg;
75                } elsif (Test::Class::MethodInfo->is_method_type($arg)) {
76                        $type = $arg;
77                } else {
78                        die 'bad attribute args';
79                };
80        };
81        return( $type, $num_tests );
82};
83
84sub _is_public_method {
85    my ($class, $name) = @_;
86    foreach my $parent_class ( Class::ISA::super_path( $class ) ) {
87        return unless $parent_class->can( $name );
88        return if _method_info( $class, $parent_class, $name );
89    }
90    return 1;
91}
92
93sub Test : ATTR(CODE,RAWDATA) {
94        my ($class, $symbol, $code_ref, $attr, $args) = @_;
95        if ($symbol eq "ANON") {
96                warn "cannot test anonymous subs - you probably loaded a Test::Class too late (after the CHECK block was run). See 'A NOTE ON LOADING TEST CLASSES' in perldoc Test::Class for more details\n";
97        } else {
98        my $name = *{$symbol}{NAME};
99        warn "overriding public method $name with a test method in $class\n"
100                if _is_public_method( $class, $name );
101        eval { 
102            my ($type, $num_tests) = _parse_attribute_args($args);       
103            $Tests->{$class}->{$name} = Test::Class::MethodInfo->new(
104                name => $name, 
105                num_tests => $num_tests,
106                type => $type,
107            ); 
108        } || warn "bad test definition '$args' in $class->$name\n";     
109    };
110};
111
112sub Tests : ATTR(CODE,RAWDATA) {
113        my ($class, $symbol, $code_ref, $attr, $args) = @_;
114    $args ||= 'no_plan';
115    Test( $class, $symbol, $code_ref, $attr, $args );
116};
117
118sub _class_of {
119    my $self = shift;
120    return ref $self ? ref $self : $self;
121}
122
123sub new {
124        my $proto = shift;
125        my $class = _class_of( $proto );
126        $proto = {} unless ref($proto);
127        my $self = bless {%$proto, @_}, $class;
128        $_Test{$self} = dclone($Tests);
129        return($self);
130};
131
132sub _get_methods {
133        my ( $self, @types ) = @_;
134        my $test_class = _class_of( $self );
135       
136        my $test_method_regexp = $ENV{ TEST_METHOD } || '.*';
137    my $method_regexp = eval { qr/\A$test_method_regexp\z/ };
138    die "TEST_METHOD ($test_method_regexp) is not a valid regexp: $@" if $@;
139       
140        my %methods = ();
141        foreach my $class ( Class::ISA::self_and_super_path( $test_class ) ) {
142                foreach my $info ( _methods_of_class( $self, $class ) ) {
143                    my $name = $info->name;
144                        foreach my $type ( @types ) {
145                            if ( $info->is_type( $type ) ) {
146                                $methods{ $name } = 1 
147                                    unless $type eq TEST && $name !~ $method_regexp;
148                }
149                        };
150                };
151        };
152
153    return sort keys %methods;
154};
155
156sub _num_expected_tests {
157        my $self = shift;
158        if (my $reason = $self->SKIP_CLASS ) {
159           return $reason eq "1" ? 0 : 1;
160    };
161        my @startup_shutdown_methods = 
162                        _get_methods($self, STARTUP, SHUTDOWN);
163        my $num_startup_shutdown_methods = 
164                        _total_num_tests($self, @startup_shutdown_methods);
165        return(NO_PLAN) if $num_startup_shutdown_methods eq NO_PLAN;
166        my @fixture_methods = _get_methods($self, SETUP, TEARDOWN);
167        my $num_fixture_tests = _total_num_tests($self, @fixture_methods);
168        return(NO_PLAN) if $num_fixture_tests eq NO_PLAN;
169        my @test_methods = _get_methods($self, TEST);
170        my $num_tests = _total_num_tests($self, @test_methods);
171        return(NO_PLAN) if $num_tests eq NO_PLAN;
172        return($num_startup_shutdown_methods + $num_tests + @test_methods * $num_fixture_tests);
173};
174
175sub expected_tests {
176        my $total = 0;
177        foreach my $test (@_) {
178                if ( _isa_class( __PACKAGE__, $test ) ) {
179                        my $n = _num_expected_tests($test);
180                        return NO_PLAN if $n eq NO_PLAN;
181                        $total += $n;
182                } elsif ( defined $test && $test =~ m/^\d+$/ ) {
183                        $total += $test;
184                } else {
185                        $test = 'undef' unless defined $test;
186                        croak "$test is not a Test::Class or an integer";
187                };
188        };
189        return $total;
190};
191
192sub _total_num_tests {
193        my ($self, @methods) = @_;
194        my $class = _class_of( $self );
195        my $total_num_tests = 0;
196        foreach my $method (@methods) {
197                foreach my $class (Class::ISA::self_and_super_path($class)) {
198                        my $info = _method_info($self, $class, $method);
199                        next unless $info;
200                        my $num_tests = $info->num_tests;
201                        return(NO_PLAN) if ($num_tests eq NO_PLAN);
202                        $total_num_tests += $num_tests;
203                        last unless $num_tests =~ m/^\+/
204                };
205        };
206        return($total_num_tests);
207};
208
209sub _has_no_tests {
210    my ( $self, $method ) = @_;
211    return _total_num_tests( $self, $method ) eq '0';
212}
213
214sub _all_ok_from {
215        my ($self, $start_test) = @_;
216        my $current_test = $Builder->current_test;
217        return(1) if $start_test == $current_test;
218        my @results = ($Builder->summary)[$start_test .. $current_test-1];
219        foreach my $result (@results) { return(0) unless $result };
220        return(1);
221};
222
223sub _exception_failure {
224        my ($self, $method, $exception, $tests) = @_;
225        local $Test::Builder::Level = 3;
226        my $message = $method;
227        $message .= " (for test method '$Current_method')"
228                        if defined $Current_method && $method ne $Current_method;
229        _show_header($self, @$tests);
230        $Builder->ok(0, "$message died ($exception)");
231};
232
233sub _run_method {
234        my ($self, $method, $tests) = @_;
235        my $num_start = $Builder->current_test;
236    my $skip_reason;
237    my $original_ok = \&Test::Builder::ok;
238    no warnings;
239    local *Test::Builder::ok = sub {
240        my ($builder, $test, $description) = @_;
241        local $Test::Builder::Level = $Test::Builder::Level+1;
242        unless ( defined($description) ) {
243            $description = $self->current_method;
244            $description =~ tr/_/ /;
245        };
246        my $is_ok = $original_ok->($builder, $test, $description);
247        unless ( $is_ok ) {
248            my $class = ref $self;
249            $Builder->diag( "  (in $class->$method)" );
250        };
251        return $is_ok;
252    };
253    $skip_reason = eval {$self->$method};
254    $skip_reason = $method unless $skip_reason;
255        my $exception = $@;
256        chomp($exception) if $exception;
257        my $num_done = $Builder->current_test - $num_start;
258        my $num_expected = _total_num_tests($self, $method);
259        $num_expected = $num_done if $num_expected eq NO_PLAN;
260        if ($num_done == $num_expected) {
261                _exception_failure($self, $method, $exception, $tests) 
262                                unless $exception eq '';
263        } elsif ($num_done > $num_expected) {
264                $Builder->diag("expected $num_expected test(s) in $method, $num_done completed\n");
265        } else {
266                until (($Builder->current_test - $num_start) >= $num_expected) {
267                        if ($exception ne '') {
268                                _exception_failure($self, $method, $exception, $tests);
269                                $skip_reason = "$method died";
270                                $exception = '';
271                        } else {
272                                $Builder->skip( $skip_reason );
273                        };
274                };
275        };
276        return(_all_ok_from($self, $num_start));
277};
278
279sub _show_header {
280        my ($self, @tests) = @_;
281        return if $Builder->has_plan;
282        my $num_tests = Test::Class->expected_tests(@tests);
283        if ($num_tests eq NO_PLAN) {
284                $Builder->no_plan;
285        } else {
286                $Builder->expected_tests($num_tests);
287        };
288};
289
290my %SKIP_THIS_CLASS = ();
291
292sub SKIP_CLASS {
293        my $class = shift;
294        $SKIP_THIS_CLASS{ $class } = shift if @_;
295        return $SKIP_THIS_CLASS{ $class };
296};
297
298sub _isa_class {
299    my ( $class, $object_or_class ) = @_;
300    return unless defined $object_or_class;
301    return if $object_or_class eq 'Contextual::Return::Value';
302    return eval { 
303        $object_or_class->isa( $class ) and $object_or_class->can( 'runtests' )
304    };
305}
306
307sub _test_classes {
308        my $class = shift;
309        return grep { _isa_class( $class, $_ ) } Devel::Symdump->rnew->packages;
310};
311
312sub runtests {
313    die "Test::Class was loaded too late (after the CHECK block was run). See 'A NOTE ON LOADING TEST CLASSES' in perldoc Test::Class for more details\n"
314        unless $Check_block_has_run;
315        my @tests = @_;
316        if (@tests == 1 && !ref($tests[0])) {
317                my $base_class = shift @tests;
318                @tests = _test_classes( $base_class );
319        };
320        my $all_passed = 1;
321        TEST_OBJECT: foreach my $t (@tests) {
322                # SHOULD ALSO ALLOW NO_PLAN
323                next if $t =~ m/^\d+$/;
324                croak "$t is not Test::Class or integer"
325                    unless _isa_class( __PACKAGE__, $t );
326        if (my $reason = $t->SKIP_CLASS) {
327            _show_header($t, @tests);
328            $Builder->skip( $reason ) unless $reason eq "1";
329        } else {
330            $t = $t->new unless ref($t);
331            foreach my $method (_get_methods($t, STARTUP)) {
332                _show_header($t, @tests) unless _has_no_tests($t, $method);
333                my $method_passed = _run_method($t, $method, \@tests);
334                $all_passed = 0 unless $method_passed;
335                next TEST_OBJECT unless $method_passed;
336            };
337            my $class = ref($t);
338            my @setup    = _get_methods($t, SETUP);
339            my @teardown = _get_methods($t, TEARDOWN);
340            foreach my $test (_get_methods($t, TEST)) { 
341                local $Current_method = $test;
342                $Builder->diag("\n$class->$test") if $ENV{TEST_VERBOSE};
343                foreach my $method (@setup, $test, @teardown) {
344                    _show_header($t, @tests) unless _has_no_tests($t, $method);
345                    $all_passed = 0 unless _run_method($t, $method, \@tests);
346                };
347            };
348            foreach my $method (_get_methods($t, SHUTDOWN)) {
349                _show_header($t, @tests) unless _has_no_tests($t, $method);
350                $all_passed = 0 unless _run_method($t, $method, \@tests);
351            }
352        }
353        }
354        return($all_passed);
355};
356
357sub _find_calling_test_class {
358        my $level = 0;
359        while (my $class = caller(++$level)) {
360                next if $class eq __PACKAGE__;
361                return $class if _isa_class( __PACKAGE__, $class );
362        }; 
363        return(undef);
364};
365
366sub num_method_tests {
367        my ($self, $method, $n) = @_;
368        my $class = _find_calling_test_class( $self )
369            or croak "not called in a Test::Class";
370        my $info = _method_info($self, $class, $method)
371            or croak "$method is not a test method of class $class";
372        $info->num_tests($n) if defined($n);
373        return( $info->num_tests );
374};
375
376sub num_tests {
377    my $self = shift;
378        croak "num_tests need to be called within a test method"
379                        unless defined $Current_method;
380        return( $self->num_method_tests( $Current_method, @_ ) );
381};
382
383sub BAILOUT {
384        my ($self, $reason) = @_;
385        $Builder->BAILOUT($reason);
386};
387
388sub _last_test_if_exiting_immediately {
389    $Builder->expected_tests || $Builder->current_test+1
390};
391
392sub FAIL_ALL {
393        my ($self, $reason) = @_;
394        my $last_test = _last_test_if_exiting_immediately();
395        $Builder->expected_tests( $last_test ) unless $Builder->has_plan;
396        $Builder->ok(0, $reason) until $Builder->current_test >= $last_test;
397        my $num_failed = grep( !$_, $Builder->summary );
398        exit( $num_failed < 254 ? $num_failed : 254 );
399};
400
401sub SKIP_ALL { 
402        my ($self, $reason) = @_;
403        $Builder->skip_all( $reason ) unless $Builder->has_plan;
404        my $last_test = _last_test_if_exiting_immediately();
405        $Builder->skip( $reason ) 
406            until $Builder->current_test >= $last_test;
407        exit(0);
408}
409
4101;
411
412__END__
413
414=head1 NAME
415
416Test::Class - Easily create test classes in an xUnit/JUnit style
417
418=head1 SYNOPSIS
419
420  package Example::Test;
421  use base qw(Test::Class);
422  use Test::More;
423
424  # setup methods are run before every test method.
425  sub make_fixture : Test(setup) {
426      my $array = [1, 2];
427      shift->{test_array} = $array;
428  };
429
430  # a test method that runs 1 test
431  sub test_push : Test {
432      my $array = shift->{test_array};
433      push @$array, 3;
434      is_deeply($array, [1, 2, 3], 'push worked');
435  };
436
437  # a test method that runs 4 tests
438  sub test_pop : Test(4) {
439      my $array = shift->{test_array};
440      is(pop @$array, 2, 'pop = 2');
441      is(pop @$array, 1, 'pop = 1');
442      is_deeply($array, [], 'array empty');
443      is(pop @$array, undef, 'pop = undef');
444  };
445
446  # teardown methods are run after every test method.
447  sub teardown : Test(teardown) {
448      my $array = shift->{test_array};
449      diag("array = (@$array) after test(s)");
450  };
451 
452later in a nearby .t file
453
454  #! /usr/bin/perl
455  use Example::Test;
456 
457  # run all the test methods in Example::Test
458  Test::Class->runtests;
459
460Outputs:
461
462  1..5
463  ok 1 - pop = 2
464  ok 2 - pop = 1
465  ok 3 - array empty
466  ok 4 - pop = undef
467  # array = () after test(s)
468  ok 5 - push worked
469  # array = (1 2 3) after test(s)
470
471
472=head1 DESCRIPTION
473
474Test::Class provides a simple way of creating classes and objects to test your code in an xUnit style.
475
476Built using L<Test::Builder> it is designing to work with other Test::Builder based modules (L<Test::More>, L<Test::Differences>, L<Test::Exception>, etc.) 
477
478I<Note:> This module will make more sense if you are already familiar with the "standard" mechanisms for testing perl code. Those unfamiliar  with L<Test::Harness>, L<Test::Simple>, L<Test::More> and friends should go take a look at them now. L<Test::Tutorial> is a good starting point.
479
480
481=head1 INTRODUCTION
482
483=head2 A brief history lesson
484
485In 1994 Kent Beck wrote a testing framework for Smalltalk called SUnit. It was popular. You can read a copy of his original paper at L<http://www.xprogramming.com/testfram.htm>.
486
487Later Kent Beck and Erich Gamma created JUnit for testing Java L<http://www.junit.org/>. It was popular too.
488
489Now there are xUnit frameworks for every language from Ada to XSLT. You can find a list at L<http://www.xprogramming.com/software.htm>.
490
491While xUnit frameworks are traditionally associated with unit testing they are also useful in the creation of functional/acceptance tests.
492
493Test::Class is (yet another) implementation of xUnit style testing in perl.
494
495
496=head2 Why you should use Test::Class
497
498Test::Class attempts to provide simple xUnit testing that integrates simply with the standard perl *.t style of testing. In particular:
499
500=over 4
501
502=item *
503
504All the advantages of xUnit testing. You can easily create test fixtures and isolate tests. It provides a framework that should be familiar to people who have used other xUnit style test systems.
505
506
507=item *
508
509It is built with L<Test::Builder> and should co-exist happily with all other Test::Builder based modules. This makes using test classes in *.t scripts, and refactoring normal tests into test classes, much simpler because:
510
511=over 4
512
513=item *
514
515You do not have to learn a new set of new test APIs and can continue using ok(), like(), etc. from L<Test::More> and friends.
516
517=item *
518
519Skipping tests and todo tests are supported.
520
521=item *
522
523You can have normal tests and Test::Class classes co-existing in the same *.t script. You don't have to re-write an entire script, but can use test classes as and when it proves useful.
524
525=back
526
527=item *
528
529You can easily package your tests as classes/modules, rather than *.t scripts. This simplifies reuse, documentation and distribution, encourages refactoring, and allows tests to be extended by inheritance.
530
531=item *
532
533You can have multiple setup/teardown methods. For example have one teardown method to clean up resources and another to check that class invariants still hold.
534
535=item *
536
537It can make running tests faster. Once you have refactored your *.t scripts into classes they can be easily run from a single script. This gains you the (often considerable) start up time that each separate *.t script takes.
538
539=back
540
541
542=head2 Why you should I<not> use Test::Class
543
544=over 4
545
546=item *
547
548If your *.t scripts are working fine then don't bother with Test::Class. For simple test suites it is almost certainly overkill. Don't start thinking about using Test::Class until issues like duplicate code in your test scripts start to annoy.
549
550=item *
551
552If you are distributing your code it is yet another module that the user has to have to run your tests (unless you distribute it with your test suite of course).
553
554=item *
555
556If you are used to the TestCase/Suite/Runner class structure used by JUnit and similar testing frameworks you may find Test::Unit more familiar (but try reading L</"HELP FOR CONFUSED JUNIT USERS"> before you give up).
557
558=back
559
560
561=head1 TEST CLASSES
562
563A test class is just a class that inherits from Test::Class. Defining a test class is as simple as doing:
564
565  package Example::Test;
566  use base qw(Test::Class);
567
568Since Test::Class does not provide its own test functions, but uses those provided by L<Test::More> and friends, you will nearly always also want to have:
569
570  use Test::More;
571
572to import the test functions into your test class.
573
574=head1 METHOD TYPES
575
576There are three different types of method you can define using Test::Class.
577
578=head2 1) Test methods
579
580You define test methods using the L<Test|/"Test"> attribute. For example:
581
582  package Example::Test;
583  use base qw(Test::Class);
584  use Test::More;
585
586  sub subtraction : Test {
587      is( 2-1, 1, 'subtraction works );
588  };
589
590This declares the C<subtraction> method as a test method that runs one test.
591
592If your test method runs more than one test, you should put the number of tests in brackets like this:
593
594  sub addition : Test(2) {
595      is(10 + 20, 30, 'addition works');
596      is(20 + 10, 30, '  both ways');
597  };
598
599If you don't know the number of tests at compile time you can use C<no_plan> like this.
600
601  sub check_class : Test(no_plan) {
602      my $objects = shift->{objects};
603      isa_ok($_, "Object") foreach @$objects;
604  };
605 
606or use the :Tests attribute, which acts just like C<:Test> but defaults to C<no_plan> if no number is given:
607
608  sub check_class : Tests {
609      my $objects = shift->{objects};
610      isa_ok($_, "Object") foreach @$objects;
611  };
612
613
614=head2 2) Setup and teardown methods
615
616Setup and teardown methods are run before and after every test. For example:
617
618  sub before : Test(setup)    { diag("running before test") };
619  sub after  : Test(teardown) { diag("running after test") };
620
621You can use setup and teardown methods to create common objects used by all of your test methods (a test I<fixture>) and store them in your Test::Class object, treating it as a hash. For example:
622
623  sub pig : Test(setup) {
624      my $self = shift;
625      $self->{test_pig} = Pig->new;
626  };
627
628  sub born_hungry : Test {
629      my $pig = shift->{test_pig};
630      is($pig->hungry, 'pigs are born hungry');
631  };
632
633  sub eats : Test(3) {
634      my $pig = shift->{test_pig};
635      ok(  $pig->feed,   'pig fed okay');
636      ok(! $pig->hungry, 'fed pig not hungry');
637      ok(! $pig->feed,   'cannot feed full pig');
638  };
639
640You can also declare setup and teardown methods as running tests. For example you could check that the test pig survives each test method by doing:
641
642  sub pig_alive : Test(teardown => 1) {
643      my $pig = shift->{test_pig};
644      ok($pig->alive, 'pig survived tests' );
645  };
646
647
648=head2 3) Startup and shutdown methods
649
650Startup and shutdown methods are like setup and teardown methods for the whole test class. All the startup methods are run once when you start running a test class. All the shutdown methods are run once just before a test class stops running.
651
652You can use these to create and destroy expensive objects that you don't want to have to create and destroy for every test - a database connection for example:
653
654  sub db_connect : Test(startup) {
655      shift->{dbi} = DBI->connect;
656  };
657 
658  sub db_disconnect : Test(shutdown) {
659      shift->{dbi}->disconnect;
660  };
661
662Just like setup and teardown methods you can pass an optional number of tests to startup and shutdown methods. For example:
663
664  sub example : Test(startup => 1) {
665      ok(1, 'a startup method with one test');
666  };
667 
668If a startup method has a failing test or throws an exception then all other tests for the current test object are ignored.
669
670=head1 RUNNING TESTS
671
672You run test methods with L<runtests()|"runtests">. Doing:
673
674  Test::Class->runtests
675
676runs all of the test methods in every loaded test class. This allows you to easily load multiple test classes in a *.t file and run them all.
677
678  #! /usr/bin/perl
679 
680  # load all the test classes I want to run
681  use Foo::Test;
682  use Foo::Bar::Test;
683  use Foo::Fribble::Test;
684  use Foo::Ni::Test;
685 
686  # and run them all
687  Test::Class->runtests;
688 
689You can use L<Test::Class::Load> to automatically load all the test classes in a given set of directories.
690
691If you need finer control you can create individual test objects with L<new()|"new">. For example to just run the tests in the test class C<Foo::Bar::Test> you can do:
692
693  Example::Test->new->runtests
694
695You can also pass L<runtests()|/"runtests"> a list of test objects to run. For example:
696
697  my $o1 = Example::Test->new;
698  my $o2 = Another::Test->new;
699  # runs all the tests in $o1 and $o2
700  $o1->runtests($o2);
701
702Since, by definition, the base Test::Class has no tests you could also have written:
703
704  my $o1 = Example::Test->new;
705  my $o2 = Another::Test->new;
706  Test::Class->runtests($o1, $o2);
707
708If you pass L<runtests()|/"runtests"> class names it will automatically create test objects for you, so the above can be written more compactly as:
709
710  Test::Class->runtests(qw( Example::Test Another::Test ))
711
712In all of the above examples L<runtests()|/"runtests"> will look at the number of tests both test classes run and output an appropriate test header for L<Test::Harness> automatically.
713
714What happens if you run test classes and normal tests in the same script? For example:
715
716  Test::Class->runtests;
717  ok(Example->new->foo, 'a test not in the test class');
718  ok(Example->new->bar, 'ditto');
719
720L<Test::Harness> will complain that it saw more tests than it expected since the test header output by L<runtests()|/"runtests"> will not include the two normal tests.
721
722To overcome this problem you can pass an integer value to L<runtests()|/"runtests">. This is added to the total number of tests in the test header. So the problematic example can be rewritten as follows:
723
724  Test::Class->runtests(+2);
725  ok(Example->new->foo, 'a test not in the test class');
726  ok(Example->new->bar, 'ditto');
727
728If you prefer to write your test plan explicitly you can use L<expected_tests()|/"expected_tests"> to find out the number of tests a class/object is expected to run.
729
730Since L<runtests()|/"runtests"> will not output a test plan if one has already been set the previous example can be written as:
731
732  plan tests => Test::Class->expected_tests(+2);
733  Test::Class->runtests;
734  ok(Example->new->foo, 'a test not in the test class');
735  ok(Example->new->bar, 'ditto');
736
737I<Remember:> Test objects are just normal perl objects. Test classes are just normal perl classes. Setup, test and teardown methods are just normal methods. You are completely free to have other methods in your class that are called from your test methods, or have object specific C<new> and C<DESTROY> methods.
738
739In particular you can override the new() method to pass parameters to your test object, or re-define the number of tests a method will run. See L<num_method_tests()|/"num_method_tests"> for an example.
740
741
742=head1 TEST DESCRIPTIONS
743
744The test functions you import from L<Test::More> and other L<Test::Builder> based modules usually take an optional third argument that specifies the test description, for example:
745
746  is $something, $something_else, 'a description of my test';
747   
748If you do not supply a test description, and the test function does not supply its own default, then Test::Class will use the name of the currently running test method, replacing all "_" characters with spaces so:
749
750  sub one_plus_one_is_two : Test {
751      is 1+1, 2;
752  }
753 
754will result in:
755
756  ok 1 - one plus one is two
757
758
759=head1 RUNNING ORDER OF METHODS
760
761Methods of each type are run in the following order:
762
763=over 4
764
765=item 1.
766
767All of the startup methods in alphabetical order
768
769=item 2.
770
771For each test method, in alphabetical order:
772
773=over 2
774
775=item *
776
777All of the setup methods in alphabetical order
778
779=item *
780
781The test method.
782
783=item *
784
785All of the teardown methods in alphabetical order
786
787=back
788
789=item 3.
790
791All of the shutdown methods in alphabetical order.
792
793=back
794
795Most of the time you should not care what order tests are run in, but it can occasionally be useful to force some test methods to be run early. For example:
796
797  sub _check_new {
798      my $self = shift;
799      isa_ok(Object->new, "Object") or $self->BAILOUT('new fails!');
800  };
801
802The leading C<_> will force the above method to run first - allowing the entire suite to be aborted before any other test methods run.
803
804
805=head1 HANDLING EXCEPTIONS
806
807If a startup, setup, test, teardown or shutdown method dies then L<runtests()|/"runtests"> will catch the exception and fail any remaining test. For example:
808
809  sub test_object : Test(2) {
810      my $object = Object->new;
811      isa_ok( $object, "Object" ) or die "could not create object\n";
812      ok( $object->open, "open worked" );
813  };
814
815will produce the following if the first test failed:
816
817  not ok 1 - The object isa Object
818  #   Failed test 'The object isa Object'
819  #   at /Users/adrianh/Desktop/foo.pl line 14.
820  #   (in MyTest->test_object)
821  #     The object isn't defined
822  not ok 2 - test_object died (could not create object)
823  #   Failed test 'test_object died (could not create object)'
824  #   at /Users/adrianh/Desktop/foo.pl line 19.
825  #   (in MyTest->test_object)
826
827This can considerably simplify testing code that throws exceptions.
828
829Rather than having to explicitly check that the code exited normally (e.g. with L<Test::Exception/"lives_ok">) the test will fail automatically - without aborting the other test methods. For example contrast:
830
831  use Test::Exception;
832
833  my $file;
834  lives_ok { $file = read_file('test.txt') } 'file read';
835  is($file, "content", 'test file read');
836
837with:
838
839  sub read_file : Test {
840      is(read_file('test.txt'), "content", 'test file read');
841  };
842 
843If more than one test remains after an exception then the first one is failed, and the remaining ones are skipped.
844
845Startup methods are a special case. Since startup methods will usually be creating state needed by all the other test methods an exception within a startup method will prevent all other test methods running.
846
847=head1 SKIPPED TESTS
848
849You can skip the rest of the tests in a method by returning from the method before all the test have finished running. The value returned is used as the reason for the tests being skipped.
850
851This makes managing tests that can be skipped for multiple reasons very simple. For example:
852
853  sub flying_pigs : Test(5) {
854      my $pig = Pig->new;
855      isa_ok($pig, 'Pig')           or return("cannot breed pigs")
856      can_ok($pig, 'takeoff')       or return("pigs don't fly here");
857      ok($pig->takeoff, 'takeoff')  or return("takeoff failed");
858      ok( $pig->altitude > 0, 'Pig is airborne' );
859      ok( $pig->airspeed > 0, '  and moving'    );
860  };
861
862If you run this test in an environment where C<Pig-E<gt>new> worked and the takeoff method existed, but failed when ran, you would get:
863
864  ok 1 - The object isa Pig
865  ok 2 - can takeoff
866  not ok 3 - takeoff
867  ok 4 # skip takeoff failed
868  ok 5 # skip takeoff failed
869
870You can also skip tests just as you do in Test::More or Test::Builder - see L<Test::More/"Conditional tests"> for more information.
871
872I<Note:> if you want to skip tests in a method with C<no_plan> tests then you have to explicitly skip the tests in the method - since Test::Class cannot determine how many tests (if any) should be skipped:
873
874  sub test_objects : Tests {
875      my $self = shift;
876      my $objects = $self->{objects};
877      if (@$objects) {
878          isa_ok($_, "Object") foreach (@$objects);
879      } else {
880          $self->builder->skip("no objects to test");
881      };
882  };
883
884Another way of overcoming this problem is to explicitly set the number of tests for the method at run time using L<num_method_tests()|/"num_method_tests"> or L<"num_tests">.
885
886You can make a test class skip all of its tests by setting L<SKIP_CLASS()|SKIP_CLASS> before L<runtests()|"runtests"> is called.
887
888=head1 TO DO TESTS
889
890You can create todo tests just as you do in L<Test::More> and L<Test::Builder> using the C<$TODO> variable. For example:
891
892  sub live_test : Test  {
893      local $TODO = "live currently unimplemented";
894      ok(Object->live, "object live");
895  };
896
897See L<Test::Harness/"Todo tests"> for more information.
898
899
900=head1 EXTENDING TEST CLASSES BY INHERITANCE
901
902You can extend test methods by inheritance in the usual way. For example consider the following test class for a C<Pig> object.
903
904  package Pig::Test;
905  use base qw(Test::Class);
906  use Test::More;
907
908  sub testing_class { "Pig" };
909  sub new_args { (-age => 3) };
910
911  sub setup : Test(setup) {
912      my $self = shift;
913      my $class = $self->testing_class;
914      my @args = $self->new_args;
915      $self->{pig} = $class->new( @args );
916  };
917
918  sub _creation : Test {
919      my $self = shift;
920      isa_ok($self->{pig}, $self->testing_class)
921              or $self->FAIL_ALL('Pig->new failed');
922  };
923
924  sub check_fields : Test {
925      my $pig = shift->{pig};
926      is($pig->age, 3, "age accessed");
927  };
928
929Next consider C<NamedPig> a subclass of C<Pig> where you can give your pig a name.
930
931We want to make sure that all the tests for the C<Pig> object still work for C<NamedPig>. We can do this by subclassing C<Pig::Test> and overriding the C<testing_class> and C<new_args> methods.
932
933  package NamedPig::Test;
934  use base qw(Pig::Test);
935  use Test::More;
936
937  sub testing_class { "NamedPig" };
938  sub new_args { (shift->SUPER::new_args, -name => 'Porky') };
939
940Now we need to test the name method. We could write another test method, but we also have the option of extending the existing C<check_fields> method.
941
942  sub check_fields : Test(2) {
943      my $self = shift;
944      $self->SUPER::check_fields;   
945      is($self->{pig}->name, 'Porky', 'name accessed');
946  };
947
948While the above works, the total number of tests for the method is dependent on the number of tests in its C<SUPER::check_fields>. If we add a test to C<Pig::Test-E<gt>check_fields> we will also have to update the number of tests of C<NamedPig::test-E<gt>check_fields>.
949
950Test::Class allows us to state explicitly that we are adding tests to an existing method by using the C<+> prefix. Since we are adding a single test to C<check_fields> it can be rewritten as:
951
952  sub check_fields : Test(+1) {
953      my $self = shift;
954      $self->SUPER::check_fields;
955      is($self->{pig}->name, 'Porky', 'name accessed');
956  };
957
958With the above definition you can add tests to C<check_fields> in C<Pig::Test> without affecting C<NamedPig::Test>.
959
960
961=head1 RUNNING INDIVIDUAL TESTS
962
963B<NOTE:> The exact mechanism for running individual tests is likely to change in the future.
964
965Sometimes you just want to run a single test.  Commenting out other tests or writing code to skip them can be a hassle, so you can specify the C<TEST_METHOD> environment variable.  The value is expected to be a valid regular expression and, if present, only runs test methods whose names match the regular expression.  Startup, setup, teardown and shutdown tests will still be run.
966
967One easy way of doing this is by specifying the environment variable I<before> the C<runtests> method is called.
968
969Running a test named C<customer_profile>:
970
971 #! /usr/bin/perl
972 use Example::Test;
973     
974 $ENV{TEST_METHOD} = 'customer_profile';
975 Test::Class->runtests;
976
977Running all tests with C<customer> in their name:
978
979 #! /usr/bin/perl
980 use Example::Test;
981     
982 $ENV{TEST_METHOD} = '.*customer.*';
983 Test::Class->runtests;
984
985If you specify an invalid regular expression, your tests will not be run:
986
987 #! /usr/bin/perl
988 use Example::Test;
989     
990 $ENV{TEST_METHOD} = 'C++';
991 Test::Class->runtests;
992
993And when you run it:
994
995 TEST_METHOD (C++) is not a valid regular expression: Search pattern \
996 not terminated at (eval 17) line 1.
997
998
999=head1 ORGANISING YOUR TEST CLASSES
1000
1001You can, of course, organise your test modules as you wish. My personal preferences is:
1002
1003=over 4
1004
1005=item *
1006
1007Name test classes with a suffix of C<::Test> so the test class for the C<Foo::Bar> module would be C<Foo::Bar::Test>.
1008
1009=item *
1010
1011Place all test classes in F<t/lib>.
1012
1013=back
1014
1015The L<Test::Class::Load> provides a simple mechanism for easily loading all of the test classes in a given set of directories.
1016
1017
1018=head1 A NOTE ON LOADING TEST CLASSES
1019
1020Due to its use of subroutine attributes Test::Class based modules must be loaded at compile rather than run time. This is because the :Test attribute is applied by a CHECK block.
1021
1022This can be problematic if you want to dynamically load Test::Class modules. Basically while:
1023
1024  require $some_test_class;
1025 
1026will break, doing:
1027
1028  BEGIN { require $some_test_class };
1029 
1030will work just fine. For more information on CHECK blocks see L<perlmod/"BEGIN, CHECK, INIT and END">.
1031
1032=head1 METHODS
1033
1034=head2 Creating and running tests
1035
1036=over 4
1037
1038=item B<Test>
1039
1040  # test methods
1041  sub method_name : Test { ... };
1042  sub method_name : Test(N) { ... };
1043
1044  # setup methods
1045  sub method_name : Test(setup) { ... };
1046  sub method_name : Test(setup => N) { ... };
1047
1048  # teardown methods
1049  sub method_name : Test(teardown) { ... };
1050  sub method_name : Test(teardown => N) { ... };
1051
1052  # startup methods
1053  sub method_name : Test(startup) { ... };
1054  sub method_name : Test(startup => N) { ... };
1055
1056  # shutdown methods
1057  sub method_name : Test(shutdown) { ... };
1058  sub method_name : Test(shutdown => N) { ... };
1059
1060Marks a startup, setup, test, teardown or shutdown method. See L<runtests()|/"runtests"> for information on how to run methods declared with the C<Test> attribute.
1061
1062N specifies the number of tests the method runs.
1063
1064=over 4
1065
1066=item *
1067
1068If N is an integer then the method should run exactly N tests.
1069
1070=item *
1071
1072If N is an integer with a C<+> prefix then the method is expected to call its C<SUPER::> method and extend it by running N additional tests.
1073
1074=item *
1075
1076If N is the string C<no_plan> then the method can run an arbitrary number of tests.
1077
1078=back
1079
1080If N is not specified it defaults to C<1> for test methods, and C<0> for startup, setup, teardown and shutdown methods.
1081
1082You can change the number of tests that a method runs using L<num_method_tests()|/"num_method_tests"> or L<num_tests()|/"num_tests">.
1083
1084
1085=item B<Tests>
1086
1087  sub method_name : Tests { ... };
1088  sub method_name : Tests(N) { ... };
1089
1090Acts just like the C<:Test> attribute, except that if the number of tests is not specified it defaults to C<no_plan>. So the following are equivalent:
1091
1092  sub silly1 :Test( no_plan ) { ok(1) foreach (1 .. rand 5) };
1093  sub silly2 :Tests           { ok(1) foreach (1 .. rand 5) };
1094
1095
1096=item B<new>
1097
1098  $Tests = CLASS->new(KEY => VAL ...)
1099  $Tests2 = $Tests->new(KEY => VAL ...)
1100
1101Creates a new test object (blessed hashref) containing the specified key/value pairs.
1102
1103If called as an object method the existing object's key/value pairs are copied into the new object. Any key/value pairs passed to C<new> override those in the original object if duplicates occur.
1104
1105Since the test object is passed to every test method as it runs it is a convenient place to store test fixtures. For example:
1106
1107  sub make_fixture : Test(setup) {
1108      my $self = shift;
1109      $self->{object} = Object->new();
1110      $self->{dbh} = Mock::DBI->new(-type => normal);
1111  };
1112
1113  sub test_open : Test {
1114      my $self = shift;
1115      my ($o, $dbh) = ($self->{object}, $self->{dbh});
1116      ok($o->open($dbh), "opened ok");
1117  };
1118
1119See L<num_method_tests()|/"num_method_tests"> for an example of overriding C<new>.
1120
1121
1122=item B<expected_tests>
1123
1124  $n = $Tests->expected_tests
1125  $n = CLASS->expected_tests
1126  $n = $Tests->expected_tests(TEST, ...)
1127  $n = CLASS->expected_tests(TEST, ...)
1128
1129Returns the total number of tests that L<runtests()|/"runtests"> will run on the specified class/object. This includes tests run by any setup and teardown methods.
1130
1131Will return C<no_plan> if the exact number of tests is undetermined (i.e. if any setup, test or teardown method has an undetermined number of tests).
1132
1133The C<expected_tests> of an object after L<runtests()|/"runtests"> has been executed will include any run time changes to the expected number of tests made by L<num_tests()|/"num_tests"> or L<num_method_tests()|/"num_method_tests">.
1134
1135C<expected_tests> can also take an optional list of test objects, test classes and integers. In this case the result is the total number of expected tests for all the test/object classes (including the one the method was applied to) plus any integer values.
1136
1137C<expected_tests> is useful when you're integrating one or more test classes into a more traditional test script, for example:
1138
1139  use Test::More;
1140  use My::Test::Class;
1141
1142  plan tests => My::Test::Class->expected_tests(+2);
1143
1144  ok(whatever, 'a test');
1145  ok(whatever, 'another test');
1146  My::Test::Class->runtests;
1147
1148
1149
1150=item B<runtests>
1151
1152  $allok = $Tests->runtests
1153  $allok = CLASS->runtests
1154  $allok = $Tests->runtests(TEST, ...)
1155  $allok = CLASS->runtests(TEST, ...)
1156
1157C<runtests> is used to run test classes. At its most basic doing:
1158
1159  $test->runtests
1160 
1161will run the test methods of the test object $test, unless C<< $test->SKIP_CLASS >> returns a true value.
1162
1163Unless you have already specified a test plan using Test::Builder (or Test::More, et al) C<runtests> will set the test plan just before the first method that runs a test is executed.
1164
1165If the environment variable C<TEST_VERBOSE> is set C<runtests> will display the name of each test method before it runs like this:
1166
1167  # My::Test::Class->my_test
1168  ok 1 - fribble
1169  # My::Test::Class->another_test
1170  ok 2 - bar
1171
1172Just like L<expected_tests()|/"expected_tests">, C<runtests> can take an optional list of test object/classes and integers. All of the test object/classes are run. Any integers are added to the total number of tests shown in the test header output by C<runtests>.
1173
1174For example, you can run all the tests in test classes A, B and C, plus one additional normal test by doing:
1175
1176    Test::Class->runtests(qw(A B C), +1);
1177    ok(1==1, 'non class test');
1178
1179Finally, if you call C<runtests> on a test class without any arguments it will run all of the test methods of that class, and all subclasses of that class. For example:
1180
1181  #! /usr/bin/perl
1182  # Test all the Foo stuff
1183 
1184  use Foo::Test;
1185  use Foo::Bar::Test;
1186  use Foo::Ni::Test;
1187 
1188  # run all the Foo*Test modules we just loaded
1189  Test::Class->runtests;
1190   
1191
1192=item B<SKIP_CLASS>
1193
1194  $reason = CLASS->SKIP_CLASS;
1195  CLASS->SKIP_CLASS( $reason );
1196
1197Determines whether the test class CLASS should run it's tests. If SKIP_CLASS returns a true value then  L<runtests()|/"runtests"> will not run any of the test methods in CLASS.
1198
1199You can override the default on a class-by-class basis by supplying a new value to SKIP_CLASS. For example if you have an abstract base class that should not run just add the following to your module:
1200
1201  My::Abstract::Test->SKIP_CLASS( 1 );
1202 
1203This will not affect any sub-classes of C<My::Abstract::Test> which will run as normal.
1204
1205If the true value returned by SKIP_CLASS is anything other than "1" then a skip test is output using this value as the skip message. For example:
1206
1207  My::Postgres::Test->SKIP_CLASS(
1208      $ENV{POSTGRES_HOME} ? 0 : '$POSTGRES_HOME needs to be set'
1209  );
1210
1211will output something like this if C<POSTGRES_HOME> is not set
1212
1213        ... other tests ...
1214        ok 123 # skip My::Postgres::Test  - $POSTGRES_HOME needs to be set
1215        ... more tests ...
1216
1217You can also override SKIP_CLASS for a class hierarchy. For example, to prevent any subclasses of My::Postgres::Test running we could override SKIP_CLASS like this:
1218
1219  sub My::Postgres::Test::SKIP_CLASS {
1220      $ENV{POSTGRES_HOME} ? 0 : '$POSTGRES_HOME needs to be set'
1221  };
1222 
1223=back
1224
1225=head2 Fetching and setting a method's test number
1226
1227
1228=over 4
1229
1230=item B<num_method_tests>
1231
1232  $n = $Tests->num_method_tests($method_name)
1233  $Tests->num_method_tests($method_name, $n)
1234  $n = CLASS->num_method_tests($method_name)
1235  CLASS->num_method_tests($method_name, $n)
1236
1237Fetch or set the number of tests that the named method is expected to run.
1238
1239If the method has an undetermined number of tests then $n should be the string C<no_plan>.
1240
1241If the method is extending the number of tests run by the method in a superclass then $n should have a C<+> prefix.
1242
1243When called as a class method any change to the expected number of tests applies to all future test objects. Existing test objects are unaffected.
1244
1245When called as an object method any change to the expected number of tests applies to that object alone.
1246
1247C<num_method_tests> is useful when you need to set the expected number of tests at object creation time, rather than at compile time.
1248
1249For example, the following test class will run a different number of tests depending on the number of objects supplied.
1250
1251  package Object::Test;
1252  use base qw(Test::Class);
1253  use Test::More;
1254
1255  sub new {
1256      my $class = shift;
1257      my $self = $class->SUPER::new(@_);
1258      my $num_objects = @{$self->{objects}};
1259      $self->num_method_tests('test_objects', $num_objects);
1260      return($self);
1261  };
1262
1263  sub test_objects : Tests {
1264    my $self = shift;
1265    ok($_->open, "opened $_") foreach @{$self->{objects}};
1266  };
1267  ...
1268  # This runs two tests
1269  Object::Test->new(objects => [$o1, $o2]);
1270
1271The advantage of setting the number of tests at object creation time, rather than using a test method without a plan, is that the number of expected tests can be determined before testing begins. This allows better diagnostics from L<runtests()|/"runtests">, L<Test::Builder> and L<Test::Harness>.
1272
1273C<num_method_tests> is a protected method and can only be called by subclasses of Test::Class. It fetches or sets the expected number of tests for the methods of the class it was I<called in>, not the methods of the object/class it was I<applied to>. This allows test classes that use C<num_method_tests> to be subclassed easily.
1274
1275For example, consider the creation of a subclass of Object::Test that ensures that all the opened objects are read-only:
1276
1277  package Special::Object::Test;
1278  use base qw(Object::Test);
1279  use Test::More;
1280
1281  sub test_objects : Test(+1) {
1282      my $self = shift;
1283      $self->SUPER::test_objects;
1284      my @bad_objects = grep {! $_->read_only} (@{$self->{objects}});
1285      ok(@bad_objects == 0, "all objects read only");
1286  };
1287  ...
1288  # This runs three tests
1289  Special::Object::Test->new(objects => [$o1, $o2]);
1290
1291Since the call to C<num_method_tests> in Object::Test only affects the C<test_objects> of Object::Test, the above works as you would expect.
1292
1293
1294=item B<num_tests>
1295
1296  $n = $Tests->num_tests
1297  $Tests->num_tests($n)
1298  $n = CLASS->num_tests
1299  CLASS->num_tests($n)
1300
1301Set or return the number of expected tests associated with the currently running test method. This is the same as calling L<num_method_tests()|/"num_method_tests"> with a method name of L<current_method()|/"current_method">.
1302
1303For example:
1304
1305  sub txt_files_readable : Tests {
1306      my $self = shift;
1307      my @files = <*.txt>;
1308      $self->num_tests(scalar(@files));
1309      ok(-r $_, "$_ readable") foreach (@files);
1310  };
1311
1312Setting the number of expected tests at run time, rather than just having a C<no_plan> test method, allows L<runtests()|/"runtests"> to display appropriate diagnostic messages if the method runs a different number of tests.
1313
1314=back
1315
1316
1317=head2 Support methods
1318
1319=over 4
1320
1321=item B<builder>
1322
1323  $Tests->builder
1324
1325Returns the underlying L<Test::Builder> object that Test::Class uses. For example:
1326
1327  sub test_close : Test {
1328      my $self = shift;
1329      my ($o, $dbh) = ($self->{object}, $self->{dbh});
1330      $self->builder->ok($o->close($dbh), "closed ok");
1331  };
1332
1333=item B<current_method>
1334
1335  $method_name = $Tests->current_method
1336  $method_name = CLASS->current_method
1337
1338Returns the name of the test method currently being executed by L<runtests()|/"runtests">, or C<undef> if L<runtests()|/"runtests"> has not been called.
1339
1340The method name is also available in the setup and teardown methods that run before and after the test method. This can be useful in producing diagnostic messages, for example:
1341
1342  sub test_invarient : Test(teardown => 1) {
1343      my $self = shift;
1344      my $m = $self->current_method;
1345      ok($self->invarient_ok, "class okay after $m");
1346  };
1347
1348
1349
1350=item B<BAILOUT>
1351
1352  $Tests->BAILOUT($reason)
1353  CLASS->BAILOUT($reason)
1354
1355Things are going so badly all testing should terminate, including running any additional test scripts invoked by L<Test::Harness>. This is exactly the same as doing:
1356
1357  $self->builder->BAILOUT
1358
1359See L<Test::Builder/"BAILOUT"> for details. Any teardown and shutdown methods are I<not> run.
1360
1361
1362=item B<FAIL_ALL>
1363
1364  $Tests->FAIL_ALL($reason)
1365  CLASS->FAIL_ALL($reason)
1366
1367Things are going so badly all the remaining tests in the current script should fail. Exits immediately with the number of tests failed, or C<254> if more than 254 tests were run. Any teardown methods are I<not> run.
1368
1369This does not affect the running of any other test scripts invoked by L<Test::Harness>.
1370
1371For example, if all your tests rely on the ability to create objects then you might want something like this as an early test:
1372
1373  sub _test_new : Test(3) {
1374      my $self = shift;
1375      isa_ok(Object->new, "Object")
1376          || $self->FAIL_ALL('cannot create Objects');
1377      ...
1378  };
1379
1380
1381
1382=item B<SKIP_ALL>
1383
1384  $Tests->SKIP_ALL($reason)
1385  CLASS->SKIP_ALL($reason)
1386
1387Things are going so badly all the remaining tests in the current script should be skipped. Exits immediately with C<0> - teardown methods are I<not> run.
1388
1389This does not affect the running of any other test scripts invoked by L<Test::Harness>.
1390
1391For example, if you had a test script that only applied to the darwin OS you could write:
1392
1393  sub _darwin_only : Test(setup) {
1394      my $self = shift;
1395      $self->SKIP_ALL("darwin only") unless $^O eq "darwin";   
1396  };
1397
1398
1399=back
1400
1401
1402
1403=head1 HELP FOR CONFUSED JUNIT USERS
1404
1405This section is for people who have used JUnit (or similar) and are confused because they don't see the TestCase/Suite/Runner class framework they were expecting. Here we take each of the major classes in JUnit and compare them with their equivalent Perl testing modules.
1406
1407=over 4
1408
1409=item B<Class Assert>
1410
1411The test assertions provided by Assert correspond to the test functions provided by the L<Test::Builder> based modules (L<Test::More>, L<Test::Exception>, L<Test::Differences>, etc.)
1412
1413Unlike JUnit the test functions supplied by Test::More et al do I<not> throw exceptions on failure. They just report the failure to STDOUT where it is collected by L<Test::Harness>. This means that where you have
1414
1415  sub foo : Test(2) {
1416      ok($foo->method1);
1417      ok($foo->method2);
1418  };
1419
1420The second test I<will> run if the first one fails. You can emulate the JUnit way of doing it by throwing an explicit exception on test failure:
1421
1422  sub foo : Test(2) {
1423      ok($foo->method1) or die "method1 failed";
1424      ok($foo->method2);
1425  };
1426
1427The exception will be caught by Test::Class and the other test automatically failed.
1428
1429=item B<Class TestCase>
1430
1431Test::Class corresponds to TestCase in JUnit.
1432
1433In Test::Class setup, test and teardown methods are marked explicitly using the L<Test|/"Test"> attribute. Since we need to know the total number of tests to provide a test plan for L<Test::Harness> we also state how many tests each method runs.
1434
1435Unlike JUnit you can have multiple setup/teardown methods in a class.
1436
1437=item B<Class TestSuite>
1438
1439Test::Class also does the work that would be done by TestSuite in JUnit.
1440
1441Since the methods are marked with attributes Test::Class knows what is and isn't a test method. This allows it to run all the test methods without having the developer create a suite manually, or use reflection to dynamically determine the test methods by name. See the L<runtests()|/"runtests"> method for more details.
1442
1443The running order of the test methods is fixed in Test::Class. Methods are executed in alphabetical order.
1444
1445Unlike JUnit, Test::Class currently does not allow you to run individual test methods.
1446
1447=item B<Class TestRunner>
1448
1449L<Test::Harness> does the work of the TestRunner in JUnit. It collects the test results (sent to STDOUT) and collates the results.
1450
1451Unlike JUnit there is no distinction made by Test::Harness between errors and failures. However, it does support skipped and todo test - which JUnit does not.
1452
1453If you want to write your own test runners you should look at L<Test::Harness::Straps>.
1454
1455=back
1456
1457
1458=head1 OTHER MODULES FOR XUNIT TESTING IN PERL
1459
1460In addition to Test::Class there are two other distributions for xUnit testing in perl. Both have a longer history than Test::Class and might be more suitable for your needs.
1461
1462I am biased since I wrote Test::Class - so please read the following with appropriate levels of scepticism. If you think I have misrepresented the modules please let me know.
1463
1464=over 4
1465
1466=item B<Test::SimpleUnit>
1467
1468A very simple unit testing framework. If you are looking for a lightweight single module solution this might be for you.
1469
1470The advantage of L<Test::SimpleUnit> is that it is simple! Just one module with a smallish API to learn.
1471
1472Of course this is also the disadvantage.
1473
1474It's not class based so you cannot create testing classes to reuse and extend.
1475
1476It doesn't use L<Test::Builder> so it's difficult to extend or integrate with other testing modules. If you are already familiar with L<Test::Builder>, L<Test::More> and friends you will have to learn a new test assertion API. It does not support L<todo tests|Test::Harness/"Todo tests">.
1477
1478=item B<Test::Unit>
1479
1480L<Test::Unit> is a port of JUnit L<http://www.junit.org/> into perl. If you have used JUnit then the Test::Unit framework should be very familiar.
1481
1482It is class based so you can easily reuse your test classes and extend by subclassing. You get a nice flexible framework you can tweak to your heart's content. If you can run Tk you also get a graphical test runner.
1483
1484However, Test::Unit is not based on L<Test::Builder>. You cannot easily move Test::Builder based test functions into Test::Unit based classes. You have to learn another test assertion API.
1485
1486Test::Unit implements it's own testing framework separate from L<Test::Harness>. You can retrofit *.t scripts as unit tests, and output test results in the format that L<Test::Harness> expects, but things like L<todo tests|Test::Harness/"Todo tests"> and L<skipping tests|Test::Harness/"Skipping tests"> are not supported.
1487
1488=back
1489
1490
1491=head1 BUGS
1492
1493None known at the time of writing.
1494
1495If you find any bugs please let me know by e-mail at <adrianh@quietstars.com>, or report the problem with L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Test-Class>.
1496
1497
1498=head1 COMMUNITY
1499
1500=head2 perl-qa
1501
1502If you are interested in testing using Perl I recommend you visit L<http://qa.perl.org/> and join the excellent perl-qa mailing list. See L<http://lists.perl.org/showlist.cgi?name=perl-qa> for details on how to subscribe.
1503
1504=head2 perlmonks
1505
1506You can find users of Test::Class, including the module author, on  L<http://www.perlmonks.org/>. Feel free to ask questions on Test::Class there.
1507
1508=head2 CPAN::Forum
1509
1510The CPAN Forum is a web forum for discussing Perl's CPAN modules.   The Test::Class forum can be found at L<http://www.cpanforum.com/dist/Test-Class>.
1511
1512
1513=head1 TO DO
1514
1515If you think this module should do something that it doesn't (or does something that it shouldn't) please let me know.
1516
1517You can see my current to do list at L<http://adrianh.tadalist.com/lists/public/4798>, with an RSS feed of changes at L<http://adrianh.tadalist.com/lists/feed_public/4798>.
1518
1519
1520=head1 ACKNOWLEDGMENTS
1521
1522This is yet another implementation of the ideas from Kent Beck's Testing Framework paper L<http://www.xprogramming.com/testfram.htm>.
1523
1524Thanks to
1525Adam Kennedy,
1526agianni,
1527Apocalypse,
1528Ask Bjorn Hansen,
1529Chris Dolan,
1530Chris Williams,
1531Corion,
1532Cosimo Streppone,
1533Daniel Berger,
1534Dave O'Neill,
1535David Cantrell,
1536David Wheeler,
1537Emil Jansson,
1538Gunnar Wolf,
1539Hai Pham,
1540Hynek,
1541imacat,
1542Jeff Deifik,
1543Jim Brandt,
1544Jochen Stenzel,
1545Johan Lindstrom,
1546John West,
1547Jonathan R. Warden,
1548Joshua ben Jore,
1549Jost Krieger,
1550Kenichi Ishigaki
1551Lee Goddard,
1552Mark Reynolds,
1553Mark Stosberg,
1554Martin Ferrari,
1555Mathieu Sauve-Frankel,
1556Matt Trout,
1557Matt Williamson,
1558Michael G Schwern,
1559Murat Uenalan,
1560Nicholas Clark,
1561Ovid,
1562Piers Cawley,
1563Rob Kinyon,
1564Scott Lanning,
1565Sebastien Aperghis-Tramoni,
1566Steve Kirkup,
1567Stray Toaster,
1568Ted Carnahan,
1569Terrence Brannon,
1570Tom Metro,
1571Tony Bowden,
1572Tony Edwardson,
1573William McKee,
1574various anonymous folk and all the fine people on perl-qa for their feedback, patches, suggestions and nagging.
1575
1576This module wouldn't be possible without the excellent L<Test::Builder>. Thanks to chromatic and Michael G Schwern for creating such a useful module.
1577
1578
1579=head1 AUTHOR
1580
1581Adrian Howard <adrianh@quietstars.com>
1582
1583If you use this module, and can spare the time please drop me an e-mail or rate it at L<http://cpanratings.perl.org/rate/?distribution=Test-Class>.
1584
1585=head1 SEE ALSO
1586
1587=over 4
1588
1589=item L<Test::Class::Load>
1590
1591Simple way to load "Test::Class" classes automatically.
1592
1593=item L<http://del.icio.us/tag/Test::Class>
1594
1595Delicious links on Test::Class.
1596
1597=item Perl Testing: A Developer's Notebook by Ian Langworth and chromatic
1598
1599Chapter 8 covers using Test::Class.
1600
1601=item Advanced Perl Programming, second edition by Simon Cozens
1602
1603Chapter 8 has a few pages on using Test::Class.
1604
1605=item The Perl Journal, April 2003
1606
1607Includes the article "Test-Driven Development in Perl" by Piers Cawley that uses Test::Class.
1608
1609=item L<Test::Builder>
1610
1611Support module for building test libraries.
1612
1613=item L<Test::Simple> & L<Test::More>
1614
1615Basic utilities for writing tests.
1616
1617=item L<http://qa.perl.org/test-modules.html>
1618
1619Overview of some of the many testing modules available on CPAN.
1620
1621=item L<http://del.icio.us/tag/perl+testing>
1622
1623Delicious links on perl testing.
1624
1625=item L<Test::Object>
1626
1627Another approach to object oriented testing.
1628
1629=item L<Test::Group> and L<Test::Block>
1630
1631Alternatives to grouping sets of tests together.
1632
1633=back
1634
1635The following modules use Test::Class as part of their test suite. You might want to look at them for usage examples:
1636
1637=over 4
1638
1639L<Aspect>, Bricolage (L<http://www.bricolage.cc/>), L<Class::StorageFactory>, L<CGI::Application::Search>, L<DBIx::Romani>, L<Xmldoom>, L<Object::Relational>, L<File::Random>, L<Geography::JapanesePrefectures>, L<Google::Adwords>, L<Merge::HashRef>, L<PerlBuildSystem>, L<Pixie>, L<Yahoo::Marketing>, and L<XUL-Node>
1640
1641=back
1642
1643The following modules are not based on L<Test::Builder>, but may be of interest as alternatives to Test::Class.
1644
1645=over 4
1646
1647=item L<Test::Unit>
1648
1649Perl unit testing framework closely modeled on JUnit.
1650
1651=item L<Test::SimpleUnit>
1652
1653A very simple unit testing framework.
1654
1655=back
1656
1657=head1 LICENCE
1658
1659Copyright 2002-2007 Adrian Howard, All Rights Reserved.
1660
1661This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Note: See TracBrowser for help on using the browser.