4 # Time-stamp: "2002-08-26 03:09:51 MDT"
9 use vars (qw($VERSION @ISA @EXPORT @EXPORT_OK $ntest $TestLevel), #public-ish
10 qw($TESTOUT $TESTERR %Program_Lines
11 $ONFAIL %todo %history $planned @FAILDETAIL) #private-ish
14 # In case a test is run in a persistent environment.
20 $TestLevel = 0; # how many extra stack frames to skip
28 @EXPORT = qw(&plan &ok &skip);
29 @EXPORT_OK = qw($ntest $TESTOUT $TESTERR);
32 $TESTOUT = *STDOUT{IO};
33 $TESTERR = *STDERR{IO};
35 # Use of this variable is strongly discouraged. It is set mainly to
36 # help test coverage analyzers know which test is running.
37 $ENV{REGRESSION_TEST} = $0;
42 Test - provides a simple framework for writing test scripts
49 # use a BEGIN block so we print our plan before MyModule is loaded
50 BEGIN { plan tests => 14, todo => [3,4] }
55 # Helpful notes. All note-lines must start with a "#".
56 print "# I'm testing MyModule version $MyModule::VERSION\n";
61 ok(0); # ok, expected failure (see todo list, above)
62 ok(1); # surprise success!
64 ok(0,1); # failure: '0' ne '1'
65 ok('broke','fixed'); # failure: 'broke' ne 'fixed'
66 ok('fixed','fixed'); # success: 'fixed' eq 'fixed'
67 ok('fixed',qr/x/); # success: 'fixed' =~ qr/x/
69 ok(sub { 1+1 }, 2); # success: '2' eq '2'
70 ok(sub { 1+1 }, 3); # failure: '2' ne '3'
73 ok @list, 3, "\@list=".join(',',@list); #extra notes
74 ok 'segmentation fault', '/(?i)success/'; #regex match
77 $^O eq 'MSWin' ? "Not for MSWin" : 0, # whether to skip
78 $foo, $bar # arguments just like for ok(...)
83 This module simplifies the task of writing test files for Perl modules,
84 such that their output is in the format that
85 L<Test::Harness|Test::Harness> expects to see.
87 =head1 QUICK START GUIDE
89 To write a test for your new (and probably not even done) module, create
90 a new file called F<t/test.t> (in a new F<t> directory). If you have
91 multiple test files, to test the "foo", "bar", and "baz" feature sets,
92 then feel free to call your files F<t/foo.t>, F<t/bar.t>, and
97 This module defines three public functions, C<plan(...)>, C<ok(...)>,
98 and C<skip(...)>. By default, all three are exported by
99 the C<use Test;> statement.
105 BEGIN { plan %theplan; }
107 This should be the first thing you call in your test script. It
108 declares your testing plan, how many there will be, if any of them
109 should be allowed to fail, and so on.
111 Typical usage is just:
114 BEGIN { plan tests => 23 }
116 These are the things that you can put in the parameters to plan:
120 =item C<tests =E<gt> I<number>>
122 The number of tests in your script.
123 This means all ok() and skip() calls.
125 =item C<todo =E<gt> [I<1,5,14>]>
127 A reference to a list of tests which are allowed to fail.
130 =item C<onfail =E<gt> sub { ... }>
132 =item C<onfail =E<gt> \&some_sub>
134 A subroutine reference to be run at the end of the test script, if
135 any of the tests fail. See L</ONFAIL>.
139 You must call C<plan(...)> once and only once. You should call it
140 in a C<BEGIN {...}> block, like so:
142 BEGIN { plan tests => 23 }
147 croak "Test::plan(%args): odd number of arguments" if @_ & 1;
148 croak "Test::plan(): should not be called more than once" if $planned;
150 local($\, $,); # guard against -l and other things that screw with
155 _read_program( (caller)[1] );
158 for (my $x=0; $x < @_; $x+=2) {
159 my ($k,$v) = @_[$x,$x+1];
160 if ($k =~ /^test(s)?$/) { $max = $v; }
161 elsif ($k eq 'todo' or
162 $k eq 'failok') { for (@$v) { $todo{$_}=1; }; }
163 elsif ($k eq 'onfail') {
164 ref $v eq 'CODE' or croak "Test::plan(onfail => $v): must be CODE";
167 else { carp "Test::plan(): skipping unrecognized directive '$k'" }
169 my @todo = sort { $a <=> $b } keys %todo;
171 print $TESTOUT "1..$max todo ".join(' ', @todo).";\n";
173 print $TESTOUT "1..$max\n";
176 print $TESTOUT "# Running under perl version $] for $^O",
177 (chr(65) eq 'A') ? "\n" : " in a non-ASCII world\n";
179 print $TESTOUT "# Win32::BuildNumber ", &Win32::BuildNumber(), "\n"
180 if defined(&Win32::BuildNumber) and defined &Win32::BuildNumber();
182 print $TESTOUT "# MacPerl verison $MacPerl::Version\n"
183 if defined $MacPerl::Version;
186 "# Current time local: %s\n# Current time GMT: %s\n",
187 scalar( gmtime($^T)), scalar(localtime($^T));
189 print $TESTOUT "# Using Test.pm version $VERSION\n";
197 return unless defined $file and length $file
198 and -e $file and -f _ and -r _;
199 open(SOURCEFILE, "<$file") || return;
200 $Program_Lines{$file} = [<SOURCEFILE>];
203 foreach my $x (@{$Program_Lines{$file}})
204 { $x =~ tr/[\cm\cj\n\r]//d }
206 unshift @{$Program_Lines{$file}}, '';
214 my $value = _to_value($input);
216 Converts an C<ok> parameter to its value. Typically this just means
217 running it, if it's a code reference. You should run all inputted
224 return (ref $v or '') eq 'CODE' ? $v->() : $v;
233 ok($have, $expect, $diagnostics);
235 This function is the reason for C<Test>'s existence. It's
236 the basic function that
237 handles printing "C<ok>" or "C<not ok>", along with the
238 current test number. (That's what C<Test::Harness> wants to see.)
240 In its most basic usage, C<ok(...)> simply takes a single scalar
241 expression. If its value is true, the test passes; if false,
242 the test fails. Examples:
244 # Examples of ok(scalar)
246 ok( 1 + 1 == 2 ); # ok if 1 + 1 == 2
247 ok( $foo =~ /bar/ ); # ok if $foo contains 'bar'
248 ok( baz($x + $y) eq 'Armondo' ); # ok if baz($x + $y) returns
250 ok( @a == @b ); # ok if @a and @b are the same length
252 The expression is evaluated in scalar context. So the following will
255 ok( @stuff ); # ok if @stuff has any elements
256 ok( !grep !defined $_, @stuff ); # ok if everything in @stuff is
259 A special case is if the expression is a subroutine reference (in either
260 C<sub {...}> syntax or C<\&foo> syntax). In
261 that case, it is executed and its value (true or false) determines if
262 the test passes or fails. For example,
264 ok( sub { # See whether sleep works at least passably
265 my $start_time = time;
267 time() - $start_time >= 4
270 In its two-argument form, C<ok(I<arg1>,I<arg2>)> compares the two scalar
271 values to see if they equal. (The equality is checked with C<eq>).
273 # Example of ok(scalar, scalar)
275 ok( "this", "that" ); # not ok, 'this' ne 'that'
277 If either (or both!) is a subroutine reference, it is run and used
278 as the value for comparing. For example:
281 open(OUT, ">x.dat") || die $!;
282 print OUT "\x{e000}";
284 my $bytecount = -s 'x.dat';
285 unlink 'x.dat' or warn "Can't unlink : $!";
290 The above test passes two values to C<ok(arg1, arg2)> -- the first is
291 the number 4, and the second is a coderef. Before C<ok> compares them,
292 it calls the coderef, and uses its return value as the real value of
293 this parameter. Assuming that C<$bytecount> returns 4, C<ok> ends up
294 testing C<4 eq 4>. Since that's true, this test passes.
296 If C<arg2> is either a regex object (i.e., C<qr/.../>) or a string
297 that I<looks like> a regex (e.g., C<'/foo/'>), then
298 C<ok(I<arg1>,I<arg2>)> will perform a pattern
299 match against it, instead of using C<eq>.
301 ok( 'JaffO', '/Jaff/' ); # ok, 'JaffO' =~ /Jaff/
302 ok( 'JaffO', qr/Jaff/ ); # ok, 'JaffO' =~ qr/Jaff/;
303 ok( 'JaffO', '/(?i)jaff/ ); # ok, 'JaffO' =~ /jaff/i;
305 Finally, you can append an optional third argument, in
306 C<ok(I<arg1>,I<arg2>, I<note>)>, where I<note> is a string value that
307 will be printed if the test fails. This should be some useful
308 information about the test, pertaining to why it failed, and/or
309 a description of the test. For example:
311 ok( grep($_ eq 'something unique', @stuff), 1,
312 "Something that should be unique isn't!\n".
313 '@stuff = '.join ', ', @stuff
316 Unfortunately, a note cannot be used with the single argument
317 style of C<ok()>. That is, if you try C<ok(I<arg1>, I<note>)>, then
318 C<Test> will interpret this as C<ok(I<arg1>, I<arg2>)>, and probably
319 end up testing C<I<arg1> eq I<arg2>> -- and that's not what you want!
321 All of the above special cases can occasionally cause some
322 problems. See L</BUGS and CAVEATS>.
326 # A past maintainer of this module said:
327 # <<ok(...)'s special handling of subroutine references is an unfortunate
328 # "feature" that can't be removed due to compatibility.>>
332 croak "ok: plan before you test!" if !$planned;
334 local($\,$,); # guard against -l and other things that screw with
337 my ($pkg,$file,$line) = caller($TestLevel);
338 my $repetition = ++$history{"$file:$line"};
339 my $context = ("$file at line $line".
340 ($repetition > 1 ? " fail \#$repetition" : ''));
343 my $result = _to_value(shift);
344 my ($expected,$diag,$isregex,$regex);
348 $expected = _to_value(shift);
349 if (!defined $expected) {
350 $ok = !defined $result;
351 } elsif (!defined $result) {
353 } elsif ((ref($expected)||'') eq 'Regexp') {
354 $ok = $result =~ /$expected/;
356 } elsif (($regex) = ($expected =~ m,^ / (.+) / $,sx) or
357 (undef, $regex) = ($expected =~ m,^ m([^\w\s]) (.+) \1 $,sx)) {
358 $ok = $result =~ /$regex/;
360 $ok = $result eq $expected;
363 my $todo = $todo{$ntest};
365 $context .= ' TODO?!' if $todo;
366 print $TESTOUT "ok $ntest # ($context)\n";
368 # Issuing two seperate prints() causes problems on VMS.
370 print $TESTOUT "not ok $ntest\n";
373 print $TESTOUT "ok $ntest\n";
377 my $detail = { 'repetition' => $repetition, 'package' => $pkg,
378 'result' => $result, 'todo' => $todo };
379 $$detail{expected} = $expected if defined $expected;
381 # Get the user's diagnostic, protecting against multi-line
383 $diag = $$detail{diagnostic} = _to_value(shift) if @_;
384 $diag =~ s/\n/\n#/g if defined $diag;
386 $context .= ' *TODO*' if $todo;
387 if (!defined $expected) {
389 print $TESTERR "# Failed test $ntest in $context\n";
391 print $TESTERR "# Failed test $ntest in $context: $diag\n";
394 my $prefix = "Test $ntest";
395 print $TESTERR "# $prefix got: ".
396 (defined $result? "'$result'":'<UNDEF>')." ($context)\n";
397 $prefix = ' ' x (length($prefix) - 5);
398 if (defined $regex) {
399 $expected = 'qr{'.$regex.'}';
402 $expected = "'$expected'";
405 print $TESTERR "# $prefix Expected: $expected\n";
407 print $TESTERR "# $prefix Expected: $expected ($diag)\n";
411 if(defined $Program_Lines{$file}[$line]) {
413 "# $file line $line is: $Program_Lines{$file}[$line]\n"
415 $Program_Lines{$file}[$line] =~ m/[^\s\#\(\)\{\}\[\]\;]/
416 # Otherwise it's a pretty uninteresting line!
419 undef $Program_Lines{$file}[$line];
420 # So we won't repeat it.
423 push @FAILDETAIL, $detail;
430 =item C<skip(I<skip_if_true>, I<args...>)>
432 This is used for tests that under some conditions can be skipped. It's
433 basically equivalent to:
435 if( $skip_if_true ) {
441 ...except that the C<ok(1)> emits not just "C<ok I<testnum>>" but
442 actually "C<ok I<testnum> # I<skip_if_true_value>>".
444 The arguments after the I<skip_if_true> are what is fed to C<ok(...)> if
445 this test isn't skipped.
450 $^O eq 'MSWin' ? 'Skip if under MSWin' : '';
452 # A test to be run EXCEPT under MSWin:
453 skip($if_MSWin, thing($foo), thing($bar) );
455 Or, going the other way:
458 $^O eq 'MSWin' ? 'Skip unless under MSWin' : '';
460 # A test to be run EXCEPT under MSWin:
461 skip($unless_MSWin, thing($foo), thing($bar) );
463 The only tricky thing to remember is that the first parameter is true if
464 you want to I<skip> the test, not I<run> it; and it also doubles as a
465 note about why it's being skipped. So in the first codeblock above, read
466 the code as "skip if MSWin -- (otherwise) test whether C<thing($foo)> is
467 C<thing($bar)>" or for the second case, "skip unless MSWin...".
469 Also, when your I<skip_if_reason> string is true, it really should (for
470 backwards compatibility with older Test.pm versions) start with the
471 string "Skip", as shown in the above examples.
473 Note that in the above cases, C<thing($foo)> and C<thing($bar)>
474 I<are> evaluated -- but as long as the C<skip_if_true> is true,
475 then we C<skip(...)> just tosses out their value (i.e., not
476 bothering to treat them like values to C<ok(...)>. But if
477 you need to I<not> eval the arguments when skipping the
483 # This code returns true if the test passes.
484 # (But it doesn't even get called if the test is skipped.)
485 thing($foo) eq thing($bar)
489 or even this, which is basically equivalent:
492 sub { thing($foo) }, sub { thing($bar) }
495 That is, both are like this:
497 if( $unless_MSWin ) {
498 ok(1); # but it actually appends "# $unless_MSWin"
499 # so that Test::Harness can tell it's a skip
501 # Not skipping, so actually call and evaluate...
502 ok( sub { thing($foo) }, sub { thing($bar) } );
508 local($\, $,); # guard against -l and other things that screw with
511 my $whyskip = _to_value(shift);
512 if (!@_ or $whyskip) {
513 $whyskip = '' if $whyskip =~ m/^\d+$/;
514 $whyskip =~ s/^[Ss]kip(?:\s+|$)//; # backwards compatibility, old
515 # versions required the reason
516 # to start with 'skip'
517 # We print in one shot for VMSy reasons.
518 my $ok = "ok $ntest # skip";
519 $ok .= " $whyskip" if length $whyskip;
525 # backwards compatiblity (I think). skip() used to be
526 # called like ok(), which is weird. I haven't decided what to do with
528 # warn <<WARN if $^W;
529 #This looks like a skip() using the very old interface. Please upgrade to
530 #the documented interface as this has been deprecated.
533 local($TestLevel) = $TestLevel+1; #to ignore this stack frame
543 $ONFAIL->(\@FAILDETAIL) if @FAILDETAIL && $ONFAIL;
555 These tests are expected to succeed. Usually, most or all of your tests
556 are in this category. If a normal test doesn't succeed, then that
557 means that something is I<wrong>.
559 =item * SKIPPED TESTS
561 The C<skip(...)> function is for tests that might or might not be
562 possible to run, depending
563 on the availability of platform-specific features. The first argument
564 should evaluate to true (think "yes, please skip") if the required
565 feature is I<not> available. After the first argument, C<skip(...)> works
566 exactly the same way as C<ok(...)> does.
570 TODO tests are designed for maintaining an B<executable TODO list>.
571 These tests are I<expected to fail.> If a TODO test does succeed,
572 then the feature in question shouldn't be on the TODO list, now
575 Packages should NOT be released with succeeding TODO tests. As soon
576 as a TODO test starts working, it should be promoted to a normal test,
577 and the newly working feature should be documented in the release
578 notes or in the change log.
584 BEGIN { plan test => 4, onfail => sub { warn "CALL 911!" } }
586 Although test failures should be enough, extra diagnostics can be
587 triggered at the end of a test run. C<onfail> is passed an array ref
588 of hash refs that describe each test failure. Each hash will contain
589 at least the following fields: C<package>, C<repetition>, and
590 C<result>. (The file, line, and test number are not included because
591 their correspondence to a particular test is tenuous.) If the test
592 had an expected value or a diagnostic (or "note") string, these will also be
595 The I<optional> C<onfail> hook might be used simply to print out the
596 version of your package and/or how to report problems. It might also
597 be used to generate extremely sophisticated diagnostics for a
598 particularly bizarre test failure. However it's not a panacea. Core
599 dumps or other unrecoverable errors prevent the C<onfail> hook from
600 running. (It is run inside an C<END> block.) Besides, C<onfail> is
601 probably over-kill in most cases. (Your test code should be simpler
602 than the code it is testing, yes?)
605 =head1 BUGS and CAVEATS
611 C<ok(...)>'s special handing of strings which look like they might be
612 regexes can also cause unexpected behavior. An innocent:
614 ok( $fileglob, '/path/to/some/*stuff/' );
616 will fail, since Test.pm considers the second argument to be a regex!
617 The best bet is to use the one-argument form:
619 ok( $fileglob eq '/path/to/some/*stuff/' );
623 C<ok(...)>'s use of string C<eq> can sometimes cause odd problems
625 numbers, especially if you're casting a string to a number:
628 ok( $foo, 1 ); # not ok, "1.0" ne 1
630 Your best bet is to use the single argument form:
632 ok( $foo == 1 ); # ok "1.0" == 1
636 As you may have inferred from the above documentation and examples,
637 C<ok>'s prototype is C<($;$$)> (and, incidentally, C<skip>'s is
638 C<($;$$$)>). This means, for example, that you can do C<ok @foo, @bar>
639 to compare the I<size> of the two arrays. But don't be fooled into
640 thinking that C<ok @foo, @bar> means a comparison of the contents of two
641 arrays -- you're comparing I<just> the number of elements of each. It's
642 so easy to make that mistake in reading C<ok @foo, @bar> that you might
643 want to be very explicit about it, and instead write C<ok scalar(@foo),
650 A past developer of this module once said that it was no longer being
651 actively developed. However, rumors of its demise were greatly
652 exaggerated. Feedback and suggestions are quite welcome.
654 Be aware that the main value of this module is its simplicity. Note
655 that there are already more ambitious modules out there, such as
656 L<Test::More> and L<Test::Unit>.
663 L<Test::Simple>, L<Test::More>, L<Devel::Cover>
665 L<Test::Builder> for building your own testing library.
667 L<Test::Unit> is an interesting XUnit-style testing library.
669 L<Test::Inline> and L<SelfTest> let you embed tests in code.
674 Copyright (c) 1998-2000 Joshua Nathaniel Pritikin. All rights reserved.
676 Copyright (c) 2001-2002 Michael G. Schwern.
678 Copyright (c) 2002 Sean M. Burke.
680 Current maintainer: Sean M. Burke. E<lt>sburke@cpan.orgE<gt>
682 This package is free software and is provided "as is" without express
683 or implied warranty. It may be used, redistributed and/or modified
684 under the same terms as Perl itself.
688 # "Your mistake was a hidden intention."
689 # -- /Oblique Strategies/, Brian Eno and Peter Schmidt