3 use Test::Harness 1.1601 ();
5 use vars qw($VERSION @ISA @EXPORT $ntest %todo %history $TestLevel);
9 @EXPORT= qw(&plan &ok &skip $ntest);
11 $TestLevel = 0; # how many extra stack frames to skip
16 # Use of this variable is strongly discouraged. It is set mainly to
17 # help test coverage analyzers know which test is running.
18 $ENV{REGRESSION_TEST} = $0;
21 croak "Test::plan(%args): odd number of arguments" if @_ & 1;
23 for (my $x=0; $x < @_; $x+=2) {
24 my ($k,$v) = @_[$x,$x+1];
25 if ($k =~ /^test(s)?$/) { $max = $v; }
26 elsif ($k eq 'todo' or
27 $k eq 'failok') { for (@$v) { $todo{$_}=1; }; }
28 else { carp "Test::plan(): skipping unrecognized directive '$k'" }
30 my @todo = sort { $a <=> $b } keys %todo;
32 print "1..$max todo ".join(' ', @todo).";\n";
40 (ref $v or '') eq 'CODE' ? $v->() : $v;
43 # prototypes are not used for maximum flexibility
45 # STDERR is NOT used for diagnostic output that should be fixed before
46 # the module is released.
49 my ($pkg,$file,$line) = caller($TestLevel);
50 my $repetition = ++$history{"$file:$line"};
51 my $context = ("$file at line $line".
52 ($repetition > 1 ? " (\#$repetition)" : ''));
56 print "not ok $ntest\n";
57 print "# test $context: DOESN'T TEST ANYTHING!\n";
59 my $result = to_value(shift);
64 $expected = to_value(shift);
65 $ok = $result eq $expected;
69 print "ok $ntest # Wow!\n";
71 $diag = to_value(shift) if @_;
73 print "not ok $ntest # (failure expected)\n";
75 print "not ok $ntest # (failure expected: $diag)\n";
83 $diag = to_value(shift) if @_;
84 if (!defined $expected) {
86 print STDERR "# Failed $context\n";
88 print STDERR "# Failed $context: $diag\n";
91 print STDERR "# Got: '$result' ($context)\n";
93 print STDERR "# Expected: '$expected'\n";
95 print STDERR "# Expected: '$expected' ($diag)\n";
106 if (to_value(shift)) {
107 print "ok $ntest # skip\n";
111 local($TestLevel) += 1; #ignore this stack frame
121 Test - provides a simple framework for writing test scripts
127 BEGIN { plan tests => 12, todo => [3,4] }
132 ok(0); # ok, expected failure (see todo list, above)
133 ok(1); # surprise success!
135 ok(0,1); # failure: '0' ne '1'
136 ok('broke','fixed'); # failure: 'broke' ne 'fixed'
137 ok('fixed','fixed'); # success: 'fixed' eq 'fixed'
139 ok(sub { 1+1 }, 2); # success: '2' eq '2'
140 ok(sub { 1+1 }, 3); # failure: '2' ne '3'
141 ok(0, int(rand(2)); # (just kidding! :-)
144 ok(scalar(@list), 3, "\@list=".join(',',@list)); #extra diagnostics
146 skip($feature_is_missing, ...); #do platform specific test
150 Test::Harness expects to see particular output when it executes tests.
151 This module aims to make writing proper test scripts just a little bit
152 easier (and less error prone :-).
160 These tests are expected to succeed. If they don't, something's
163 =item * SKIPPED TESTS
165 Skip tests need a platform specific feature that might or might not be
166 available. The first argument should evaluate to true if the required
167 feature is NOT available. After the first argument, skip tests work
168 exactly the same way as do normal tests.
172 TODO tests are designed for maintaining an executable TODO list.
173 These tests are expected NOT to succeed (otherwise the feature they
174 test would be on the new feature list, not the TODO list).
176 Packages should NOT be released with successful TODO tests. As soon
177 as a TODO test starts working, it should be promoted to a normal test
178 and the new feature should be documented in the release notes.
184 L<Test::Harness> and various test coverage analysis tools.
188 Copyright © 1998 Joshua Nathaniel Pritikin. All rights reserved.
190 This package is free software and is provided "as is" without express
191 or implied warranty. It may be used, redistributed and/or modified
192 under the terms of the Perl Artistic License (see
193 http://www.perl.com/perl/misc/Artistic.html)