1Test::Class(3)        User Contributed Perl Documentation       Test::Class(3)
2
3
4

NAME

6       Test::Class - Easily create test classes in an xUnit/JUnit style
7

SYNOPSIS

9         package Example::Test;
10         use base qw(Test::Class);
11         use Test::More;
12
13         # setup methods are run before every test method.
14         sub make_fixture : Test(setup) {
15             my $array = [1, 2];
16             shift->{test_array} = $array;
17         };
18
19         # a test method that runs 1 test
20         sub test_push : Test {
21             my $array = shift->{test_array};
22             push @$array, 3;
23             is_deeply($array, [1, 2, 3], 'push worked');
24         };
25
26         # a test method that runs 4 tests
27         sub test_pop : Test(4) {
28             my $array = shift->{test_array};
29             is(pop @$array, 2, 'pop = 2');
30             is(pop @$array, 1, 'pop = 1');
31             is_deeply($array, [], 'array empty');
32             is(pop @$array, undef, 'pop = undef');
33         };
34
35         # teardown methods are run after every test method.
36         sub teardown : Test(teardown) {
37             my $array = shift->{test_array};
38             diag("array = (@$array) after test(s)");
39         };
40
41       later in a nearby .t file
42
43         #! /usr/bin/perl
44         use Example::Test;
45
46         # run all the test methods in Example::Test
47         Test::Class->runtests;
48
49       Outputs:
50
51         1..5
52         ok 1 - pop = 2
53         ok 2 - pop = 1
54         ok 3 - array empty
55         ok 4 - pop = undef
56         # array = () after test(s)
57         ok 5 - push worked
58         # array = (1 2 3) after test(s)
59

DESCRIPTION

61       Test::Class provides a simple way of creating classes and objects to
62       test your code in an xUnit style.
63
64       Built using Test::Builder it is designing to work with other
65       Test::Builder based modules (Test::More, Test::Differences,
66       Test::Exception, etc.)
67
68       Note: This module will make more sense if you are already familiar with
69       the "standard" mechanisms for testing perl code. Those unfamiliar  with
70       Test::Harness, Test::Simple, Test::More and friends should go take a
71       look at them now. Test::Tutorial is a good starting point.
72

INTRODUCTION

74   A brief history lesson
75       In 1994 Kent Beck wrote a testing framework for Smalltalk called SUnit.
76       It was popular. You can read a copy of his original paper at
77       <http://www.xprogramming.com/testfram.htm>.
78
79       Later Kent Beck and Erich Gamma created JUnit for testing Java
80       <http://www.junit.org/>. It was popular too.
81
82       Now there are xUnit frameworks for every language from Ada to XSLT. You
83       can find a list at <http://www.xprogramming.com/software.htm>.
84
85       While xUnit frameworks are traditionally associated with unit testing
86       they are also useful in the creation of functional/acceptance tests.
87
88       Test::Class is (yet another) implementation of xUnit style testing in
89       perl.
90
91   Why you should use Test::Class
92       Test::Class attempts to provide simple xUnit testing that integrates
93       simply with the standard perl *.t style of testing. In particular:
94
95       ·   All the advantages of xUnit testing. You can easily create test
96           fixtures and isolate tests. It provides a framework that should be
97           familiar to people who have used other xUnit style test systems.
98
99       ·   It is built with Test::Builder and should co-exist happily with all
100           other Test::Builder based modules. This makes using test classes in
101           *.t scripts, and refactoring normal tests into test classes, much
102           simpler because:
103
104           ·   You do not have to learn a new set of new test APIs and can
105               continue using ok(), like(), etc. from Test::More and friends.
106
107           ·   Skipping tests and todo tests are supported.
108
109           ·   You can have normal tests and Test::Class classes co-existing
110               in the same *.t script. You don't have to re-write an entire
111               script, but can use test classes as and when it proves useful.
112
113       ·   You can easily package your tests as classes/modules, rather than
114           *.t scripts. This simplifies reuse, documentation and distribution,
115           encourages refactoring, and allows tests to be extended by
116           inheritance.
117
118       ·   You can have multiple setup/teardown methods. For example have one
119           teardown method to clean up resources and another to check that
120           class invariants still hold.
121
122       ·   It can make running tests faster. Once you have refactored your *.t
123           scripts into classes they can be easily run from a single script.
124           This gains you the (often considerable) start up time that each
125           separate *.t script takes.
126
127   Why you should not use Test::Class
128       ·   If your *.t scripts are working fine then don't bother with
129           Test::Class. For simple test suites it is almost certainly
130           overkill. Don't start thinking about using Test::Class until issues
131           like duplicate code in your test scripts start to annoy.
132
133       ·   If you are distributing your code it is yet another module that the
134           user has to have to run your tests (unless you distribute it with
135           your test suite of course).
136
137       ·   If you are used to the TestCase/Suite/Runner class structure used
138           by JUnit and similar testing frameworks you may find Test::Unit
139           more familiar (but try reading "HELP FOR CONFUSED JUNIT USERS"
140           before you give up).
141

TEST CLASSES

143       A test class is just a class that inherits from Test::Class. Defining a
144       test class is as simple as doing:
145
146         package Example::Test;
147         use base qw(Test::Class);
148
149       Since Test::Class does not provide its own test functions, but uses
150       those provided by Test::More and friends, you will nearly always also
151       want to have:
152
153         use Test::More;
154
155       to import the test functions into your test class.
156

METHOD TYPES

