8 $VERSION = eval $VERSION; # make the alpha version come out as a number
12 my $Test = Test::Builder->new;
17 *{$caller.'::ok'} = \&ok;
19 $Test->exported_to($caller);
26 Test::Simple - Basic utilities for writing tests.
30 use Test::Simple tests => 1;
32 ok( $foo eq $bar, 'foo is bar' );
37 ** If you are unfamiliar with testing B<read Test::Tutorial> first! **
39 This is an extremely simple, extremely basic module for writing tests
40 suitable for CPAN modules and other pursuits. If you wish to do more
41 complicated testing, use the Test::More module (a drop-in replacement
44 The basic unit of Perl testing is the ok. For each thing you want to
45 test your program will print out an "ok" or "not ok" to indicate pass
46 or fail. You do this with the ok() function (see below).
48 The only other constraint is you must pre-declare how many tests you
49 plan to run. This is in case something goes horribly wrong during the
50 test and your test program aborts, or skips a test or whatever. You
53 use Test::Simple tests => 23;
62 ok( $foo eq $bar, $name );
65 ok() is given an expression (in this case C<$foo eq $bar>). If it's
66 true, the test passed. If it's false, it didn't. That's about it.
68 ok() prints out either "ok" or "not ok" along with a test number (it
69 keeps track of that for you).
71 # This produces "ok 1 - Hell not yet frozen over" (or not ok)
72 ok( get_temperature($hell) > 0, 'Hell not yet frozen over' );
74 If you provide a $name, that will be printed along with the "ok/not
75 ok" to make it easier to find your test when if fails (just search for
76 the name). It also makes it easier for the next guy to understand
77 what your test is for. It's highly recommended you use test names.
79 All tests are run in scalar context. So this:
81 ok( @stuff, 'I have some stuff' );
83 will do what you mean (fail if stuff is empty)
94 Test::Simple will start by printing number of tests run in the form
95 "1..M" (so "1..5" means you're going to run 5 tests). This strange
96 format lets Test::Harness know how many tests you plan on running in
97 case something goes horribly wrong.
99 If all your tests passed, Test::Simple will exit with zero (which is
100 normal). If anything failed it will exit with how many failed. If
101 you run less (or more) tests than you planned, the missing (or extras)
102 will be considered failures. If no tests were ever run Test::Simple
103 will throw a warning and exit with 255. If the test died, even after
104 having successfully completed all its tests, it will still be
105 considered a failure and will exit with 255.
107 So the exit codes are...
109 0 all tests successful
111 any other number how many failed (including missing or extras)
113 If you fail more than 254 tests, it will be reported as 254.
115 This module is by no means trying to be a complete testing system.
116 It's just to get you started. Once you're off the ground its
117 recommended you look at L<Test::More>.
122 Here's an example of a simple .t file for the fictional Film module.
124 use Test::Simple tests => 5;
126 use Film; # What you're testing.
128 my $btaste = Film->new({ Title => 'Bad Taste',
129 Director => 'Peter Jackson',
131 NumExplodingSheep => 1
133 ok( defined($btaste) and ref $btaste eq 'Film', 'new() works' );
135 ok( $btaste->Title eq 'Bad Taste', 'Title() get' );
136 ok( $btaste->Director eq 'Peter Jackson', 'Director() get' );
137 ok( $btaste->Rating eq 'R', 'Rating() get' );
138 ok( $btaste->NumExplodingSheep == 1, 'NumExplodingSheep() get' );
140 It will produce output like this:
145 ok 3 - Director() get
146 not ok 4 - Rating() get
147 # Failed test (t/film.t at line 14)
148 ok 5 - NumExplodingSheep() get
149 # Looks like you failed 1 tests of 5
151 Indicating the Film::Rating() method is broken.
156 Test::Simple will only report a maximum of 254 failures in its exit
157 code. If this is a problem, you probably have a huge test script.
158 Split it into multiple files. (Otherwise blame the Unix folks for
159 using an unsigned short integer as the exit status).
161 Because VMS's exit codes are much, much different than the rest of the
162 universe, and perl does horrible mangling to them that gets in my way,
163 it works like this on VMS.
165 0 SS$_NORMAL all tests successful
166 4 SS$_ABORT something went wrong
168 Unfortunately, I can't differentiate any further.
173 Test::Simple is B<explicitly> tested all the way back to perl 5.004.
175 Test::Simple is thread-safe in perl 5.8.0 and up.
179 This module was conceived while talking with Tony Bowden in his
180 kitchen one night about the problems I was having writing some really
181 complicated feature into the new Testing module. He observed that the
182 main problem is not dealing with these edge cases but that people hate
183 to write tests B<at all>. What was needed was a dead simple module
184 that took all the hard work out of testing and was really, really easy
185 to learn. Paul Johnson simultaneously had this idea (unfortunately,
186 he wasn't in Tony's kitchen). This is it.
195 More testing functions! Once you outgrow Test::Simple, look at
196 Test::More. Test::Simple is 100% forward compatible with Test::More
197 (i.e. you can just use Test::More instead of Test::Simple in your
198 programs and things will still work).
202 The original Perl testing module.
206 Elaborate unit testing.
208 =item L<Test::Inline>, L<SelfTest>
210 Embed tests in your code!
212 =item L<Test::Harness>
214 Interprets the output of your test program.
221 Idea by Tony Bowden and Paul Johnson, code by Michael G Schwern
222 E<lt>schwern@pobox.comE<gt>, wardrobe by Calvin Klein.
227 Copyright 2001, 2002, 2004 by Michael G Schwern E<lt>schwern@pobox.comE<gt>.
229 This program is free software; you can redistribute it and/or
230 modify it under the same terms as Perl itself.
232 See F<http://www.perl.com/perl/misc/Artistic.html>