1Test::More(3) User Contributed Perl Documentation Test::More(3)
2
3
4
6 Test::More - yet another framework for writing test scripts
7
9 use Test::More tests => 23;
10 # or
11 use Test::More skip_all => $reason;
12 # or
13 use Test::More; # see done_testing()
14
15 require_ok( 'Some::Module' );
16
17 # Various ways to say "ok"
18 ok($got eq $expected, $test_name);
19
20 is ($got, $expected, $test_name);
21 isnt($got, $expected, $test_name);
22
23 # Rather than print STDERR "# here's what went wrong\n"
24 diag("here's what went wrong");
25
26 like ($got, qr/expected/, $test_name);
27 unlike($got, qr/expected/, $test_name);
28
29 cmp_ok($got, '==', $expected, $test_name);
30
31 is_deeply($got_complex_structure, $expected_complex_structure, $test_name);
32
33 SKIP: {
34 skip $why, $how_many unless $have_some_feature;
35
36 ok( foo(), $test_name );
37 is( foo(42), 23, $test_name );
38 };
39
40 TODO: {
41 local $TODO = $why;
42
43 ok( foo(), $test_name );
44 is( foo(42), 23, $test_name );
45 };
46
47 can_ok($module, @methods);
48 isa_ok($object, $class);
49
50 pass($test_name);
51 fail($test_name);
52
53 BAIL_OUT($why);
54
55 # UNIMPLEMENTED!!!
56 my @status = Test::More::status;
57
59 STOP! If you're just getting started writing tests, have a look at
60 Test2::Suite first.
61
62 This is a drop in replacement for Test::Simple which you can switch to
63 once you get the hang of basic testing.
64
65 The purpose of this module is to provide a wide range of testing
66 utilities. Various ways to say "ok" with better diagnostics,
67 facilities to skip tests, test future features and compare complicated
68 data structures. While you can do almost anything with a simple ok()
69 function, it doesn't provide good diagnostic output.
70
71 I love it when a plan comes together
72 Before anything else, you need a testing plan. This basically declares
73 how many tests your script is going to run to protect against premature
74 failure.
75
76 The preferred way to do this is to declare a plan when you "use
77 Test::More".
78
79 use Test::More tests => 23;
80
81 There are cases when you will not know beforehand how many tests your
82 script is going to run. In this case, you can declare your tests at
83 the end.
84
85 use Test::More;
86
87 ... run your tests ...
88
89 done_testing( $number_of_tests_run );
90
91 NOTE done_testing() should never be called in an "END { ... }" block.
92
93 Sometimes you really don't know how many tests were run, or it's too
94 difficult to calculate. In which case you can leave off
95 $number_of_tests_run.
96
97 In some cases, you'll want to completely skip an entire testing script.
98
99 use Test::More skip_all => $skip_reason;
100
101 Your script will declare a skip with the reason why you skipped and
102 exit immediately with a zero (success). See Test::Harness for details.
103
104 If you want to control what functions Test::More will export, you have
105 to use the 'import' option. For example, to import everything but
106 'fail', you'd do:
107
108 use Test::More tests => 23, import => ['!fail'];
109
110 Alternatively, you can use the plan() function. Useful for when you
111 have to calculate the number of tests.
112
113 use Test::More;
114 plan tests => keys %Stuff * 3;
115
116 or for deciding between running the tests at all:
117
118 use Test::More;
119 if( $^O eq 'MacOS' ) {
120 plan skip_all => 'Test irrelevant on MacOS';
121 }
122 else {
123 plan tests => 42;
124 }
125
126 done_testing
127 done_testing();
128 done_testing($number_of_tests);
129
130 If you don't know how many tests you're going to run, you can issue
131 the plan when you're done running tests.
132
133 $number_of_tests is the same as plan(), it's the number of tests
134 you expected to run. You can omit this, in which case the number
135 of tests you ran doesn't matter, just the fact that your tests ran
136 to conclusion.
137
138 This is safer than and replaces the "no_plan" plan.
139
140 Note: You must never put done_testing() inside an "END { ... }"
141 block. The plan is there to ensure your test does not exit before
142 testing has completed. If you use an END block you completely
143 bypass this protection.
144
145 Test names
146 By convention, each test is assigned a number in order. This is
147 largely done automatically for you. However, it's often very useful to
148 assign a name to each test. Which would you rather see:
149
150 ok 4
151 not ok 5
152 ok 6
153
154 or
155
156 ok 4 - basic multi-variable
157 not ok 5 - simple exponential
158 ok 6 - force == mass * acceleration
159
160 The later gives you some idea of what failed. It also makes it easier
161 to find the test in your script, simply search for "simple
162 exponential".
163
164 All test functions take a name argument. It's optional, but highly
165 suggested that you use it.
166
167 I'm ok, you're not ok.
168 The basic purpose of this module is to print out either "ok #" or "not
169 ok #" depending on if a given test succeeded or failed. Everything
170 else is just gravy.
171
172 All of the following print "ok" or "not ok" depending on if the test
173 succeeded or failed. They all also return true or false, respectively.
174
175 ok
176 ok($got eq $expected, $test_name);
177
178 This simply evaluates any expression ("$got eq $expected" is just a
179 simple example) and uses that to determine if the test succeeded or
180 failed. A true expression passes, a false one fails. Very simple.
181
182 For example:
183
184 ok( $exp{9} == 81, 'simple exponential' );
185 ok( Film->can('db_Main'), 'set_db()' );
186 ok( $p->tests == 4, 'saw tests' );
187 ok( !grep(!defined $_, @items), 'all items defined' );
188
189 (Mnemonic: "This is ok.")
190
191 $test_name is a very short description of the test that will be
192 printed out. It makes it very easy to find a test in your script
193 when it fails and gives others an idea of your intentions.
194 $test_name is optional, but we very strongly encourage its use.
195
196 Should an ok() fail, it will produce some diagnostics:
197
198 not ok 18 - sufficient mucus
199 # Failed test 'sufficient mucus'
200 # in foo.t at line 42.
201
202 This is the same as Test::Simple's ok() routine.
203
204 is
205 isnt
206 is ( $got, $expected, $test_name );
207 isnt( $got, $expected, $test_name );
208
209 Similar to ok(), is() and isnt() compare their two arguments with
210 "eq" and "ne" respectively and use the result of that to determine
211 if the test succeeded or failed. So these:
212
213 # Is the ultimate answer 42?
214 is( ultimate_answer(), 42, "Meaning of Life" );
215
216 # $foo isn't empty
217 isnt( $foo, '', "Got some foo" );
218
219 are similar to these:
220
221 ok( ultimate_answer() eq 42, "Meaning of Life" );
222 ok( $foo ne '', "Got some foo" );
223
224 "undef" will only ever match "undef". So you can test a value
225 against "undef" like this:
226
227 is($not_defined, undef, "undefined as expected");
228
229 (Mnemonic: "This is that." "This isn't that.")
230
231 So why use these? They produce better diagnostics on failure.
232 ok() cannot know what you are testing for (beyond the name), but
233 is() and isnt() know what the test was and why it failed. For
234 example this test:
235
236 my $foo = 'waffle'; my $bar = 'yarblokos';
237 is( $foo, $bar, 'Is foo the same as bar?' );
238
239 Will produce something like this:
240
241 not ok 17 - Is foo the same as bar?
242 # Failed test 'Is foo the same as bar?'
243 # in foo.t at line 139.
244 # got: 'waffle'
245 # expected: 'yarblokos'
246
247 So you can figure out what went wrong without rerunning the test.
248
249 You are encouraged to use is() and isnt() over ok() where possible,
250 however do not be tempted to use them to find out if something is
251 true or false!
252
253 # XXX BAD!
254 is( exists $brooklyn{tree}, 1, 'A tree grows in Brooklyn' );
255
256 This does not check if "exists $brooklyn{tree}" is true, it checks
257 if it returns 1. Very different. Similar caveats exist for false
258 and 0. In these cases, use ok().
259
260 ok( exists $brooklyn{tree}, 'A tree grows in Brooklyn' );
261
262 A simple call to isnt() usually does not provide a strong test but
263 there are cases when you cannot say much more about a value than
264 that it is different from some other value:
265
266 new_ok $obj, "Foo";
267
268 my $clone = $obj->clone;
269 isa_ok $obj, "Foo", "Foo->clone";
270
271 isnt $obj, $clone, "clone() produces a different object";
272
273 Historically we supported an "isn't()" function as an alias of
274 isnt(), however in Perl 5.37.9 support for the use of aprostrophe
275 as a package separator was deprecated and by Perl 5.42.0 support
276 for it will have been removed completely. Accordingly use of
277 "isn't()" is also deprecated, and will produce warnings when used
278 unless 'deprecated' warnings are specifically disabled in the scope
279 where it is used. You are strongly advised to migrate to using
280 isnt() instead.
281
282 like
283 like( $got, qr/expected/, $test_name );
284
285 Similar to ok(), like() matches $got against the regex
286 "qr/expected/".
287
288 So this:
289
290 like($got, qr/expected/, 'this is like that');
291
292 is similar to:
293
294 ok( $got =~ m/expected/, 'this is like that');
295
296 (Mnemonic "This is like that".)
297
298 The second argument is a regular expression. It may be given as a
299 regex reference (i.e. "qr//") or (for better compatibility with
300 older perls) as a string that looks like a regex (alternative
301 delimiters are currently not supported):
302
303 like( $got, '/expected/', 'this is like that' );
304
305 Regex options may be placed on the end ('/expected/i').
306
307 Its advantages over ok() are similar to that of is() and isnt().
308 Better diagnostics on failure.
309
310 unlike
311 unlike( $got, qr/expected/, $test_name );
312
313 Works exactly as like(), only it checks if $got does not match the
314 given pattern.
315
316 cmp_ok
317 cmp_ok( $got, $op, $expected, $test_name );
318
319 Halfway between ok() and is() lies cmp_ok(). This allows you to
320 compare two arguments using any binary perl operator. The test
321 passes if the comparison is true and fails otherwise.
322
323 # ok( $got eq $expected );
324 cmp_ok( $got, 'eq', $expected, 'this eq that' );
325
326 # ok( $got == $expected );
327 cmp_ok( $got, '==', $expected, 'this == that' );
328
329 # ok( $got && $expected );
330 cmp_ok( $got, '&&', $expected, 'this && that' );
331 ...etc...
332
333 Its advantage over ok() is when the test fails you'll know what
334 $got and $expected were:
335
336 not ok 1
337 # Failed test in foo.t at line 12.
338 # '23'
339 # &&
340 # undef
341
342 It's also useful in those cases where you are comparing numbers and
343 is()'s use of "eq" will interfere:
344
345 cmp_ok( $big_hairy_number, '==', $another_big_hairy_number );
346
347 It's especially useful when comparing greater-than or smaller-than
348 relation between values:
349
350 cmp_ok( $some_value, '<=', $upper_limit );
351
352 can_ok
353 can_ok($module, @methods);
354 can_ok($object, @methods);
355
356 Checks to make sure the $module or $object can do these @methods
357 (works with functions, too).
358
359 can_ok('Foo', qw(this that whatever));
360
361 is almost exactly like saying:
362
363 ok( Foo->can('this') &&
364 Foo->can('that') &&
365 Foo->can('whatever')
366 );
367
368 only without all the typing and with a better interface. Handy for
369 quickly testing an interface.
370
371 No matter how many @methods you check, a single can_ok() call
372 counts as one test. If you desire otherwise, use:
373
374 foreach my $meth (@methods) {
375 can_ok('Foo', $meth);
376 }
377
378 isa_ok
379 isa_ok($object, $class, $object_name);
380 isa_ok($subclass, $class, $object_name);
381 isa_ok($ref, $type, $ref_name);
382
383 Checks to see if the given "$object->isa($class)". Also checks to
384 make sure the object was defined in the first place. Handy for
385 this sort of thing:
386
387 my $obj = Some::Module->new;
388 isa_ok( $obj, 'Some::Module' );
389
390 where you'd otherwise have to write
391
392 my $obj = Some::Module->new;
393 ok( defined $obj && $obj->isa('Some::Module') );
394
395 to safeguard against your test script blowing up.
396
397 You can also test a class, to make sure that it has the right
398 ancestor:
399
400 isa_ok( 'Vole', 'Rodent' );
401
402 It works on references, too:
403
404 isa_ok( $array_ref, 'ARRAY' );
405
406 The diagnostics of this test normally just refer to 'the object'.
407 If you'd like them to be more specific, you can supply an
408 $object_name (for example 'Test customer').
409
410 new_ok
411 my $obj = new_ok( $class );
412 my $obj = new_ok( $class => \@args );
413 my $obj = new_ok( $class => \@args, $object_name );
414
415 A convenience function which combines creating an object and
416 calling isa_ok() on that object.
417
418 It is basically equivalent to:
419
420 my $obj = $class->new(@args);
421 isa_ok $obj, $class, $object_name;
422
423 If @args is not given, an empty list will be used.
424
425 This function only works on new() and it assumes new() will return
426 just a single object which isa $class.
427
428 subtest
429 subtest $name => \&code, @args;
430
431 subtest() runs the &code as its own little test with its own plan
432 and its own result. The main test counts this as a single test
433 using the result of the whole subtest to determine if its ok or not
434 ok.
435
436 For example...
437
438 use Test::More tests => 3;
439
440 pass("First test");
441
442 subtest 'An example subtest' => sub {
443 plan tests => 2;
444
445 pass("This is a subtest");
446 pass("So is this");
447 };
448
449 pass("Third test");
450
451 This would produce.
452
453 1..3
454 ok 1 - First test
455 # Subtest: An example subtest
456 1..2
457 ok 1 - This is a subtest
458 ok 2 - So is this
459 ok 2 - An example subtest
460 ok 3 - Third test
461
462 A subtest may call "skip_all". No tests will be run, but the
463 subtest is considered a skip.
464
465 subtest 'skippy' => sub {
466 plan skip_all => 'cuz I said so';
467 pass('this test will never be run');
468 };
469
470 Returns true if the subtest passed, false otherwise.
471
472 Due to how subtests work, you may omit a plan if you desire. This
473 adds an implicit done_testing() to the end of your subtest. The
474 following two subtests are equivalent:
475
476 subtest 'subtest with implicit done_testing()', sub {
477 ok 1, 'subtests with an implicit done testing should work';
478 ok 1, '... and support more than one test';
479 ok 1, '... no matter how many tests are run';
480 };
481
482 subtest 'subtest with explicit done_testing()', sub {
483 ok 1, 'subtests with an explicit done testing should work';
484 ok 1, '... and support more than one test';
485 ok 1, '... no matter how many tests are run';
486 done_testing();
487 };
488
489 Extra arguments given to "subtest" are passed to the callback. For
490 example:
491
492 sub my_subtest {
493 my $range = shift;
494 ...
495 }
496
497 for my $range (1, 10, 100, 1000) {
498 subtest "testing range $range", \&my_subtest, $range;
499 }
500
501 pass
502 fail
503 pass($test_name);
504 fail($test_name);
505
506 Sometimes you just want to say that the tests have passed. Usually
507 the case is you've got some complicated condition that is difficult
508 to wedge into an ok(). In this case, you can simply use pass() (to
509 declare the test ok) or fail (for not ok). They are synonyms for
510 ok(1) and ok(0).
511
512 Use these very, very, very sparingly.
513
514 Module tests
515 Sometimes you want to test if a module, or a list of modules, can
516 successfully load. For example, you'll often want a first test which
517 simply loads all the modules in the distribution to make sure they work
518 before going on to do more complicated testing.
519
520 For such purposes we have "use_ok" and "require_ok".
521
522 require_ok
523 require_ok($module);
524 require_ok($file);
525
526 Tries to "require" the given $module or $file. If it loads
527 successfully, the test will pass. Otherwise it fails and displays
528 the load error.
529
530 "require_ok" will guess whether the input is a module name or a
531 filename.
532
533 No exception will be thrown if the load fails.
534
535 # require Some::Module
536 require_ok "Some::Module";
537
538 # require "Some/File.pl";
539 require_ok "Some/File.pl";
540
541 # stop testing if any of your modules will not load
542 for my $module (@module) {
543 require_ok $module or BAIL_OUT "Can't load $module";
544 }
545
546 use_ok
547 BEGIN { use_ok($module); }
548 BEGIN { use_ok($module, @imports); }
549
550 Like "require_ok", but it will "use" the $module in question and
551 only loads modules, not files.
552
553 If you just want to test a module can be loaded, use "require_ok".
554
555 If you just want to load a module in a test, we recommend simply
556 using "use" directly. It will cause the test to stop.
557
558 It's recommended that you run use_ok() inside a BEGIN block so its
559 functions are exported at compile-time and prototypes are properly
560 honored.
561
562 If @imports are given, they are passed through to the use. So
563 this:
564
565 BEGIN { use_ok('Some::Module', qw(foo bar)) }
566
567 is like doing this:
568
569 use Some::Module qw(foo bar);
570
571 Version numbers can be checked like so:
572
573 # Just like "use Some::Module 1.02"
574 BEGIN { use_ok('Some::Module', 1.02) }
575
576 Don't try to do this:
577
578 BEGIN {
579 use_ok('Some::Module');
580
581 ...some code that depends on the use...
582 ...happening at compile time...
583 }
584
585 because the notion of "compile-time" is relative. Instead, you
586 want:
587
588 BEGIN { use_ok('Some::Module') }
589 BEGIN { ...some code that depends on the use... }
590
591 If you want the equivalent of "use Foo ()", use a module but not
592 import anything, use "require_ok".
593
594 BEGIN { require_ok "Foo" }
595
596 Complex data structures
597 Not everything is a simple eq check or regex. There are times you need
598 to see if two data structures are equivalent. For these instances
599 Test::More provides a handful of useful functions.
600
601 NOTE I'm not quite sure what will happen with filehandles.
602
603 is_deeply
604 is_deeply( $got, $expected, $test_name );
605
606 Similar to is(), except that if $got and $expected are references,
607 it does a deep comparison walking each data structure to see if
608 they are equivalent. If the two structures are different, it will
609 display the place where they start differing.
610
611 is_deeply() compares the dereferenced values of references, the
612 references themselves (except for their type) are ignored. This
613 means aspects such as blessing and ties are not considered
614 "different".
615
616 is_deeply() currently has very limited handling of function
617 reference and globs. It merely checks if they have the same
618 referent. This may improve in the future.
619
620 Test::Differences and Test::Deep provide more in-depth
621 functionality along these lines.
622
623 NOTE is_deeply() has limitations when it comes to comparing strings
624 and refs:
625
626 my $path = path('.');
627 my $hash = {};
628 is_deeply( $path, "$path" ); # ok
629 is_deeply( $hash, "$hash" ); # fail
630
631 This happens because is_deeply will unoverload all arguments
632 unconditionally. It is probably best not to use is_deeply with
633 overloading. For legacy reasons this is not likely to ever be
634 fixed. If you would like a much better tool for this you should see
635 Test2::Suite Specifically Test2::Tools::Compare has an is()
636 function that works like "is_deeply" with many improvements.
637
638 Diagnostics
639 If you pick the right test function, you'll usually get a good idea of
640 what went wrong when it failed. But sometimes it doesn't work out that
641 way. So here we have ways for you to write your own diagnostic
642 messages which are safer than just "print STDERR".
643
644 diag
645 diag(@diagnostic_message);
646
647 Prints a diagnostic message which is guaranteed not to interfere
648 with test output. Like "print" @diagnostic_message is simply
649 concatenated together.
650
651 Returns false, so as to preserve failure.
652
653 Handy for this sort of thing:
654
655 ok( grep(/foo/, @users), "There's a foo user" ) or
656 diag("Since there's no foo, check that /etc/bar is set up right");
657
658 which would produce:
659
660 not ok 42 - There's a foo user
661 # Failed test 'There's a foo user'
662 # in foo.t at line 52.
663 # Since there's no foo, check that /etc/bar is set up right.
664
665 You might remember "ok() or diag()" with the mnemonic open() or
666 die().
667
668 NOTE The exact formatting of the diagnostic output is still
669 changing, but it is guaranteed that whatever you throw at it won't
670 interfere with the test.
671
672 note
673 note(@diagnostic_message);
674
675 Like diag(), except the message will not be seen when the test is
676 run in a harness. It will only be visible in the verbose TAP
677 stream.
678
679 Handy for putting in notes which might be useful for debugging, but
680 don't indicate a problem.
681
682 note("Tempfile is $tempfile");
683
684 explain
685 my @dump = explain @diagnostic_message;
686
687 Will dump the contents of any references in a human readable
688 format. Usually you want to pass this into "note" or "diag".
689
690 Handy for things like...
691
692 is_deeply($have, $want) || diag explain $have;
693
694 or
695
696 note explain \%args;
697 Some::Class->method(%args);
698
699 Conditional tests
700 Sometimes running a test under certain conditions will cause the test
701 script to die. A certain function or method isn't implemented (such as
702 fork() on MacOS), some resource isn't available (like a net connection)
703 or a module isn't available. In these cases it's necessary to skip
704 tests, or declare that they are supposed to fail but will work in the
705 future (a todo test).
706
707 For more details on the mechanics of skip and todo tests see
708 Test::Harness.
709
710 The way Test::More handles this is with a named block. Basically, a
711 block of tests which can be skipped over or made todo. It's best if I
712 just show you...
713
714 SKIP: BLOCK
715 SKIP: {
716 skip $why, $how_many if $condition;
717
718 ...normal testing code goes here...
719 }
720
721 This declares a block of tests that might be skipped, $how_many
722 tests there are, $why and under what $condition to skip them. An
723 example is the easiest way to illustrate:
724
725 SKIP: {
726 eval { require HTML::Lint };
727
728 skip "HTML::Lint not installed", 2 if $@;
729
730 my $lint = new HTML::Lint;
731 isa_ok( $lint, "HTML::Lint" );
732
733 $lint->parse( $html );
734 is( $lint->errors, 0, "No errors found in HTML" );
735 }
736
737 If the user does not have HTML::Lint installed, the whole block of
738 code won't be run at all. Test::More will output special ok's
739 which Test::Harness interprets as skipped, but passing, tests.
740
741 It's important that $how_many accurately reflects the number of
742 tests in the SKIP block so the # of tests run will match up with
743 your plan. If your plan is "no_plan" $how_many is optional and
744 will default to 1.
745
746 It's perfectly safe to nest SKIP blocks. Each SKIP block must have
747 the label "SKIP", or Test::More can't work its magic.
748
749 You don't skip tests which are failing because there's a bug in
750 your program, or for which you don't yet have code written. For
751 that you use TODO. Read on.
752
753 TODO: BLOCK
754 TODO: {
755 local $TODO = $why if $condition;
756
757 ...normal testing code goes here...
758 }
759
760 Declares a block of tests you expect to fail and $why. Perhaps
761 it's because you haven't fixed a bug or haven't finished a new
762 feature:
763
764 TODO: {
765 local $TODO = "URI::Geller not finished";
766
767 my $card = "Eight of clubs";
768 is( URI::Geller->your_card, $card, 'Is THIS your card?' );
769
770 my $spoon;
771 URI::Geller->bend_spoon;
772 is( $spoon, 'bent', "Spoon bending, that's original" );
773 }
774
775 With a todo block, the tests inside are expected to fail.
776 Test::More will run the tests normally, but print out special flags
777 indicating they are "todo". Test::Harness will interpret failures
778 as being ok. Should anything succeed, it will report it as an
779 unexpected success. You then know the thing you had todo is done
780 and can remove the TODO flag.
781
782 The nice part about todo tests, as opposed to simply commenting out
783 a block of tests, is that it is like having a programmatic todo
784 list. You know how much work is left to be done, you're aware of
785 what bugs there are, and you'll know immediately when they're
786 fixed.
787
788 Once a todo test starts succeeding, simply move it outside the
789 block. When the block is empty, delete it.
790
791 Note that, if you leave $TODO unset or undef, Test::More reports
792 failures as normal. This can be useful to mark the tests as
793 expected to fail only in certain conditions, e.g.:
794
795 TODO: {
796 local $TODO = "$^O doesn't work yet. :(" if !_os_is_supported($^O);
797
798 ...
799 }
800
801 todo_skip
802 TODO: {
803 todo_skip $why, $how_many if $condition;
804
805 ...normal testing code...
806 }
807
808 With todo tests, it's best to have the tests actually run. That
809 way you'll know when they start passing. Sometimes this isn't
810 possible. Often a failing test will cause the whole program to die
811 or hang, even inside an "eval BLOCK" with and using "alarm". In
812 these extreme cases you have no choice but to skip over the broken
813 tests entirely.
814
815 The syntax and behavior is similar to a "SKIP: BLOCK" except the
816 tests will be marked as failing but todo. Test::Harness will
817 interpret them as passing.
818
819 When do I use SKIP vs. TODO?
820 If it's something the user might not be able to do, use SKIP. This
821 includes optional modules that aren't installed, running under an
822 OS that doesn't have some feature (like fork() or symlinks), or
823 maybe you need an Internet connection and one isn't available.
824
825 If it's something the programmer hasn't done yet, use TODO. This
826 is for any code you haven't written yet, or bugs you have yet to
827 fix, but want to put tests in your testing script (always a good
828 idea).
829
830 Test control
831 BAIL_OUT
832 BAIL_OUT($reason);
833
834 Indicates to the harness that things are going so badly all testing
835 should terminate. This includes the running of any additional test
836 scripts.
837
838 This is typically used when testing cannot continue such as a
839 critical module failing to compile or a necessary external utility
840 not being available such as a database connection failing.
841
842 The test will exit with 255.
843
844 For even better control look at Test::Most.
845
846 Discouraged comparison functions
847 The use of the following functions is discouraged as they are not
848 actually testing functions and produce no diagnostics to help figure
849 out what went wrong. They were written before is_deeply() existed
850 because I couldn't figure out how to display a useful diff of two
851 arbitrary data structures.
852
853 These functions are usually used inside an ok().
854
855 ok( eq_array(\@got, \@expected) );
856
857 is_deeply() can do that better and with diagnostics.
858
859 is_deeply( \@got, \@expected );
860
861 They may be deprecated in future versions.
862
863 eq_array
864 my $is_eq = eq_array(\@got, \@expected);
865
866 Checks if two arrays are equivalent. This is a deep check, so
867 multi-level structures are handled correctly.
868
869 eq_hash
870 my $is_eq = eq_hash(\%got, \%expected);
871
872 Determines if the two hashes contain the same keys and values.
873 This is a deep check.
874
875 eq_set
876 my $is_eq = eq_set(\@got, \@expected);
877
878 Similar to eq_array(), except the order of the elements is not
879 important. This is a deep check, but the irrelevancy of order only
880 applies to the top level.
881
882 ok( eq_set(\@got, \@expected) );
883
884 Is better written:
885
886 is_deeply( [sort @got], [sort @expected] );
887
888 NOTE By historical accident, this is not a true set comparison.
889 While the order of elements does not matter, duplicate elements do.
890
891 NOTE eq_set() does not know how to deal with references at the top
892 level. The following is an example of a comparison which might not
893 work:
894
895 eq_set([\1, \2], [\2, \1]);
896
897 Test::Deep contains much better set comparison functions.
898
899 Extending and Embedding Test::More
900 Sometimes the Test::More interface isn't quite enough. Fortunately,
901 Test::More is built on top of Test::Builder which provides a single,
902 unified backend for any test library to use. This means two test
903 libraries which both use Test::Builder can be used together in the same
904 program.
905
906 If you simply want to do a little tweaking of how the tests behave, you
907 can access the underlying Test::Builder object like so:
908
909 builder
910 my $test_builder = Test::More->builder;
911
912 Returns the Test::Builder object underlying Test::More for you to
913 play with.
914
916 If all your tests passed, Test::Builder will exit with zero (which is
917 normal). If anything failed it will exit with how many failed. If you
918 run less (or more) tests than you planned, the missing (or extras) will
919 be considered failures. If no tests were ever run Test::Builder will
920 throw a warning and exit with 255. If the test died, even after having
921 successfully completed all its tests, it will still be considered a
922 failure and will exit with 255.
923
924 So the exit codes are...
925
926 0 all tests successful
927 255 test died or all passed but wrong # of tests run
928 any other number how many failed (including missing or extras)
929
930 If you fail more than 254 tests, it will be reported as 254.
931
932 NOTE This behavior may go away in future versions.
933
935 Test::More works with Perls as old as 5.8.1.
936
937 Thread support is not very reliable before 5.10.1, but that's because
938 threads are not very reliable before 5.10.1.
939
940 Although Test::More has been a core module in versions of Perl since
941 5.6.2, Test::More has evolved since then, and not all of the features
942 you're used to will be present in the shipped version of Test::More. If
943 you are writing a module, don't forget to indicate in your package
944 metadata the minimum version of Test::More that you require. For
945 instance, if you want to use done_testing() but want your test script
946 to run on Perl 5.10.0, you will need to explicitly require Test::More >
947 0.88.
948
949 Key feature milestones include:
950
951 subtests
952 Subtests were released in Test::More 0.94, which came with Perl
953 5.12.0. Subtests did not implicitly call done_testing() until 0.96;
954 the first Perl with that fix was Perl 5.14.0 with 0.98.
955
956 done_testing()
957 This was released in Test::More 0.88 and first shipped with Perl in
958 5.10.1 as part of Test::More 0.92.
959
960 cmp_ok()
961 Although cmp_ok() was introduced in 0.40, 0.86 fixed an important
962 bug to make it safe for overloaded objects; the fixed first shipped
963 with Perl in 5.10.1 as part of Test::More 0.92.
964
965 new_ok() note() and explain()
966 These were was released in Test::More 0.82, and first shipped with
967 Perl in 5.10.1 as part of Test::More 0.92.
968
969 There is a full version history in the Changes file, and the Test::More
970 versions included as core can be found using Module::CoreList:
971
972 $ corelist -a Test::More
973
975 utf8 / "Wide character in print"
976 If you use utf8 or other non-ASCII characters with Test::More you
977 might get a "Wide character in print" warning. Using "binmode
978 STDOUT, ":utf8"" will not fix it. Test::Builder (which powers
979 Test::More) duplicates STDOUT and STDERR. So any changes to them,
980 including changing their output disciplines, will not be seen by
981 Test::More.
982
983 One work around is to apply encodings to STDOUT and STDERR as early
984 as possible and before Test::More (or any other Test module) loads.
985
986 use open ':std', ':encoding(utf8)';
987 use Test::More;
988
989 A more direct work around is to change the filehandles used by
990 Test::Builder.
991
992 my $builder = Test::More->builder;
993 binmode $builder->output, ":encoding(utf8)";
994 binmode $builder->failure_output, ":encoding(utf8)";
995 binmode $builder->todo_output, ":encoding(utf8)";
996
997 Overloaded objects
998 String overloaded objects are compared as strings (or in cmp_ok()'s
999 case, strings or numbers as appropriate to the comparison op).
1000 This prevents Test::More from piercing an object's interface
1001 allowing better blackbox testing. So if a function starts
1002 returning overloaded objects instead of bare strings your tests
1003 won't notice the difference. This is good.
1004
1005 However, it does mean that functions like is_deeply() cannot be
1006 used to test the internals of string overloaded objects. In this
1007 case I would suggest Test::Deep which contains more flexible
1008 testing functions for complex data structures.
1009
1010 Threads
1011 Test::More will only be aware of threads if "use threads" has been
1012 done before Test::More is loaded. This is ok:
1013
1014 use threads;
1015 use Test::More;
1016
1017 This may cause problems:
1018
1019 use Test::More
1020 use threads;
1021
1022 5.8.1 and above are supported. Anything below that has too many
1023 bugs.
1024
1026 This is a case of convergent evolution with Joshua Pritikin's Test
1027 module. I was largely unaware of its existence when I'd first written
1028 my own ok() routines. This module exists because I can't figure out
1029 how to easily wedge test names into Test's interface (along with a few
1030 other problems).
1031
1032 The goal here is to have a testing utility that's simple to learn,
1033 quick to use and difficult to trip yourself up with while still
1034 providing more flexibility than the existing Test.pm. As such, the
1035 names of the most common routines are kept tiny, special cases and
1036 magic side-effects are kept to a minimum. WYSIWYG.
1037
1039
1040 ALTERNATIVES
1041 Test2::Suite is the most recent and modern set of tools for testing.
1042
1043 Test::Simple if all this confuses you and you just want to write some
1044 tests. You can upgrade to Test::More later (it's forward compatible).
1045
1046 Test::Legacy tests written with Test.pm, the original testing module,
1047 do not play well with other testing libraries. Test::Legacy emulates
1048 the Test.pm interface and does play well with others.
1049
1050 ADDITIONAL LIBRARIES
1051 Test::Differences for more ways to test complex data structures. And
1052 it plays well with Test::More.
1053
1054 Test::Class is like xUnit but more perlish.
1055
1056 Test::Deep gives you more powerful complex data structure testing.
1057
1058 Test::Inline shows the idea of embedded testing.
1059
1060 Mock::Quick The ultimate mocking library. Easily spawn objects defined
1061 on the fly. Can also override, block, or reimplement packages as
1062 needed.
1063
1064 Test::FixtureBuilder Quickly define fixture data for unit tests.
1065
1066 OTHER COMPONENTS
1067 Test::Harness is the test runner and output interpreter for Perl. It's
1068 the thing that powers "make test" and where the "prove" utility comes
1069 from.
1070
1071 BUNDLES
1072 Test::Most Most commonly needed test functions and features.
1073
1075 Michael G Schwern <schwern@pobox.com> with much inspiration from Joshua
1076 Pritikin's Test module and lots of help from Barrie Slaymaker, Tony
1077 Bowden, blackstar.co.uk, chromatic, Fergal Daly and the perl-qa gang.
1078
1080 Chad Granum <exodist@cpan.org>
1081
1083 See https://github.com/Test-More/test-more/issues to report and view
1084 bugs.
1085
1087 The source code repository for Test::More can be found at
1088 http://github.com/Test-More/test-more/.
1089
1091 Copyright 2001-2008 by Michael G Schwern <schwern@pobox.com>.
1092
1093 This program is free software; you can redistribute it and/or modify it
1094 under the same terms as Perl itself.
1095
1096 See http://www.perl.com/perl/misc/Artistic.html
1097
1098
1099
1100perl v5.38.0 2023-07-21 Test::More(3)