158       There are three different types of method you can define using
159       Test::Class.
160
161   1) Test methods
162       You define test methods using the Test attribute. For example:
163
164         package Example::Test;
165         use base qw(Test::Class);
166         use Test::More;
167
168         sub subtraction : Test {
169             is( 2-1, 1, 'subtraction works );
170         };
171
172       This declares the "subtraction" method as a test method that runs one
173       test.
174
175       If your test method runs more than one test, you should put the number
176       of tests in brackets like this:
177
178         sub addition : Test(2) {
179             is(10 + 20, 30, 'addition works');
180             is(20 + 10, 30, '  both ways');
181         };
182
183       If you don't know the number of tests at compile time you can use
184       "no_plan" like this.
185
186         sub check_class : Test(no_plan) {
187             my $objects = shift->{objects};
188             isa_ok($_, "Object") foreach @$objects;
189         };
190
191       or use the :Tests attribute, which acts just like ":Test" but defaults
192       to "no_plan" if no number is given:
193
194         sub check_class : Tests {
195             my $objects = shift->{objects};
196             isa_ok($_, "Object") foreach @$objects;
197         };
198
199   2) Setup and teardown methods
200       Setup and teardown methods are run before and after every test. For
201       example:
202
203         sub before : Test(setup)    { diag("running before test") };
204         sub after  : Test(teardown) { diag("running after test") };
205
206       You can use setup and teardown methods to create common objects used by
207       all of your test methods (a test fixture) and store them in your
208       Test::Class object, treating it as a hash. For example:
209
210         sub pig : Test(setup) {
211             my $self = shift;
212             $self->{test_pig} = Pig->new;
213         };
214
215         sub born_hungry : Test {
216             my $pig = shift->{test_pig};
217             is($pig->hungry, 'pigs are born hungry');
218         };
219
220         sub eats : Test(3) {
221             my $pig = shift->{test_pig};
222             ok(  $pig->feed,   'pig fed okay');
223             ok(! $pig->hungry, 'fed pig not hungry');
224             ok(! $pig->feed,   'cannot feed full pig');
225         };
226
227       You can also declare setup and teardown methods as running tests. For
228       example you could check that the test pig survives each test method by
229       doing:
230
231         sub pig_alive : Test(teardown => 1) {
232             my $pig = shift->{test_pig};
233             ok($pig->alive, 'pig survived tests' );
234         };
235
236   3) Startup and shutdown methods
237       Startup and shutdown methods are like setup and teardown methods for
238       the whole test class. All the startup methods are run once when you
239       start running a test class. All the shutdown methods are run once just
240       before a test class stops running.
241
242       You can use these to create and destroy expensive objects that you
243       don't want to have to create and destroy for every test - a database
244       connection for example:
245
246         sub db_connect : Test(startup) {
247             shift->{dbi} = DBI->connect;
248         };
249
250         sub db_disconnect : Test(shutdown) {
251             shift->{dbi}->disconnect;
252         };
253
254       Just like setup and teardown methods you can pass an optional number of
255       tests to startup and shutdown methods. For example:
256
257         sub example : Test(startup => 1) {
258             ok(1, 'a startup method with one test');
259         };
260
261       If a startup method has a failing test or throws an exception then all
262       other tests for the current test object are ignored.
263

RUNNING TESTS

265       You run test methods with runtests(). Doing:
266
267         Test::Class->runtests
268
269       runs all of the test methods in every loaded test class. This allows
270       you to easily load multiple test classes in a *.t file and run them
271       all.
272
273         #! /usr/bin/perl
274
275         # load all the test classes I want to run
276         use Foo::Test;
277         use Foo::Bar::Test;
278         use Foo::Fribble::Test;
279         use Foo::Ni::Test;
280
281         # and run them all
282         Test::Class->runtests;
283
284       You can use Test::Class::Load to automatically load all the test
285       classes in a given set of directories.
286
287       If you need finer control you can create individual test objects with
288       new(). For example to just run the tests in the test class
289       "Foo::Bar::Test" you can do:
290
291         Example::Test->new->runtests
292
293       You can also pass runtests() a list of test objects to run. For
294       example:
295
296         my $o1 = Example::Test->new;
297         my $o2 = Another::Test->new;
298         # runs all the tests in $o1 and $o2
299         $o1->runtests($o2);
300
301       Since, by definition, the base Test::Class has no tests you could also
302       have written:
303
304         my $o1 = Example::Test->new;
305         my $o2 = Another::Test->new;
306         Test::Class->runtests($o1, $o2);
307
308       If you pass runtests() class names it will automatically create test
309       objects for you, so the above can be written more compactly as:
310
311         Test::Class->runtests(qw( Example::Test Another::Test ))
312
313       In all of the above examples runtests() will look at the number of
314       tests both test classes run and output an appropriate test header for
315       Test::Harness automatically.
316
317       What happens if you run test classes and normal tests in the same
318       script? For example:
319
320         Test::Class->runtests;
321         ok(Example->new->foo, 'a test not in the test class');
322         ok(Example->new->bar, 'ditto');
323
324       Test::Harness will complain that it saw more tests than it expected
325       since the test header output by runtests() will not include the two
326       normal tests.
327
328       To overcome this problem you can pass an integer value to runtests().
329       This is added to the total number of tests in the test header. So the
330       problematic example can be rewritten as follows:
331
332         Test::Class->runtests(+2);
333         ok(Example->new->foo, 'a test not in the test class');
334         ok(Example->new->bar, 'ditto');
335
336       If you prefer to write your test plan explicitly you can use
337       expected_tests() to find out the number of tests a class/object is
338       expected to run.
339
340       Since runtests() will not output a test plan if one has already been
341       set the previous example can be written as:
342
343         plan tests => Test::Class->expected_tests(+2);
344         Test::Class->runtests;
345         ok(Example->new->foo, 'a test not in the test class');
346         ok(Example->new->bar, 'ditto');
347
348       Remember: Test objects are just normal perl objects. Test classes are
349       just normal perl classes. Setup, test and teardown methods are just
350       normal methods. You are completely free to have other methods in your
351       class that are called from your test methods, or have object specific
352       "new" and "DESTROY" methods.
353
354       In particular you can override the new() method to pass parameters to
355       your test object, or re-define the number of tests a method will run.
356       See num_method_tests() for an example.
357

