11
my $Test = Test::Builder->new;
16
*{$caller.'::ok'} = \&ok;
18
$Test->exported_to($caller);
25
Test::Simple - Basic utilities for writing tests.
29
use Test::Simple tests => 1;
31
ok( $foo eq $bar, 'foo is bar' );
36
** If you are unfamiliar with testing B<read Test::Tutorial> first! **
38
This is an extremely simple, extremely basic module for writing tests
39
suitable for CPAN modules and other pursuits. If you wish to do more
40
complicated testing, use the Test::More module (a drop-in replacement
43
The basic unit of Perl testing is the ok. For each thing you want to
44
test your program will print out an "ok" or "not ok" to indicate pass
45
or fail. You do this with the ok() function (see below).
47
The only other constraint is you must pre-declare how many tests you
48
plan to run. This is in case something goes horribly wrong during the
49
test and your test program aborts, or skips a test or whatever. You
52
use Test::Simple tests => 23;
61
ok( $foo eq $bar, $name );
64
ok() is given an expression (in this case C<$foo eq $bar>). If it's
65
true, the test passed. If it's false, it didn't. That's about it.
67
ok() prints out either "ok" or "not ok" along with a test number (it
68
keeps track of that for you).
70
# This produces "ok 1 - Hell not yet frozen over" (or not ok)
71
ok( get_temperature($hell) > 0, 'Hell not yet frozen over' );
73
If you provide a $name, that will be printed along with the "ok/not
74
ok" to make it easier to find your test when if fails (just search for
75
the name). It also makes it easier for the next guy to understand
76
what your test is for. It's highly recommended you use test names.
78
All tests are run in scalar context. So this:
80
ok( @stuff, 'I have some stuff' );
82
will do what you mean (fail if stuff is empty)
93
Test::Simple will start by printing number of tests run in the form
94
"1..M" (so "1..5" means you're going to run 5 tests). This strange
95
format lets Test::Harness know how many tests you plan on running in
96
case something goes horribly wrong.
98
If all your tests passed, Test::Simple will exit with zero (which is
99
normal). If anything failed it will exit with how many failed. If
100
you run less (or more) tests than you planned, the missing (or extras)
101
will be considered failures. If no tests were ever run Test::Simple
102
will throw a warning and exit with 255. If the test died, even after
103
having successfully completed all its tests, it will still be
104
considered a failure and will exit with 255.
106
So the exit codes are...
108
0 all tests successful
110
any other number how many failed (including missing or extras)
112
If you fail more than 254 tests, it will be reported as 254.
114
This module is by no means trying to be a complete testing system.
115
It's just to get you started. Once you're off the ground its
116
recommended you look at L<Test::More>.
121
Here's an example of a simple .t file for the fictional Film module.
123
use Test::Simple tests => 5;
125
use Film; # What you're testing.
127
my $btaste = Film->new({ Title => 'Bad Taste',
128
Director => 'Peter Jackson',
130
NumExplodingSheep => 1
132
ok( defined($btaste) and ref $btaste eq 'Film', 'new() works' );
134
ok( $btaste->Title eq 'Bad Taste', 'Title() get' );
135
ok( $btaste->Director eq 'Peter Jackson', 'Director() get' );
136
ok( $btaste->Rating eq 'R', 'Rating() get' );
137
ok( $btaste->NumExplodingSheep == 1, 'NumExplodingSheep() get' );
139
It will produce output like this:
144
ok 3 - Director() get
145
not ok 4 - Rating() get
146
# Failed test (t/film.t at line 14)
147
ok 5 - NumExplodingSheep() get
148
# Looks like you failed 1 tests of 5
150
Indicating the Film::Rating() method is broken.
155
Test::Simple will only report a maximum of 254 failures in its exit
156
code. If this is a problem, you probably have a huge test script.
157
Split it into multiple files. (Otherwise blame the Unix folks for
158
using an unsigned short integer as the exit status).
160
Because VMS's exit codes are much, much different than the rest of the
161
universe, and perl does horrible mangling to them that gets in my way,
162
it works like this on VMS.
164
0 SS$_NORMAL all tests successful
165
4 SS$_ABORT something went wrong
167
Unfortunately, I can't differentiate any further.
172
Test::Simple is B<explicitly> tested all the way back to perl 5.004.
174
Test::Simple is thread-safe in perl 5.8.0 and up.
178
This module was conceived while talking with Tony Bowden in his
179
kitchen one night about the problems I was having writing some really
180
complicated feature into the new Testing module. He observed that the
181
main problem is not dealing with these edge cases but that people hate
182
to write tests B<at all>. What was needed was a dead simple module
183
that took all the hard work out of testing and was really, really easy
184
to learn. Paul Johnson simultaneously had this idea (unfortunately,
185
he wasn't in Tony's kitchen). This is it.
194
More testing functions! Once you outgrow Test::Simple, look at
195
Test::More. Test::Simple is 100% forward compatible with Test::More
196
(i.e. you can just use Test::More instead of Test::Simple in your
197
programs and things will still work).
201
The original Perl testing module.
205
Elaborate unit testing.
207
=item L<Test::Inline>, L<SelfTest>
209
Embed tests in your code!
211
=item L<Test::Harness>
213
Interprets the output of your test program.
220
Idea by Tony Bowden and Paul Johnson, code by Michael G Schwern
221
E<lt>schwern@pobox.comE<gt>, wardrobe by Calvin Klein.
226
Copyright 2001 by Michael G Schwern E<lt>schwern@pobox.comE<gt>.
228
This program is free software; you can redistribute it and/or
229
modify it under the same terms as Perl itself.
231
See F<http://www.perl.com/perl/misc/Artistic.html>