8 $VERSION = eval $VERSION; ## no critic (BuiltinFunctions::ProhibitStringyEval)
10 use Test::Builder::Module;
11 our @ISA = qw(Test::Builder::Module);
14 my $CLASS = __PACKAGE__;
18 Test::Simple - Basic utilities for writing tests.
22 use Test::Simple tests => 1;
24 ok( $foo eq $bar, 'foo is bar' );
29 ** If you are unfamiliar with testing B<read Test::Tutorial> first! **
31 This is an extremely simple, extremely basic module for writing tests
32 suitable for CPAN modules and other pursuits. If you wish to do more
33 complicated testing, use the Test::More module (a drop-in replacement
36 The basic unit of Perl testing is the ok. For each thing you want to
37 test your program will print out an "ok" or "not ok" to indicate pass
38 or fail. You do this with the ok() function (see below).
40 The only other constraint is you must pre-declare how many tests you
41 plan to run. This is in case something goes horribly wrong during the
42 test and your test program aborts, or skips a test or whatever. You
45 use Test::Simple tests => 23;
54 ok( $foo eq $bar, $name );
57 ok() is given an expression (in this case C<$foo eq $bar>). If it's
58 true, the test passed. If it's false, it didn't. That's about it.
60 ok() prints out either "ok" or "not ok" along with a test number (it
61 keeps track of that for you).
63 # This produces "ok 1 - Hell not yet frozen over" (or not ok)
64 ok( get_temperature($hell) > 0, 'Hell not yet frozen over' );
66 If you provide a $name, that will be printed along with the "ok/not
67 ok" to make it easier to find your test when if fails (just search for
68 the name). It also makes it easier for the next guy to understand
69 what your test is for. It's highly recommended you use test names.
71 All tests are run in scalar context. So this:
73 ok( @stuff, 'I have some stuff' );
75 will do what you mean (fail if stuff is empty)
79 sub ok ($;$) { ## no critic (Subroutines::ProhibitSubroutinePrototypes)
80 return $CLASS->builder->ok(@_);
85 Test::Simple will start by printing number of tests run in the form
86 "1..M" (so "1..5" means you're going to run 5 tests). This strange
87 format lets Test::Harness know how many tests you plan on running in
88 case something goes horribly wrong.
90 If all your tests passed, Test::Simple will exit with zero (which is
91 normal). If anything failed it will exit with how many failed. If
92 you run less (or more) tests than you planned, the missing (or extras)
93 will be considered failures. If no tests were ever run Test::Simple
94 will throw a warning and exit with 255. If the test died, even after
95 having successfully completed all its tests, it will still be
96 considered a failure and will exit with 255.
98 So the exit codes are...
100 0 all tests successful
101 255 test died or all passed but wrong # of tests run
102 any other number how many failed (including missing or extras)
104 If you fail more than 254 tests, it will be reported as 254.
106 This module is by no means trying to be a complete testing system.
107 It's just to get you started. Once you're off the ground its
108 recommended you look at L<Test::More>.
113 Here's an example of a simple .t file for the fictional Film module.
115 use Test::Simple tests => 5;
117 use Film; # What you're testing.
119 my $btaste = Film->new({ Title => 'Bad Taste',
120 Director => 'Peter Jackson',
122 NumExplodingSheep => 1
124 ok( defined($btaste) && ref $btaste eq 'Film, 'new() works' );
126 ok( $btaste->Title eq 'Bad Taste', 'Title() get' );
127 ok( $btaste->Director eq 'Peter Jackson', 'Director() get' );
128 ok( $btaste->Rating eq 'R', 'Rating() get' );
129 ok( $btaste->NumExplodingSheep == 1, 'NumExplodingSheep() get' );
131 It will produce output like this:
136 ok 3 - Director() get
137 not ok 4 - Rating() get
138 # Failed test 'Rating() get'
139 # in t/film.t at line 14.
140 ok 5 - NumExplodingSheep() get
141 # Looks like you failed 1 tests of 5
143 Indicating the Film::Rating() method is broken.
148 Test::Simple will only report a maximum of 254 failures in its exit
149 code. If this is a problem, you probably have a huge test script.
150 Split it into multiple files. (Otherwise blame the Unix folks for
151 using an unsigned short integer as the exit status).
153 Because VMS's exit codes are much, much different than the rest of the
154 universe, and perl does horrible mangling to them that gets in my way,
155 it works like this on VMS.
157 0 SS$_NORMAL all tests successful
158 4 SS$_ABORT something went wrong
160 Unfortunately, I can't differentiate any further.
165 Test::Simple is B<explicitly> tested all the way back to perl 5.004.
167 Test::Simple is thread-safe in perl 5.8.0 and up.
171 This module was conceived while talking with Tony Bowden in his
172 kitchen one night about the problems I was having writing some really
173 complicated feature into the new Testing module. He observed that the
174 main problem is not dealing with these edge cases but that people hate
175 to write tests B<at all>. What was needed was a dead simple module
176 that took all the hard work out of testing and was really, really easy
177 to learn. Paul Johnson simultaneously had this idea (unfortunately,
178 he wasn't in Tony's kitchen). This is it.
187 More testing functions! Once you outgrow Test::Simple, look at
188 Test::More. Test::Simple is 100% forward compatible with Test::More
189 (i.e. you can just use Test::More instead of Test::Simple in your
190 programs and things will still work).
194 Look in Test::More's SEE ALSO for more testing modules.
199 Idea by Tony Bowden and Paul Johnson, code by Michael G Schwern
200 E<lt>schwern@pobox.comE<gt>, wardrobe by Calvin Klein.
205 Copyright 2001-2008 by Michael G Schwern E<lt>schwern@pobox.comE<gt>.
207 This program is free software; you can redistribute it and/or
208 modify it under the same terms as Perl itself.
210 See F<http://www.perl.com/perl/misc/Artistic.html>