TEST DESCRIPTIONS

359       The test functions you import from Test::More and other Test::Builder
360       based modules usually take an optional third argument that specifies
361       the test description, for example:
362
363         is $something, $something_else, 'a description of my test';
364
365       If you do not supply a test description, and the test function does not
366       supply its own default, then Test::Class will use the name of the
367       currently running test method, replacing all "_" characters with spaces
368       so:
369
370         sub one_plus_one_is_two : Test {
371             is 1+1, 2;
372         }
373
374       will result in:
375
376         ok 1 - one plus one is two
377

RUNNING ORDER OF METHODS

379       Methods of each type are run in the following order:
380
381       1.  All of the startup methods in alphabetical order
382
383       2.  For each test method, in alphabetical order:
384
385           · All of the setup methods in alphabetical order
386
387           · The test method.
388
389           · All of the teardown methods in alphabetical order
390
391       3.  All of the shutdown methods in alphabetical order.
392
393       Most of the time you should not care what order tests are run in, but
394       it can occasionally be useful to force some test methods to be run
395       early. For example:
396
397         sub _check_new {
398             my $self = shift;
399             isa_ok(Object->new, "Object") or $self->BAILOUT('new fails!');
400         };
401
402       The leading "_" will force the above method to run first - allowing the
403       entire suite to be aborted before any other test methods run.
404

HANDLING EXCEPTIONS

406       If a startup, setup, test, teardown or shutdown method dies then
407       runtests() will catch the exception and fail any remaining test. For
408       example:
409
410         sub test_object : Test(2) {
411             my $object = Object->new;
412             isa_ok( $object, "Object" ) or die "could not create object\n";
413             ok( $object->open, "open worked" );
414         };
415
416       will produce the following if the first test failed:
417
418         not ok 1 - The object isa Object
419         #   Failed test 'The object isa Object'
420         #   at /Users/adrianh/Desktop/foo.pl line 14.
421         #   (in MyTest->test_object)
422         #     The object isn't defined
423         not ok 2 - test_object died (could not create object)
424         #   Failed test 'test_object died (could not create object)'
425         #   at /Users/adrianh/Desktop/foo.pl line 19.
426         #   (in MyTest->test_object)
427
428       This can considerably simplify testing code that throws exceptions.
429
430       Rather than having to explicitly check that the code exited normally
431       (e.g. with "lives_ok" in Test::Exception) the test will fail
432       automatically - without aborting the other test methods. For example
433       contrast:
434
435         use Test::Exception;
436
437         my $file;
438         lives_ok { $file = read_file('test.txt') } 'file read';
439         is($file, "content", 'test file read');
440
441       with:
442
443         sub read_file : Test {
444             is(read_file('test.txt'), "content", 'test file read');
445         };
446
447       If more than one test remains after an exception then the first one is
448       failed, and the remaining ones are skipped.
449
450       Startup methods are a special case. Since startup methods will usually
451       be creating state needed by all the other test methods an exception
452       within a startup method will prevent all other test methods running.
453

SKIPPED TESTS

455       You can skip the rest of the tests in a method by returning from the
456       method before all the test have finished running. The value returned is
457       used as the reason for the tests being skipped.
458
459       This makes managing tests that can be skipped for multiple reasons very
460       simple. For example:
461
462         sub flying_pigs : Test(5) {
463             my $pig = Pig->new;
464             isa_ok($pig, 'Pig')           or return("cannot breed pigs")
465             can_ok($pig, 'takeoff')       or return("pigs don't fly here");
466             ok($pig->takeoff, 'takeoff')  or return("takeoff failed");
467             ok( $pig->altitude > 0, 'Pig is airborne' );
468             ok( $pig->airspeed > 0, '  and moving'    );
469         };
470
471       If you run this test in an environment where "Pig->new" worked and the
472       takeoff method existed, but failed when ran, you would get:
473
474         ok 1 - The object isa Pig
475         ok 2 - can takeoff
476         not ok 3 - takeoff
477         ok 4 # skip takeoff failed
478         ok 5 # skip takeoff failed
479
480       You can also skip tests just as you do in Test::More or Test::Builder -
481       see "Conditional tests" in Test::More for more information.
482
483       Note: if you want to skip tests in a method with "no_plan" tests then
484       you have to explicitly skip the tests in the method - since Test::Class
485       cannot determine how many tests (if any) should be skipped:
486
487         sub test_objects : Tests {
488             my $self = shift;
489             my $objects = $self->{objects};
490             if (@$objects) {
491                 isa_ok($_, "Object") foreach (@$objects);
492             } else {
493                 $self->builder->skip("no objects to test");
494             };
495         };
496
497       Another way of overcoming this problem is to explicitly set the number
498       of tests for the method at run time using num_method_tests() or
499       "num_tests".
500
501       You can make a test class skip all of its tests by setting SKIP_CLASS()
502       before runtests() is called.
503

TO DO TESTS

505       You can create todo tests just as you do in Test::More and
506       Test::Builder using the $TODO variable. For example:
507
508         sub live_test : Test  {
509             local $TODO = "live currently unimplemented";
510             ok(Object->live, "object live");
511         };
512
513       See "Todo tests" in Test::Harness for more information.
514

EXTENDING TEST CLASSES BY INHERITANCE

