12 my(@Test_Results) = ();
13 my($Num_Tests, $Planned_Tests, $Test_Died) = (0,0,0);
16 my $IsVMS = $^O eq 'VMS';
19 # I'd like to have Test::Simple interfere with the program being
20 # tested as little as possible. This includes using Exporter or
21 # anything else (including strict).
25 if( $_[1] eq 'no_plan' ) {
35 my($class, %config) = @_;
37 if( !exists $config{tests} ) {
38 die "You have to tell $class how many tests you plan to run.\n".
39 " use $class tests => 42; for example.\n";
41 elsif( !defined $config{tests} ) {
42 die "Got an undefined number of tests. Looks like you tried to tell ".
43 "$class how many tests you plan to run but made a mistake.\n";
45 elsif( !$config{tests} ) {
46 die "You told $class you plan to run 0 tests! You've got to run ".
50 $Planned_Tests = $config{tests};
55 my_print *TESTOUT, "1..$Planned_Tests\n";
59 *{$caller.'::ok'} = \&ok;
69 *{$caller.'::ok'} = \&ok;
75 open(*TESTOUT, ">&STDOUT") or _whoa(1, "Can't dup STDOUT!");
76 open(*TESTERR, ">&STDOUT") or _whoa(1, "Can't dup STDOUT!");
78 my $orig_fh = select TESTOUT;
87 Test::Simple - Basic utilities for writing tests.
91 use Test::Simple tests => 1;
93 ok( $foo eq $bar, 'foo is bar' );
98 ** If you are unfamiliar with testing B<read Test::Tutorial> first! **
100 This is an extremely simple, extremely basic module for writing tests
101 suitable for CPAN modules and other pursuits. If you wish to do more
102 complicated testing, use the Test::More module (a drop-in replacement
105 The basic unit of Perl testing is the ok. For each thing you want to
106 test your program will print out an "ok" or "not ok" to indicate pass
107 or fail. You do this with the ok() function (see below).
109 The only other constraint is you must predeclare how many tests you
110 plan to run. This is in case something goes horribly wrong during the
111 test and your test program aborts, or skips a test or whatever. You
114 use Test::Simple tests => 23;
116 You must have a plan.
123 ok( $foo eq $bar, $name );
126 ok() is given an expression (in this case C<$foo eq $bar>). If its
127 true, the test passed. If its false, it didn't. That's about it.
129 ok() prints out either "ok" or "not ok" along with a test number (it
130 keeps track of that for you).
132 # This produces "ok 1 - Hell not yet frozen over" (or not ok)
133 ok( get_temperature($hell) > 0, 'Hell not yet frozen over' );
135 If you provide a $name, that will be printed along with the "ok/not
136 ok" to make it easier to find your test when if fails (just search for
137 the name). It also makes it easier for the next guy to understand
138 what your test is for. Its highly recommended you use test names.
140 All tests are run in scalar context. So this:
142 ok( @stuff, 'I have some stuff' );
144 will do what you mean (fail if stuff is empty)
149 my($test, $name) = @_;
151 unless( $Have_Plan ) {
152 die "You tried to use ok() without a plan! Gotta have a plan.\n".
153 " use Test::Simple tests => 23; for example.\n";
158 my_print *TESTERR, <<ERR if defined $name and $name =~ /^[\d\s]+$/;
159 You named your test '$name'. You shouldn't use numbers for your test names.
164 my($pack, $file, $line) = caller;
165 # temporary special case for Test::More & Parrot::Test's calls.
166 if( $pack eq 'Test::More' || $pack eq 'Parrot::Test' ) {
167 ($pack, $file, $line) = caller(1);
170 my($is_todo) = ${$pack.'::TODO'} ? 1 : 0;
172 # We must print this all in one shot or else it will break on VMS
176 $Test_Results[$Num_Tests-1] = $is_todo ? 1 : 0;
179 $Test_Results[$Num_Tests-1] = 1;
181 $msg .= "ok $Num_Tests";
183 if( defined $name ) {
184 $name =~ s|#|\\#|g; # # in a name can confuse Test::Harness.
188 my $what_todo = ${$pack.'::TODO'};
189 $msg .= " # TODO $what_todo";
193 my_print *TESTOUT, $msg;
197 my $msg = $is_todo ? "Failed (TODO)" : "Failed";
198 my_print *TESTERR, "# $msg test ($file at line $line)\n";
201 return $test ? 1 : 0;
208 unless( $Have_Plan ) {
209 die "You tried to use ok() without a plan! Gotta have a plan.\n".
210 " use Test::Simple tests => 23; for example.\n";
215 # XXX Set this to "Skip" instead?
216 $Test_Results[$Num_Tests-1] = 1;
218 # We must print this all in one shot or else it will break on VMS
220 $msg .= "ok $Num_Tests # skip $why\n";
222 my_print *TESTOUT, $msg;
230 Test::Simple will start by printing number of tests run in the form
231 "1..M" (so "1..5" means you're going to run 5 tests). This strange
232 format lets Test::Harness know how many tests you plan on running in
233 case something goes horribly wrong.
235 If all your tests passed, Test::Simple will exit with zero (which is
236 normal). If anything failed it will exit with how many failed. If
237 you run less (or more) tests than you planned, the missing (or extras)
238 will be considered failures. If no tests were ever run Test::Simple
239 will throw a warning and exit with 255. If the test died, even after
240 having successfully completed all its tests, it will still be
241 considered a failure and will exit with 255.
243 So the exit codes are...
245 0 all tests successful
247 any other number how many failed (including missing or extras)
249 If you fail more than 254 tests, it will be reported as 254.
255 =item B<_sanity_check>
259 Runs a bunch of end of test sanity checks to make sure reality came
260 through ok. If anything is wrong it will die with a fairly friendly
267 _whoa($Num_Tests < 0, 'Says here you ran a negative number of tests!');
268 _whoa(!$Have_Plan and $Num_Tests,
269 'Somehow your tests ran without a plan!');
270 _whoa($Num_Tests != @Test_Results,
271 'Somehow you got a different number of results than tests ran!');
276 _whoa($check, $description);
278 A sanity check, similar to assert(). If the $check is true, something
279 has gone horribly wrong. It will die with the given $description and
280 a note to contact the author.
285 my($check, $desc) = @_;
289 This should never happen! Please contact the author immediately!
298 Perl seems to have some trouble with exiting inside an END block. 5.005_03
299 and 5.6.1 both seem to do odd things. Instead, this function edits $?
300 directly. It should ONLY be called from inside an END block. It
301 doesn't actually exit, that's your job.
318 $SIG{__DIE__} = sub {
319 # We don't want to muck with death in an eval, but $^S isn't
320 # totally reliable. 5.005_03 and 5.6.1 both do the wrong thing
321 # with it. Instead, we use caller. This also means it runs under
324 for( my $stack = 1; my $sub = (caller($stack))[3]; $stack++ ) {
325 $in_eval = 1 if $sub =~ /^\(eval\)/;
327 $Test_Died = 1 unless $in_eval;
333 # Bailout if import() was never called. This is so
334 # "require Test::Simple" doesn't puke.
335 do{ _my_exit(0) && return } if !$Have_Plan and !$Num_Tests;
337 # Figure out if we passed or failed and print helpful messages.
339 # The plan? We have no plan.
340 unless( $Planned_Tests ) {
341 my_print *TESTOUT, "1..$Num_Tests\n";
342 $Planned_Tests = $Num_Tests;
345 my $num_failed = grep !$_, @Test_Results[0..$Planned_Tests-1];
346 $num_failed += abs($Planned_Tests - @Test_Results);
348 if( $Num_Tests < $Planned_Tests ) {
349 my_print *TESTERR, <<"FAIL";
350 # Looks like you planned $Planned_Tests tests but only ran $Num_Tests.
353 elsif( $Num_Tests > $Planned_Tests ) {
354 my $num_extra = $Num_Tests - $Planned_Tests;
355 my_print *TESTERR, <<"FAIL";
356 # Looks like you planned $Planned_Tests tests but ran $num_extra extra.
359 elsif ( $num_failed ) {
360 my_print *TESTERR, <<"FAIL";
361 # Looks like you failed $num_failed tests of $Planned_Tests.
366 my_print *TESTERR, <<"FAIL";
367 # Looks like your test died just after $Num_Tests.
370 _my_exit( 255 ) && return;
373 _my_exit( $num_failed <= 254 ? $num_failed : 254 ) && return;
375 elsif ( $Test::Simple::Skip_All ) {
376 _my_exit( 0 ) && return;
379 my_print *TESTERR, "# No tests run!\n";
380 _my_exit( 255 ) && return;
387 This module is by no means trying to be a complete testing system.
388 Its just to get you started. Once you're off the ground its
389 recommended you look at L<Test::More>.
394 Here's an example of a simple .t file for the fictional Film module.
396 use Test::Simple tests => 5;
398 use Film; # What you're testing.
400 my $btaste = Film->new({ Title => 'Bad Taste',
401 Director => 'Peter Jackson',
403 NumExplodingSheep => 1
405 ok( defined($btaste) and ref $btaste eq 'Film', 'new() works' );
407 ok( $btaste->Title eq 'Bad Taste', 'Title() get' );
408 ok( $btaste->Director eq 'Peter Jackson', 'Director() get' );
409 ok( $btaste->Rating eq 'R', 'Rating() get' );
410 ok( $btaste->NumExplodingSheep == 1, 'NumExplodingSheep() get' );
412 It will produce output like this:
417 ok 3 - Director() get
418 not ok 4 - Rating() get
419 # Failed test (t/film.t at line 14)
420 ok 5 - NumExplodingSheep() get
421 # Looks like you failed 1 tests of 5
423 Indicating the Film::Rating() method is broken.
428 Test::Simple will only report a maximum of 254 failures in its exit
429 code. If this is a problem, you probably have a huge test script.
430 Split it into multiple files. (Otherwise blame the Unix folks for
431 using an unsigned short integer as the exit status).
433 Because VMS's exit codes are much, much different than the rest of the
434 universe, and perl does horrible mangling to them that gets in my way,
435 it works like this on VMS.
437 0 SS$_NORMAL all tests successful
438 4 SS$_ABORT something went wrong
440 Unfortunately, I can't differentiate any further.
445 Test::Simple is B<explicitly> tested all the way back to perl 5.004.
450 This module was conceived while talking with Tony Bowden in his
451 kitchen one night about the problems I was having writing some really
452 complicated feature into the new Testing module. He observed that the
453 main problem is not dealing with these edge cases but that people hate
454 to write tests B<at all>. What was needed was a dead simple module
455 that took all the hard work out of testing and was really, really easy
456 to learn. Paul Johnson simultaneously had this idea (unfortunately,
457 he wasn't in Tony's kitchen). This is it.
462 Idea by Tony Bowden and Paul Johnson, code by Michael G Schwern
463 E<lt>schwern@pobox.comE<gt>, wardrobe by Calvin Klein.
472 More testing functions! Once you outgrow Test::Simple, look at
473 Test::More. Test::Simple is 100% forward compatible with Test::More
474 (ie. you can just use Test::More instead of Test::Simple in your
475 programs and things will still work).
479 The original Perl testing module.
483 Elaborate unit testing.
485 =item L<Pod::Tests>, L<SelfTest>
487 Embed tests in your code!
489 =item L<Test::Harness>
491 Interprets the output of your test program.