9 # Can't use Carp because it might cause use_ok() to accidentally succeed
10 # even though the module being used forgot to use Carp. Yes, this
13 my($file, $line) = (caller(1))[1,2];
14 warn @_, " at $file line $line\n";
20 use vars qw($VERSION @ISA @EXPORT %EXPORT_TAGS $TODO);
23 @EXPORT = qw(ok use_ok require_ok
24 is isnt like unlike is_deeply
28 eq_array eq_hash eq_set
35 my $Test = Test::Builder->new;
39 # 5.004's Exporter doesn't have export_to_level.
44 (undef) = shift; # redundant arg
45 my $callpkg = caller($level);
46 $pkg->export($callpkg, @_);
52 Test::More - yet another framework for writing test scripts
56 use Test::More tests => $Num_Tests;
58 use Test::More qw(no_plan);
60 use Test::More skip_all => $reason;
62 BEGIN { use_ok( 'Some::Module' ); }
63 require_ok( 'Some::Module' );
65 # Various ways to say "ok"
66 ok($this eq $that, $test_name);
68 is ($this, $that, $test_name);
69 isnt($this, $that, $test_name);
71 # Rather than print STDERR "# here's what went wrong\n"
72 diag("here's what went wrong");
74 like ($this, qr/that/, $test_name);
75 unlike($this, qr/that/, $test_name);
77 cmp_ok($this, '==', $that, $test_name);
79 is_deeply($complex_structure1, $complex_structure2, $test_name);
82 skip $why, $how_many unless $have_some_feature;
84 ok( foo(), $test_name );
85 is( foo(42), 23, $test_name );
91 ok( foo(), $test_name );
92 is( foo(42), 23, $test_name );
95 can_ok($module, @methods);
96 isa_ok($object, $class);
101 # Utility comparison functions.
102 eq_array(\@this, \@that);
103 eq_hash(\%this, \%that);
104 eq_set(\@this, \@that);
107 my @status = Test::More::status;
115 B<STOP!> If you're just getting started writing tests, have a look at
116 Test::Simple first. This is a drop in replacement for Test::Simple
117 which you can switch to once you get the hang of basic testing.
119 The purpose of this module is to provide a wide range of testing
120 utilities. Various ways to say "ok" with better diagnostics,
121 facilities to skip tests, test future features and compare complicated
122 data structures. While you can do almost anything with a simple
123 C<ok()> function, it doesn't provide good diagnostic output.
126 =head2 I love it when a plan comes together
128 Before anything else, you need a testing plan. This basically declares
129 how many tests your script is going to run to protect against premature
132 The preferred way to do this is to declare a plan when you C<use Test::More>.
134 use Test::More tests => $Num_Tests;
136 There are rare cases when you will not know beforehand how many tests
137 your script is going to run. In this case, you can declare that you
138 have no plan. (Try to avoid using this as it weakens your test.)
140 use Test::More qw(no_plan);
142 B<NOTE>: using no_plan requires a Test::Harness upgrade else it will
143 think everything has failed. See L<BUGS and CAVEATS>)
145 In some cases, you'll want to completely skip an entire testing script.
147 use Test::More skip_all => $skip_reason;
149 Your script will declare a skip with the reason why you skipped and
150 exit immediately with a zero (success). See L<Test::Harness> for
153 If you want to control what functions Test::More will export, you
154 have to use the 'import' option. For example, to import everything
155 but 'fail', you'd do:
157 use Test::More tests => 23, import => ['!fail'];
159 Alternatively, you can use the plan() function. Useful for when you
160 have to calculate the number of tests.
163 plan tests => keys %Stuff * 3;
165 or for deciding between running the tests at all:
168 if( $^O eq 'MacOS' ) {
169 plan skip_all => 'Test irrelevant on MacOS';
182 $Test->exported_to($caller);
187 while( $idx <= $#plan ) {
188 if( $plan[$idx] eq 'import' ) {
189 @imports = @{$plan[$idx+1]};
192 elsif( $plan[$idx] eq 'no_diag' ) {
197 push @cleaned_plan, $plan[$idx];
202 $Test->plan(@cleaned_plan);
204 __PACKAGE__->_export_to_level(1, __PACKAGE__, @imports);
215 By convention, each test is assigned a number in order. This is
216 largely done automatically for you. However, it's often very useful to
217 assign a name to each test. Which would you rather see:
225 ok 4 - basic multi-variable
226 not ok 5 - simple exponential
227 ok 6 - force == mass * acceleration
229 The later gives you some idea of what failed. It also makes it easier
230 to find the test in your script, simply search for "simple
233 All test functions take a name argument. It's optional, but highly
234 suggested that you use it.
237 =head2 I'm ok, you're not ok.
239 The basic purpose of this module is to print out either "ok #" or "not
240 ok #" depending on if a given test succeeded or failed. Everything
243 All of the following print "ok" or "not ok" depending on if the test
244 succeeded or failed. They all also return true or false,
251 ok($this eq $that, $test_name);
253 This simply evaluates any expression (C<$this eq $that> is just a
254 simple example) and uses that to determine if the test succeeded or
255 failed. A true expression passes, a false one fails. Very simple.
259 ok( $exp{9} == 81, 'simple exponential' );
260 ok( Film->can('db_Main'), 'set_db()' );
261 ok( $p->tests == 4, 'saw tests' );
262 ok( !grep !defined $_, @items, 'items populated' );
264 (Mnemonic: "This is ok.")
266 $test_name is a very short description of the test that will be printed
267 out. It makes it very easy to find a test in your script when it fails
268 and gives others an idea of your intentions. $test_name is optional,
269 but we B<very> strongly encourage its use.
271 Should an ok() fail, it will produce some diagnostics:
273 not ok 18 - sufficient mucus
274 # Failed test 18 (foo.t at line 42)
276 This is actually Test::Simple's ok() routine.
281 my($test, $name) = @_;
282 $Test->ok($test, $name);
289 is ( $this, $that, $test_name );
290 isnt( $this, $that, $test_name );
292 Similar to ok(), is() and isnt() compare their two arguments
293 with C<eq> and C<ne> respectively and use the result of that to
294 determine if the test succeeded or failed. So these:
296 # Is the ultimate answer 42?
297 is( ultimate_answer(), 42, "Meaning of Life" );
300 isnt( $foo, '', "Got some foo" );
302 are similar to these:
304 ok( ultimate_answer() eq 42, "Meaning of Life" );
305 ok( $foo ne '', "Got some foo" );
307 (Mnemonic: "This is that." "This isn't that.")
309 So why use these? They produce better diagnostics on failure. ok()
310 cannot know what you are testing for (beyond the name), but is() and
311 isnt() know what the test was and why it failed. For example this
314 my $foo = 'waffle'; my $bar = 'yarblokos';
315 is( $foo, $bar, 'Is foo the same as bar?' );
317 Will produce something like this:
319 not ok 17 - Is foo the same as bar?
320 # Failed test (foo.t at line 139)
322 # expected: 'yarblokos'
324 So you can figure out what went wrong without rerunning the test.
326 You are encouraged to use is() and isnt() over ok() where possible,
327 however do not be tempted to use them to find out if something is
331 is( exists $brooklyn{tree}, 1, 'A tree grows in Brooklyn' );
333 This does not check if C<exists $brooklyn{tree}> is true, it checks if
334 it returns 1. Very different. Similar caveats exist for false and 0.
335 In these cases, use ok().
337 ok( exists $brooklyn{tree}, 'A tree grows in Brooklyn' );
339 For those grammatical pedants out there, there's an C<isn't()>
340 function which is an alias of isnt().
357 like( $this, qr/that/, $test_name );
359 Similar to ok(), like() matches $this against the regex C<qr/that/>.
363 like($this, qr/that/, 'this is like that');
367 ok( $this =~ /that/, 'this is like that');
369 (Mnemonic "This is like that".)
371 The second argument is a regular expression. It may be given as a
372 regex reference (i.e. C<qr//>) or (for better compatibility with older
373 perls) as a string that looks like a regex (alternative delimiters are
374 currently not supported):
376 like( $this, '/that/', 'this is like that' );
378 Regex options may be placed on the end (C<'/that/i'>).
380 Its advantages over ok() are similar to that of is() and isnt(). Better
381 diagnostics on failure.
392 unlike( $this, qr/that/, $test_name );
394 Works exactly as like(), only it checks if $this B<does not> match the
406 cmp_ok( $this, $op, $that, $test_name );
408 Halfway between ok() and is() lies cmp_ok(). This allows you to
409 compare two arguments using any binary perl operator.
411 # ok( $this eq $that );
412 cmp_ok( $this, 'eq', $that, 'this eq that' );
414 # ok( $this == $that );
415 cmp_ok( $this, '==', $that, 'this == that' );
417 # ok( $this && $that );
418 cmp_ok( $this, '&&', $that, 'this && that' );
421 Its advantage over ok() is when the test fails you'll know what $this
425 # Failed test (foo.t at line 12)
430 It's also useful in those cases where you are comparing numbers and
431 is()'s use of C<eq> will interfere:
433 cmp_ok( $big_hairy_number, '==', $another_big_hairy_number );
444 can_ok($module, @methods);
445 can_ok($object, @methods);
447 Checks to make sure the $module or $object can do these @methods
448 (works with functions, too).
450 can_ok('Foo', qw(this that whatever));
452 is almost exactly like saying:
454 ok( Foo->can('this') &&
459 only without all the typing and with a better interface. Handy for
460 quickly testing an interface.
462 No matter how many @methods you check, a single can_ok() call counts
463 as one test. If you desire otherwise, use:
465 foreach my $meth (@methods) {
466 can_ok('Foo', $meth);
472 my($proto, @methods) = @_;
473 my $class = ref $proto || $proto;
476 my $ok = $Test->ok( 0, "$class->can(...)" );
477 $Test->diag(' can_ok() called with no methods');
482 foreach my $method (@methods) {
483 local($!, $@); # don't interfere with caller's $@
484 # eval sometimes resets $!
485 eval { $proto->can($method) } || push @nok, $method;
489 $name = @methods == 1 ? "$class->can('$methods[0]')"
490 : "$class->can(...)";
492 my $ok = $Test->ok( !@nok, $name );
494 $Test->diag(map " $class->can('$_') failed\n", @nok);
501 isa_ok($object, $class, $object_name);
502 isa_ok($ref, $type, $ref_name);
504 Checks to see if the given C<< $object->isa($class) >>. Also checks to make
505 sure the object was defined in the first place. Handy for this sort
508 my $obj = Some::Module->new;
509 isa_ok( $obj, 'Some::Module' );
511 where you'd otherwise have to write
513 my $obj = Some::Module->new;
514 ok( defined $obj && $obj->isa('Some::Module') );
516 to safeguard against your test script blowing up.
518 It works on references, too:
520 isa_ok( $array_ref, 'ARRAY' );
522 The diagnostics of this test normally just refer to 'the object'. If
523 you'd like them to be more specific, you can supply an $object_name
524 (for example 'Test customer').
529 my($object, $class, $obj_name) = @_;
532 $obj_name = 'The object' unless defined $obj_name;
533 my $name = "$obj_name isa $class";
534 if( !defined $object ) {
535 $diag = "$obj_name isn't defined";
537 elsif( !ref $object ) {
538 $diag = "$obj_name isn't a reference";
541 # We can't use UNIVERSAL::isa because we want to honor isa() overrides
542 local($@, $!); # eval sometimes resets $!
543 my $rslt = eval { $object->isa($class) };
545 if( $@ =~ /^Can't call method "isa" on unblessed reference/ ) {
546 if( !UNIVERSAL::isa($object, $class) ) {
547 my $ref = ref $object;
548 $diag = "$obj_name isn't a '$class' it's a '$ref'";
552 WHOA! I tried to call ->isa on your object and got some weird error.
553 This should never happen. Please contact the author immediately.
560 my $ref = ref $object;
561 $diag = "$obj_name isn't a '$class' it's a '$ref'";
569 $ok = $Test->ok( 0, $name );
570 $Test->diag(" $diag\n");
573 $ok = $Test->ok( 1, $name );
587 Sometimes you just want to say that the tests have passed. Usually
588 the case is you've got some complicated condition that is difficult to
589 wedge into an ok(). In this case, you can simply use pass() (to
590 declare the test ok) or fail (for not ok). They are synonyms for
593 Use these very, very, very sparingly.
609 If you pick the right test function, you'll usually get a good idea of
610 what went wrong when it failed. But sometimes it doesn't work out
611 that way. So here we have ways for you to write your own diagnostic
612 messages which are safer than just C<print STDERR>.
618 diag(@diagnostic_message);
620 Prints a diagnostic message which is guaranteed not to interfere with
621 test output. Handy for this sort of thing:
623 ok( grep(/foo/, @users), "There's a foo user" ) or
624 diag("Since there's no foo, check that /etc/bar is set up right");
628 not ok 42 - There's a foo user
629 # Failed test (foo.t at line 52)
630 # Since there's no foo, check that /etc/bar is set up right.
632 You might remember C<ok() or diag()> with the mnemonic C<open() or
635 All diag()s can be made silent by passing the "no_diag" option to
636 Test::More. C<use Test::More tests => 1, 'no_diag'>. This is useful
637 if you have diagnostics for personal testing but then wish to make
638 them silent for release without commenting out each individual
641 B<NOTE> The exact formatting of the diagnostic output is still
642 changing, but it is guaranteed that whatever you throw at it it won't
643 interfere with the test.
648 return unless $Show_Diag;
657 You usually want to test if the module you're testing loads ok, rather
658 than just vomiting if its load fails. For such purposes we have
659 C<use_ok> and C<require_ok>.
665 BEGIN { use_ok($module); }
666 BEGIN { use_ok($module, @imports); }
668 These simply use the given $module and test to make sure the load
669 happened ok. It's recommended that you run use_ok() inside a BEGIN
670 block so its functions are exported at compile-time and prototypes are
673 If @imports are given, they are passed through to the use. So this:
675 BEGIN { use_ok('Some::Module', qw(foo bar)) }
679 use Some::Module qw(foo bar);
681 Version numbers can be checked like so:
683 # Just like "use Some::Module 1.02"
684 BEGIN { use_ok('Some::Module', 1.02) }
686 Don't try to do this:
689 use_ok('Some::Module');
691 ...some code that depends on the use...
692 ...happening at compile time...
695 because the notion of "compile-time" is relative. Instead, you want:
697 BEGIN { use_ok('Some::Module') }
698 BEGIN { ...some code that depends on the use... }
704 my($module, @imports) = @_;
705 @imports = () unless @imports;
707 my($pack,$filename,$line) = caller;
709 local($@,$!); # eval sometimes interferes with $!
711 if( @imports == 1 and $imports[0] =~ /^\d+(?:\.\d+)?$/ ) {
712 # probably a version check. Perl needs to see the bare number
713 # for it to work with non-Exporter based modules.
716 use $module $imports[0];
722 use $module \@imports;
726 my $ok = $Test->ok( !$@, "use $module;" );
730 $@ =~ s{^BEGIN failed--compilation aborted at .*$}
731 {BEGIN failed--compilation aborted at $filename line $line.}m;
732 $Test->diag(<<DIAGNOSTIC);
733 Tried to use '$module'.
746 Like use_ok(), except it requires the $module.
755 local($!, $@); # eval sometimes interferes with $!
761 my $ok = $Test->ok( !$@, "require $module;" );
765 $Test->diag(<<DIAGNOSTIC);
766 Tried to require '$module'.
777 =head2 Conditional tests
779 Sometimes running a test under certain conditions will cause the
780 test script to die. A certain function or method isn't implemented
781 (such as fork() on MacOS), some resource isn't available (like a
782 net connection) or a module isn't available. In these cases it's
783 necessary to skip tests, or declare that they are supposed to fail
784 but will work in the future (a todo test).
786 For more details on the mechanics of skip and todo tests see
789 The way Test::More handles this is with a named block. Basically, a
790 block of tests which can be skipped over or made todo. It's best if I
798 skip $why, $how_many if $condition;
800 ...normal testing code goes here...
803 This declares a block of tests that might be skipped, $how_many tests
804 there are, $why and under what $condition to skip them. An example is
805 the easiest way to illustrate:
808 eval { require HTML::Lint };
810 skip "HTML::Lint not installed", 2 if $@;
812 my $lint = new HTML::Lint;
813 isa_ok( $lint, "HTML::Lint" );
815 $lint->parse( $html );
816 is( $lint->errors, 0, "No errors found in HTML" );
819 If the user does not have HTML::Lint installed, the whole block of
820 code I<won't be run at all>. Test::More will output special ok's
821 which Test::Harness interprets as skipped, but passing, tests.
822 It's important that $how_many accurately reflects the number of tests
823 in the SKIP block so the # of tests run will match up with your plan.
825 It's perfectly safe to nest SKIP blocks. Each SKIP block must have
826 the label C<SKIP>, or Test::More can't work its magic.
828 You don't skip tests which are failing because there's a bug in your
829 program, or for which you don't yet have code written. For that you
836 my($why, $how_many) = @_;
838 unless( defined $how_many ) {
839 # $how_many can only be avoided when no_plan is in use.
840 _carp "skip() needs to know \$how_many tests are in the block"
841 unless $Test::Builder::No_Plan;
845 for( 1..$how_many ) {
857 local $TODO = $why if $condition;
859 ...normal testing code goes here...
862 Declares a block of tests you expect to fail and $why. Perhaps it's
863 because you haven't fixed a bug or haven't finished a new feature:
866 local $TODO = "URI::Geller not finished";
868 my $card = "Eight of clubs";
869 is( URI::Geller->your_card, $card, 'Is THIS your card?' );
872 URI::Geller->bend_spoon;
873 is( $spoon, 'bent', "Spoon bending, that's original" );
876 With a todo block, the tests inside are expected to fail. Test::More
877 will run the tests normally, but print out special flags indicating
878 they are "todo". Test::Harness will interpret failures as being ok.
879 Should anything succeed, it will report it as an unexpected success.
880 You then know the thing you had todo is done and can remove the
883 The nice part about todo tests, as opposed to simply commenting out a
884 block of tests, is it's like having a programmatic todo list. You know
885 how much work is left to be done, you're aware of what bugs there are,
886 and you'll know immediately when they're fixed.
888 Once a todo test starts succeeding, simply move it outside the block.
889 When the block is empty, delete it.
891 B<NOTE>: TODO tests require a Test::Harness upgrade else it will
892 treat it as a normal failure. See L<BUGS and CAVEATS>)
898 todo_skip $why, $how_many if $condition;
900 ...normal testing code...
903 With todo tests, it's best to have the tests actually run. That way
904 you'll know when they start passing. Sometimes this isn't possible.
905 Often a failing test will cause the whole program to die or hang, even
906 inside an C<eval BLOCK> with and using C<alarm>. In these extreme
907 cases you have no choice but to skip over the broken tests entirely.
909 The syntax and behavior is similar to a C<SKIP: BLOCK> except the
910 tests will be marked as failing but todo. Test::Harness will
911 interpret them as passing.
916 my($why, $how_many) = @_;
918 unless( defined $how_many ) {
919 # $how_many can only be avoided when no_plan is in use.
920 _carp "todo_skip() needs to know \$how_many tests are in the block"
921 unless $Test::Builder::No_Plan;
925 for( 1..$how_many ) {
926 $Test->todo_skip($why);
933 =item When do I use SKIP vs. TODO?
935 B<If it's something the user might not be able to do>, use SKIP.
936 This includes optional modules that aren't installed, running under
937 an OS that doesn't have some feature (like fork() or symlinks), or maybe
938 you need an Internet connection and one isn't available.
940 B<If it's something the programmer hasn't done yet>, use TODO. This
941 is for any code you haven't written yet, or bugs you have yet to fix,
942 but want to put tests in your testing script (always a good idea).
947 =head2 Comparison functions
949 Not everything is a simple eq check or regex. There are times you
950 need to see if two arrays are equivalent, for instance. For these
951 instances, Test::More provides a handful of useful functions.
953 B<NOTE> These are NOT well-tested on circular references. Nor am I
954 quite sure what will happen with filehandles.
960 is_deeply( $this, $that, $test_name );
962 Similar to is(), except that if $this and $that are hash or array
963 references, it does a deep comparison walking each data structure to
964 see if they are equivalent. If the two structures are different, it
965 will display the place where they start differing.
967 Test::Differences and Test::Deep provide more in-depth functionality
972 use vars qw(@Data_Stack);
973 my $DNE = bless [], 'Does::Not::Exist';
975 unless( @_ == 2 or @_ == 3 ) {
977 is_deeply() takes two or three args, you gave %d.
978 This usually means you passed an array or hash instead
981 chop $msg; # clip off newline so carp() will put in line/file
983 _carp sprintf $msg, scalar @_;
986 my($this, $that, $name) = @_;
989 if( !ref $this || !ref $that ) {
990 $ok = $Test->is_eq($this, $that, $name);
993 local @Data_Stack = ();
994 if( _deep_check($this, $that) ) {
995 $ok = $Test->ok(1, $name);
998 $ok = $Test->ok(0, $name);
999 $ok = $Test->diag(_format_stack(@Data_Stack));
1011 foreach my $entry (@Stack) {
1012 my $type = $entry->{type} || '';
1013 my $idx = $entry->{'idx'};
1014 if( $type eq 'HASH' ) {
1015 $var .= "->" unless $did_arrow++;
1018 elsif( $type eq 'ARRAY' ) {
1019 $var .= "->" unless $did_arrow++;
1022 elsif( $type eq 'REF' ) {
1027 my @vals = @{$Stack[-1]{vals}}[0,1];
1029 ($vars[0] = $var) =~ s/\$FOO/ \$got/;
1030 ($vars[1] = $var) =~ s/\$FOO/\$expected/;
1032 my $out = "Structures begin differing at:\n";
1033 foreach my $idx (0..$#vals) {
1034 my $val = $vals[$idx];
1035 $vals[$idx] = !defined $val ? 'undef' :
1036 $val eq $DNE ? "Does not exist"
1040 $out .= "$vars[0] = $vals[0]\n";
1041 $out .= "$vars[1] = $vals[1]\n";
1050 eq_array(\@this, \@that);
1052 Checks if two arrays are equivalent. This is a deep check, so
1053 multi-level structures are handled correctly.
1060 return 1 if $a1 eq $a2;
1063 my $max = $#$a1 > $#$a2 ? $#$a1 : $#$a2;
1065 my $e1 = $_ > $#$a1 ? $DNE : $a1->[$_];
1066 my $e2 = $_ > $#$a2 ? $DNE : $a2->[$_];
1068 push @Data_Stack, { type => 'ARRAY', idx => $_, vals => [$e1, $e2] };
1069 $ok = _deep_check($e1,$e2);
1070 pop @Data_Stack if $ok;
1083 # Quiet uninitialized value warnings when comparing undefs.
1090 if( UNIVERSAL::isa($e1, 'ARRAY') and
1091 UNIVERSAL::isa($e2, 'ARRAY') )
1093 $ok = eq_array($e1, $e2);
1095 elsif( UNIVERSAL::isa($e1, 'HASH') and
1096 UNIVERSAL::isa($e2, 'HASH') )
1098 $ok = eq_hash($e1, $e2);
1100 elsif( UNIVERSAL::isa($e1, 'REF') and
1101 UNIVERSAL::isa($e2, 'REF') )
1103 push @Data_Stack, { type => 'REF', vals => [$e1, $e2] };
1104 $ok = _deep_check($$e1, $$e2);
1105 pop @Data_Stack if $ok;
1107 elsif( UNIVERSAL::isa($e1, 'SCALAR') and
1108 UNIVERSAL::isa($e2, 'SCALAR') )
1110 push @Data_Stack, { type => 'REF', vals => [$e1, $e2] };
1111 $ok = _deep_check($$e1, $$e2);
1114 push @Data_Stack, { vals => [$e1, $e2] };
1126 eq_hash(\%this, \%that);
1128 Determines if the two hashes contain the same keys and values. This
1135 return 1 if $a1 eq $a2;
1138 my $bigger = keys %$a1 > keys %$a2 ? $a1 : $a2;
1139 foreach my $k (keys %$bigger) {
1140 my $e1 = exists $a1->{$k} ? $a1->{$k} : $DNE;
1141 my $e2 = exists $a2->{$k} ? $a2->{$k} : $DNE;
1143 push @Data_Stack, { type => 'HASH', idx => $k, vals => [$e1, $e2] };
1144 $ok = _deep_check($e1, $e2);
1145 pop @Data_Stack if $ok;
1155 eq_set(\@this, \@that);
1157 Similar to eq_array(), except the order of the elements is B<not>
1158 important. This is a deep check, but the irrelevancy of order only
1159 applies to the top level.
1161 B<NOTE> By historical accident, this is not a true set comparision.
1162 While the order of elements does not matter, duplicate elements do.
1166 # We must make sure that references are treated neutrally. It really
1167 # doesn't matter how we sort them, as long as both arrays are sorted
1168 # with the same algorithm.
1169 sub _bogus_sort { local $^W = 0; ref $a ? -1 : ref $b ? 1 : $a cmp $b }
1173 return 0 unless @$a1 == @$a2;
1175 # There's faster ways to do this, but this is easiest.
1176 return eq_array( [sort _bogus_sort @$a1], [sort _bogus_sort @$a2] );
1182 =head2 Extending and Embedding Test::More
1184 Sometimes the Test::More interface isn't quite enough. Fortunately,
1185 Test::More is built on top of Test::Builder which provides a single,
1186 unified backend for any test library to use. This means two test
1187 libraries which both use Test::Builder B<can be used together in the
1190 If you simply want to do a little tweaking of how the tests behave,
1191 you can access the underlying Test::Builder object like so:
1197 my $test_builder = Test::More->builder;
1199 Returns the Test::Builder object underlying Test::More for you to play
1205 return Test::Builder->new;
1213 If all your tests passed, Test::Builder will exit with zero (which is
1214 normal). If anything failed it will exit with how many failed. If
1215 you run less (or more) tests than you planned, the missing (or extras)
1216 will be considered failures. If no tests were ever run Test::Builder
1217 will throw a warning and exit with 255. If the test died, even after
1218 having successfully completed all its tests, it will still be
1219 considered a failure and will exit with 255.
1221 So the exit codes are...
1223 0 all tests successful
1225 any other number how many failed (including missing or extras)
1227 If you fail more than 254 tests, it will be reported as 254.
1232 Test::More is B<explicitly> tested all the way back to perl 5.004.
1234 =head1 BUGS and CAVEATS
1240 Test::More will only be aware of threads if "use threads" has been done
1241 I<before> Test::More is loaded. This is ok:
1246 This may cause problems:
1251 =item Making your own ok()
1253 If you are trying to extend Test::More, don't. Use Test::Builder
1256 =item The eq_* family has some caveats.
1258 =item Test::Harness upgrade
1260 no_plan and todo depend on new Test::Harness features and fixes. If
1261 you're going to distribute tests that use no_plan or todo your
1262 end-users will have to upgrade Test::Harness to the latest one on
1263 CPAN. If you avoid no_plan and TODO tests, the stock Test::Harness
1266 Installing Test::More should also upgrade Test::Harness.
1273 This is a case of convergent evolution with Joshua Pritikin's Test
1274 module. I was largely unaware of its existence when I'd first
1275 written my own ok() routines. This module exists because I can't
1276 figure out how to easily wedge test names into Test's interface (along
1277 with a few other problems).
1279 The goal here is to have a testing utility that's simple to learn,
1280 quick to use and difficult to trip yourself up with while still
1281 providing more flexibility than the existing Test.pm. As such, the
1282 names of the most common routines are kept tiny, special cases and
1283 magic side-effects are kept to a minimum. WYSIWYG.
1288 L<Test::Simple> if all this confuses you and you just want to write
1289 some tests. You can upgrade to Test::More later (it's forward
1292 L<Test> is the old testing module. Its main benefit is that it has
1293 been distributed with Perl since 5.004_05.
1295 L<Test::Harness> for details on how your test results are interpreted
1298 L<Test::Differences> for more ways to test complex data structures.
1299 And it plays well with Test::More.
1301 L<Test::Class> is like XUnit but more perlish.
1303 L<Test::Deep> gives you more powerful complex data structure testing.
1305 L<Test::Unit> is XUnit style testing.
1307 L<Test::Inline> shows the idea of embedded testing.
1309 L<Bundle::Test> installs a whole bunch of useful test modules.
1314 Michael G Schwern E<lt>schwern@pobox.comE<gt> with much inspiration
1315 from Joshua Pritikin's Test module and lots of help from Barrie
1316 Slaymaker, Tony Bowden, blackstar.co.uk, chromatic and the perl-qa gang.
1321 Copyright 2001, 2002 by Michael G Schwern E<lt>schwern@pobox.comE<gt>.
1323 This program is free software; you can redistribute it and/or
1324 modify it under the same terms as Perl itself.
1326 See F<http://www.perl.com/perl/misc/Artistic.html>