516       You can extend test methods by inheritance in the usual way. For
517       example consider the following test class for a "Pig" object.
518
519         package Pig::Test;
520         use base qw(Test::Class);
521         use Test::More;
522
523         sub testing_class { "Pig" };
524         sub new_args { (-age => 3) };
525
526         sub setup : Test(setup) {
527             my $self = shift;
528             my $class = $self->testing_class;
529             my @args = $self->new_args;
530             $self->{pig} = $class->new( @args );
531         };
532
533         sub _creation : Test {
534             my $self = shift;
535             isa_ok($self->{pig}, $self->testing_class)
536                     or $self->FAIL_ALL('Pig->new failed');
537         };
538
539         sub check_fields : Test {
540             my $pig = shift->{pig};
541             is($pig->age, 3, "age accessed");
542         };
543
544       Next consider "NamedPig" a subclass of "Pig" where you can give your
545       pig a name.
546
547       We want to make sure that all the tests for the "Pig" object still work
548       for "NamedPig". We can do this by subclassing "Pig::Test" and
549       overriding the "testing_class" and "new_args" methods.
550
551         package NamedPig::Test;
552         use base qw(Pig::Test);
553         use Test::More;
554
555         sub testing_class { "NamedPig" };
556         sub new_args { (shift->SUPER::new_args, -name => 'Porky') };
557
558       Now we need to test the name method. We could write another test
559       method, but we also have the option of extending the existing
560       "check_fields" method.
561
562         sub check_fields : Test(2) {
563             my $self = shift;
564             $self->SUPER::check_fields;
565             is($self->{pig}->name, 'Porky', 'name accessed');
566         };
567
568       While the above works, the total number of tests for the method is
569       dependent on the number of tests in its "SUPER::check_fields". If we
570       add a test to "Pig::Test->check_fields" we will also have to update the
571       number of tests of "NamedPig::test->check_fields".
572
573       Test::Class allows us to state explicitly that we are adding tests to
574       an existing method by using the "+" prefix. Since we are adding a
575       single test to "check_fields" it can be rewritten as:
576
577         sub check_fields : Test(+1) {
578             my $self = shift;
579             $self->SUPER::check_fields;
580             is($self->{pig}->name, 'Porky', 'name accessed');
581         };
582
583       With the above definition you can add tests to "check_fields" in
584       "Pig::Test" without affecting "NamedPig::Test".
585

RUNNING INDIVIDUAL TESTS

587       NOTE: The exact mechanism for running individual tests is likely to
588       change in the future.
589
590       Sometimes you just want to run a single test.  Commenting out other
591       tests or writing code to skip them can be a hassle, so you can specify
592       the "TEST_METHOD" environment variable.  The value is expected to be a
593       valid regular expression and, if present, only runs test methods whose
594       names match the regular expression.  Startup, setup, teardown and
595       shutdown tests will still be run.
596
597       One easy way of doing this is by specifying the environment variable
598       before the "runtests" method is called.
599
600       Running a test named "customer_profile":
601
602        #! /usr/bin/perl
603        use Example::Test;
604
605        $ENV{TEST_METHOD} = 'customer_profile';
606        Test::Class->runtests;
607
608       Running all tests with "customer" in their name:
609
610        #! /usr/bin/perl
611        use Example::Test;
612
613        $ENV{TEST_METHOD} = '.*customer.*';
614        Test::Class->runtests;
615
616       If you specify an invalid regular expression, your tests will not be
617       run:
618
619        #! /usr/bin/perl
620        use Example::Test;
621
622        $ENV{TEST_METHOD} = 'C++';
623        Test::Class->runtests;
624
625       And when you run it:
626
627        TEST_METHOD (C++) is not a valid regular expression: Search pattern \
628        not terminated at (eval 17) line 1.
629

ORGANISING YOUR TEST CLASSES

631       You can, of course, organise your test modules as you wish. My personal
632       preferences is:
633
634       ·   Name test classes with a suffix of "::Test" so the test class for
635           the "Foo::Bar" module would be "Foo::Bar::Test".
636
637       ·   Place all test classes in t/lib.
638
639       The Test::Class::Load provides a simple mechanism for easily loading
640       all of the test classes in a given set of directories.
641

A NOTE ON LOADING TEST CLASSES

643       Due to its use of subroutine attributes Test::Class based modules must
644       be loaded at compile rather than run time. This is because the :Test
645       attribute is applied by a CHECK block.
646
647       This can be problematic if you want to dynamically load Test::Class
648       modules. Basically while:
649
650         require $some_test_class;
651
652       will break, doing:
653
654         BEGIN { require $some_test_class };
655
656       will work just fine. For more information on CHECK blocks see "BEGIN,
657       CHECK, INIT and END" in perlmod.
658
659       If you still can't arrange for your classes to be loaded at runtime,
660       you could use an alternative mechanism for adding your tests:
661
662         # sub test_something : Test(3) {...}
663         # becomes
664         sub test_something {...}
665         __PACKAGE__->add_testinfo('test_something', test => 3);
666
667       See the add_testinfo method for more details.
668

METHODS

670   Creating and running tests
671       Test
672             # test methods
673             sub method_name : Test { ... };
674             sub method_name : Test(N) { ... };
675
676             # setup methods
677             sub method_name : Test(setup) { ... };
678             sub method_name : Test(setup => N) { ... };
679
680             # teardown methods
681             sub method_name : Test(teardown) { ... };
682             sub method_name : Test(teardown => N) { ... };
683
684             # startup methods
685             sub method_name : Test(startup) { ... };
686             sub method_name : Test(startup => N) { ... };
687
688             # shutdown methods
689             sub method_name : Test(shutdown) { ... };
690             sub method_name : Test(shutdown => N) { ... };
691
692           Marks a startup, setup, test, teardown or shutdown method. See
693           runtests() for information on how to run methods declared with the
694           "Test" attribute.
695
696           N specifies the number of tests the method runs.
697
698           ·   If N is an integer then the method should run exactly N tests.
699
700           ·   If N is an integer with a "+" prefix then the method is
701               expected to call its "SUPER::" method and extend it by running
702               N additional tests.
703
704           ·   If N is the string "no_plan" then the method can run an
705               arbitrary number of tests.
706
707           If N is not specified it defaults to 1 for test methods, and 0 for
708           startup, setup, teardown and shutdown methods.
709
710           You can change the number of tests that a method runs using
711           num_method_tests() or num_tests().
712
713       Tests
714             sub method_name : Tests { ... };
715             sub method_name : Tests(N) { ... };
716
717           Acts just like the ":Test" attribute, except that if the number of
718           tests is not specified it defaults to "no_plan". So the following
719           are equivalent:
720
721             sub silly1 :Test( no_plan ) { ok(1) foreach (1 .. rand 5) };
722             sub silly2 :Tests           { ok(1) foreach (1 .. rand 5) };
723
724       new
725             $Tests = CLASS->new(KEY => VAL ...)
726             $Tests2 = $Tests->new(KEY => VAL ...)
727
728           Creates a new test object (blessed hashref) containing the
729           specified key/value pairs.
730
731           If called as an object method the existing object's key/value pairs
732           are copied into the new object. Any key/value pairs passed to "new"
733           override those in the original object if duplicates occur.
734
735           Since the test object is passed to every test method as it runs it
736           is a convenient place to store test fixtures. For example:
737
738             sub make_fixture : Test(setup) {
739                 my $self = shift;
740                 $self->{object} = Object->new();
741                 $self->{dbh} = Mock::DBI->new(-type => normal);
742             };
743
744             sub test_open : Test {
745                 my $self = shift;
746                 my ($o, $dbh) = ($self->{object}, $self->{dbh});
747                 ok($o->open($dbh), "opened ok");
748             };
749
750           See num_method_tests() for an example of overriding "new".
751
752       expected_tests
753             $n = $Tests->expected_tests
754             $n = CLASS->expected_tests
755             $n = $Tests->expected_tests(TEST, ...)
756             $n = CLASS->expected_tests(TEST, ...)
757
758           Returns the total number of tests that runtests() will run on the
759           specified class/object. This includes tests run by any setup and
760           teardown methods.
761
762           Will return "no_plan" if the exact number of tests is undetermined
763           (i.e. if any setup, test or teardown method has an undetermined
764           number of tests).
765
766           The "expected_tests" of an object after runtests() has been
767           executed will include any run time changes to the expected number
768           of tests made by num_tests() or num_method_tests().
769
770           "expected_tests" can also take an optional list of test objects,
771           test classes and integers. In this case the result is the total
772           number of expected tests for all the test/object classes (including
773           the one the method was applied to) plus any integer values.
774
775           "expected_tests" is useful when you're integrating one or more test
776           classes into a more traditional test script, for example:
777
778             use Test::More;
779             use My::Test::Class;
780
781             plan tests => My::Test::Class->expected_tests(+2);
782
783             ok(whatever, 'a test');
784             ok(whatever, 'another test');
785             My::Test::Class->runtests;
786
787       runtests
788             $allok = $Tests->runtests
789             $allok = CLASS->runtests
790             $allok = $Tests->runtests(TEST, ...)
791             $allok = CLASS->runtests(TEST, ...)
792
793           "runtests" is used to run test classes. At its most basic doing:
794
795             $test->runtests
796
797           will run the test methods of the test object $test, unless
798           "$test->SKIP_CLASS" returns a true value.
799
800           Unless you have already specified a test plan using Test::Builder
801           (or Test::More, et al) "runtests" will set the test plan just
802           before the first method that runs a test is executed.
803
804           If the environment variable "TEST_VERBOSE" is set "runtests" will
805           display the name of each test method before it runs like this:
806
807             # My::Test::Class->my_test
808             ok 1 - fribble
809             # My::Test::Class->another_test
810             ok 2 - bar
811
812           Just like expected_tests(), "runtests" can take an optional list of
813           test object/classes and integers. All of the test object/classes
814           are run. Any integers are added to the total number of tests shown
815           in the test header output by "runtests".
816
817           For example, you can run all the tests in test classes A, B and C,
818           plus one additional normal test by doing:
819
820               Test::Class->runtests(qw(A B C), +1);
821               ok(1==1, 'non class test');
822
823           Finally, if you call "runtests" on a test class without any
824           arguments it will run all of the test methods of that class, and
825           all subclasses of that class. For example:
826
827             #! /usr/bin/perl
828             # Test all the Foo stuff
829
830             use Foo::Test;
831             use Foo::Bar::Test;
832             use Foo::Ni::Test;
833
834             # run all the Foo*Test modules we just loaded
835             Test::Class->runtests;
836
837       SKIP_CLASS
838             $reason = CLASS->SKIP_CLASS;
839             CLASS->SKIP_CLASS( $reason );
840
841           Determines whether the test class CLASS should run it's tests. If
842           SKIP_CLASS returns a true value then  runtests() will not run any
843           of the test methods in CLASS.
844
845           You can override the default on a class-by-class basis by supplying
846           a new value to SKIP_CLASS. For example if you have an abstract base
847           class that should not run just add the following to your module:
848
849             My::Abstract::Test->SKIP_CLASS( 1 );
850
851           This will not affect any sub-classes of "My::Abstract::Test" which
852           will run as normal.
853
854           If the true value returned by SKIP_CLASS is anything other than "1"
855           then a skip test is output using this value as the skip message.
856           For example:
857
858             My::Postgres::Test->SKIP_CLASS(
859                 $ENV{POSTGRES_HOME} ? 0 : '$POSTGRES_HOME needs to be set'
860             );
861
862           will output something like this if "POSTGRES_HOME" is not set
863
864                   ... other tests ...
865                   ok 123 # skip My::Postgres::Test  - $POSTGRES_HOME needs to be set
866                   ... more tests ...
867
868           You can also override SKIP_CLASS for a class hierarchy. For
869           example, to prevent any subclasses of My::Postgres::Test running we
870           could override SKIP_CLASS like this:
871
872             sub My::Postgres::Test::SKIP_CLASS {
873                 $ENV{POSTGRES_HOME} ? 0 : '$POSTGRES_HOME needs to be set'
874             };
875
876   Fetching and setting a method's test number
877       num_method_tests
878             $n = $Tests->num_method_tests($method_name)
879             $Tests->num_method_tests($method_name, $n)
880             $n = CLASS->num_method_tests($method_name)
881             CLASS->num_method_tests($method_name, $n)
882
883           Fetch or set the number of tests that the named method is expected
884           to run.
885
886           If the method has an undetermined number of tests then $n should be
887           the string "no_plan".
888
889           If the method is extending the number of tests run by the method in
890           a superclass then $n should have a "+" prefix.
891
892           When called as a class method any change to the expected number of
893           tests applies to all future test objects. Existing test objects are
894           unaffected.
895
896           When called as an object method any change to the expected number
897           of tests applies to that object alone.
898
899           "num_method_tests" is useful when you need to set the expected
900           number of tests at object creation time, rather than at compile
901           time.
902
903           For example, the following test class will run a different number
904           of tests depending on the number of objects supplied.
905
906             package Object::Test;
907             use base qw(Test::Class);
908             use Test::More;
909
910             sub new {
911                 my $class = shift;
912                 my $self = $class->SUPER::new(@_);
913                 my $num_objects = @{$self->{objects}};
914                 $self->num_method_tests('test_objects', $num_objects);
915                 return($self);
916             };
917
918             sub test_objects : Tests {
919               my $self = shift;
920               ok($_->open, "opened $_") foreach @{$self->{objects}};
921             };
922             ...
923             # This runs two tests
924             Object::Test->new(objects => [$o1, $o2]);
925
926           The advantage of setting the number of tests at object creation
927           time, rather than using a test method without a plan, is that the
928           number of expected tests can be determined before testing begins.
929           This allows better diagnostics from runtests(), Test::Builder and
930           Test::Harness.
931
932           "num_method_tests" is a protected method and can only be called by
933           subclasses of Test::Class. It fetches or sets the expected number
934           of tests for the methods of the class it was called in, not the
935           methods of the object/class it was applied to. This allows test
936           classes that use "num_method_tests" to be subclassed easily.
937
938           For example, consider the creation of a subclass of Object::Test
939           that ensures that all the opened objects are read-only:
940
941             package Special::Object::Test;
942             use base qw(Object::Test);
943             use Test::More;
944
945             sub test_objects : Test(+1) {
946                 my $self = shift;
947                 $self->SUPER::test_objects;
948                 my @bad_objects = grep {! $_->read_only} (@{$self->{objects}});
949                 ok(@bad_objects == 0, "all objects read only");
950             };
951             ...
952             # This runs three tests
953             Special::Object::Test->new(objects => [$o1, $o2]);
954
955           Since the call to "num_method_tests" in Object::Test only affects
956           the "test_objects" of Object::Test, the above works as you would
957           expect.
958
959       num_tests
960             $n = $Tests->num_tests
961             $Tests->num_tests($n)
962             $n = CLASS->num_tests
963             CLASS->num_tests($n)
964
965           Set or return the number of expected tests associated with the
966           currently running test method. This is the same as calling
967           num_method_tests() with a method name of current_method().
968
969           For example:
970
971             sub txt_files_readable : Tests {
972                 my $self = shift;
973                 my @files = <*.txt>;
974                 $self->num_tests(scalar(@files));
975                 ok(-r $_, "$_ readable") foreach (@files);
976             };
977
978           Setting the number of expected tests at run time, rather than just
979           having a "no_plan" test method, allows runtests() to display
980           appropriate diagnostic messages if the method runs a different
981           number of tests.
982
983   Support methods
984       builder
985             $Tests->builder
986
987           Returns the underlying Test::Builder object that Test::Class uses.
988           For example:
989
990             sub test_close : Test {
991                 my $self = shift;
992                 my ($o, $dbh) = ($self->{object}, $self->{dbh});
993                 $self->builder->ok($o->close($dbh), "closed ok");
994             };
995
996       current_method
997             $method_name = $Tests->current_method
998             $method_name = CLASS->current_method
999
1000           Returns the name of the test method currently being executed by
1001           runtests(), or "undef" if runtests() has not been called.
1002
1003           The method name is also available in the setup and teardown methods
1004           that run before and after the test method. This can be useful in
1005           producing diagnostic messages, for example:
1006
1007             sub test_invarient : Test(teardown => 1) {
1008                 my $self = shift;
1009                 my $m = $self->current_method;
1010                 ok($self->invarient_ok, "class okay after $m");
1011             };
1012
1013       BAILOUT
1014             $Tests->BAILOUT($reason)
1015             CLASS->BAILOUT($reason)
1016
1017           Things are going so badly all testing should terminate, including
1018           running any additional test scripts invoked by Test::Harness. This
1019           is exactly the same as doing:
1020
1021             $self->builder->BAILOUT
1022
1023           See "BAILOUT" in Test::Builder for details. Any teardown and
1024           shutdown methods are not run.
1025
1026       FAIL_ALL
1027             $Tests->FAIL_ALL($reason)
1028             CLASS->FAIL_ALL($reason)
1029
1030           Things are going so badly all the remaining tests in the current
1031           script should fail. Exits immediately with the number of tests
1032           failed, or 254 if more than 254 tests were run. Any teardown
1033           methods are not run.
1034
1035           This does not affect the running of any other test scripts invoked
1036           by Test::Harness.
1037
1038           For example, if all your tests rely on the ability to create
1039           objects then you might want something like this as an early test:
1040
1041             sub _test_new : Test(3) {
1042                 my $self = shift;
1043                 isa_ok(Object->new, "Object")
1044                     || $self->FAIL_ALL('cannot create Objects');
1045                 ...
1046             };
1047
1048       SKIP_ALL
1049             $Tests->SKIP_ALL($reason)
1050             CLASS->SKIP_ALL($reason)
1051
1052           Things are going so badly all the remaining tests in the current
1053           script should be skipped. Exits immediately with 0 - teardown
1054           methods are not run.
1055
1056           This does not affect the running of any other test scripts invoked
1057           by Test::Harness.
1058
1059           For example, if you had a test script that only applied to the
1060           darwin OS you could write:
1061
1062             sub _darwin_only : Test(setup) {
1063                 my $self = shift;
1064                 $self->SKIP_ALL("darwin only") unless $^O eq "darwin";
1065             };
1066
1067       add_testinfo
1068             CLASS->add_test($name, $type, $num_tests)
1069
1070           Chiefly for use by libraries like Test::Class::Sugar, which can't
1071           use the ":Test(...)" interfaces make test methods. "add_testinfo"
1072           informs the class about a test method that has been defined without
1073           a "Test", "Tests" or other attribute.
1074
1075           $name is the name of the method, $type must be one of "startup",
1076           "setup", "test", "teardown" or "shutdown", and $num_tests has the
1077           same meaning as "N" in the description of the Test attribute.
1078

HELP FOR CONFUSED JUNIT USERS

1080       This section is for people who have used JUnit (or similar) and are
1081       confused because they don't see the TestCase/Suite/Runner class
1082       framework they were expecting. Here we take each of the major classes
1083       in JUnit and compare them with their equivalent Perl testing modules.
1084
1085       Class Assert
1086           The test assertions provided by Assert correspond to the test
1087           functions provided by the Test::Builder based modules (Test::More,
1088           Test::Exception, Test::Differences, etc.)
1089
1090           Unlike JUnit the test functions supplied by Test::More et al do not
1091           throw exceptions on failure. They just report the failure to STDOUT
1092           where it is collected by Test::Harness. This means that where you
1093           have
1094
1095             sub foo : Test(2) {
1096                 ok($foo->method1);
1097                 ok($foo->method2);
1098             };
1099
1100           The second test will run if the first one fails. You can emulate
1101           the JUnit way of doing it by throwing an explicit exception on test
1102           failure:
1103
1104             sub foo : Test(2) {
1105                 ok($foo->method1) or die "method1 failed";
1106                 ok($foo->method2);
1107             };
1108
1109           The exception will be caught by Test::Class and the other test
1110           automatically failed.
1111
1112       Class TestCase
1113           Test::Class corresponds to TestCase in JUnit.
1114
1115           In Test::Class setup, test and teardown methods are marked
1116           explicitly using the Test attribute. Since we need to know the
1117           total number of tests to provide a test plan for Test::Harness we
1118           also state how many tests each method runs.
1119
1120           Unlike JUnit you can have multiple setup/teardown methods in a
1121           class.
1122
1123       Class TestSuite
1124           Test::Class also does the work that would be done by TestSuite in
1125           JUnit.
1126
1127           Since the methods are marked with attributes Test::Class knows what
1128           is and isn't a test method. This allows it to run all the test
1129           methods without having the developer create a suite manually, or
1130           use reflection to dynamically determine the test methods by name.
1131           See the runtests() method for more details.
1132
1133           The running order of the test methods is fixed in Test::Class.
1134           Methods are executed in alphabetical order.
1135
1136           Unlike JUnit, Test::Class currently does not allow you to run
1137           individual test methods.
1138
1139       Class TestRunner
1140           Test::Harness does the work of the TestRunner in JUnit. It collects
1141           the test results (sent to STDOUT) and collates the results.
1142
1143           Unlike JUnit there is no distinction made by Test::Harness between
1144           errors and failures. However, it does support skipped and todo test
1145           - which JUnit does not.
1146
1147           If you want to write your own test runners you should look at
1148           Test::Harness::Straps.
1149

OTHER MODULES FOR XUNIT TESTING IN PERL

1151       In addition to Test::Class there are two other distributions for xUnit
1152       testing in perl. Both have a longer history than Test::Class and might
1153       be more suitable for your needs.
1154
1155       I am biased since I wrote Test::Class - so please read the following
1156       with appropriate levels of scepticism. If you think I have
1157       misrepresented the modules please let me know.
1158
1159       Test::SimpleUnit
1160           A very simple unit testing framework. If you are looking for a
1161           lightweight single module solution this might be for you.
1162
1163           The advantage of Test::SimpleUnit is that it is simple! Just one
1164           module with a smallish API to learn.
1165
1166           Of course this is also the disadvantage.
1167
1168           It's not class based so you cannot create testing classes to reuse
1169           and extend.
1170
1171           It doesn't use Test::Builder so it's difficult to extend or
1172           integrate with other testing modules. If you are already familiar
1173           with Test::Builder, Test::More and friends you will have to learn a
1174           new test assertion API. It does not support todo tests.
1175
1176       Test::Unit
1177           Test::Unit is a port of JUnit <http://www.junit.org/> into perl. If
1178           you have used JUnit then the Test::Unit framework should be very
1179           familiar.
1180
1181           It is class based so you can easily reuse your test classes and
1182           extend by subclassing. You get a nice flexible framework you can
1183           tweak to your heart's content. If you can run Tk you also get a
1184           graphical test runner.
1185
1186           However, Test::Unit is not based on Test::Builder. You cannot
1187           easily move Test::Builder based test functions into Test::Unit
1188           based classes. You have to learn another test assertion API.
1189
1190           Test::Unit implements it's own testing framework separate from
1191           Test::Harness. You can retrofit *.t scripts as unit tests, and
1192           output test results in the format that Test::Harness expects, but
1193           things like todo tests and skipping tests are not supported.
1194

BUGS

1196       None known at the time of writing.
1197
1198       If you find any bugs please let me know by e-mail at
1199       <adrianh@quietstars.com>, or report the problem with
1200       http://rt.cpan.org/NoAuth/Bugs.html?Dist=Test-Class
1201       <http://rt.cpan.org/NoAuth/Bugs.html?Dist=Test-Class>.
1202

COMMUNITY

1204   perl-qa
1205       If you are interested in testing using Perl I recommend you visit
1206       <http://qa.perl.org/> and join the excellent perl-qa mailing list. See
1207       http://lists.perl.org/showlist.cgi?name=perl-qa
1208       <http://lists.perl.org/showlist.cgi?name=perl-qa> for details on how to
1209       subscribe.
1210
1211   perlmonks
1212       You can find users of Test::Class, including the module author, on
1213       <http://www.perlmonks.org/>. Feel free to ask questions on Test::Class
1214       there.
1215
1216   CPAN::Forum
1217       The CPAN Forum is a web forum for discussing Perl's CPAN modules.   The
1218       Test::Class forum can be found at
1219       http://www.cpanforum.com/dist/Test-Class
1220       <http://www.cpanforum.com/dist/Test-Class>.
1221

TO DO

1223       If you think this module should do something that it doesn't (or does
1224       something that it shouldn't) please let me know.
1225
1226       You can see my current to do list at
1227       <http://adrianh.tadalist.com/lists/public/4798>, with an RSS feed of
1228       changes at <http://adrianh.tadalist.com/lists/feed_public/4798>.
1229

ACKNOWLEDGMENTS

1231       This is yet another implementation of the ideas from Kent Beck's
1232       Testing Framework paper <http://www.xprogramming.com/testfram.htm>.
1233
1234       Thanks to Adam Kennedy, agianni, Apocalypse, Ask Bjorn Hansen, Chris
1235       Dolan, Chris Williams, Corion, Cosimo Streppone, Daniel Berger, Dave
1236       O'Neill, David Cantrell, David Wheeler, Emil Jansson, Gunnar Wolf, Hai
1237       Pham, Hynek, imacat, Jeff Deifik, Jim Brandt, Jochen Stenzel, Johan
1238       Lindstrom, John West, Jonathan R. Warden, Joshua ben Jore, Jost
1239       Krieger, Kenichi Ishigaki Lee Goddard, Mark Reynolds, Mark Stosberg,
1240       Martin Ferrari, Mathieu Sauve-Frankel, Matt Trout, Matt Williamson,
1241       Michael G Schwern, Murat Uenalan, Nicholas Clark, Ovid, Piers Cawley,
1242       Rob Kinyon, Scott Lanning, Sebastien Aperghis-Tramoni, Steve Kirkup,
1243       Stray Toaster, Ted Carnahan, Terrence Brannon, Tom Metro, Tony Bowden,
1244       Tony Edwardson, William McKee, various anonymous folk and all the fine
1245       people on perl-qa for their feedback, patches, suggestions and nagging.
1246
1247       This module wouldn't be possible without the excellent Test::Builder.
1248       Thanks to chromatic and Michael G Schwern for creating such a useful
1249       module.
1250

AUTHOR

1252       Adrian Howard <adrianh@quietstars.com>
1253
1254       If you use this module, and can spare the time please drop me an e-mail
1255       or rate it at http://cpanratings.perl.org/rate/?distribution=Test-Class
1256       <http://cpanratings.perl.org/rate/?distribution=Test-Class>.
1257

SEE ALSO

1259       Test::Class::Load
1260           Simple way to load "Test::Class" classes automatically.
1261
1262       <http://del.icio.us/tag/Test::Class>
1263           Delicious links on Test::Class.
1264
1265       Perl Testing: A Developer's Notebook by Ian Langworth and chromatic
1266           Chapter 8 covers using Test::Class.
1267
1268       Advanced Perl Programming, second edition by Simon Cozens
1269           Chapter 8 has a few pages on using Test::Class.
1270
1271       The Perl Journal, April 2003
1272           Includes the article "Test-Driven Development in Perl" by Piers
1273           Cawley that uses Test::Class.
1274
1275       Test::Builder
1276           Support module for building test libraries.
1277
1278       Test::Simple & Test::More
1279           Basic utilities for writing tests.
1280
1281       http://qa.perl.org/test-modules.html <http://qa.perl.org/test-
1282       modules.html>
1283           Overview of some of the many testing modules available on CPAN.
1284
1285       <http://del.icio.us/tag/perl+testing>
1286           Delicious links on perl testing.
1287
1288       Test::Object
1289           Another approach to object oriented testing.
1290
1291       Test::Group and Test::Block
1292           Alternatives to grouping sets of tests together.
1293
1294       The following modules use Test::Class as part of their test suite. You
1295       might want to look at them for usage examples:
1296
1297           Aspect, Bricolage (<http://www.bricolage.cc/>),
1298           Class::StorageFactory, CGI::Application::Search, DBIx::Romani,
1299           Xmldoom, Object::Relational, File::Random,
1300           Geography::JapanesePrefectures, Google::Adwords, Merge::HashRef,
1301           PerlBuildSystem, Pixie, Yahoo::Marketing, and XUL-Node
1302
1303       The following modules are not based on Test::Builder, but may be of
1304       interest as alternatives to Test::Class.
1305
1306       Test::Unit
1307           Perl unit testing framework closely modeled on JUnit.
1308
1309       Test::SimpleUnit
1310           A very simple unit testing framework.
1311

LICENCE

1313       Copyright 2002-2009 Adrian Howard, All Rights Reserved.
1314
1315       This program is free software; you can redistribute it and/or modify it
1316       under the same terms as Perl itself.
1317
1318
1319
1320perl v5.12.0                      2010-05-11                    Test::Class(3)
Impressum