sync blead with Update Archive::Extract 0.34
[p5sagit/p5-mst-13.2.git] / lib / Test / Simple.pm
CommitLineData
4dd974da 1package Test::Simple;
3c4bf434 2# $Id$
4dd974da 3
d020a79a 4use 5.004;
4dd974da 5
ccbd73a4 6use strict;
7
8f70d4fd 8our $VERSION = '0.86_01';
ccbd73a4 9$VERSION = eval $VERSION; ## no critic (BuiltinFunctions::ProhibitStringyEval)
d020a79a 10
b1ddf169 11use Test::Builder::Module;
ccbd73a4 12our @ISA = qw(Test::Builder::Module);
13our @EXPORT = qw(ok);
4dd974da 14
b1ddf169 15my $CLASS = __PACKAGE__;
4dd974da 16
4dd974da 17=head1 NAME
18
19Test::Simple - Basic utilities for writing tests.
20
21=head1 SYNOPSIS
22
23 use Test::Simple tests => 1;
24
25 ok( $foo eq $bar, 'foo is bar' );
26
27
28=head1 DESCRIPTION
29
d020a79a 30** If you are unfamiliar with testing B<read Test::Tutorial> first! **
31
4dd974da 32This is an extremely simple, extremely basic module for writing tests
d020a79a 33suitable for CPAN modules and other pursuits. If you wish to do more
34complicated testing, use the Test::More module (a drop-in replacement
35for this one).
4dd974da 36
37The basic unit of Perl testing is the ok. For each thing you want to
38test your program will print out an "ok" or "not ok" to indicate pass
39or fail. You do this with the ok() function (see below).
40
9631ec48 41The only other constraint is you must pre-declare how many tests you
4dd974da 42plan to run. This is in case something goes horribly wrong during the
43test and your test program aborts, or skips a test or whatever. You
44do this like so:
45
46 use Test::Simple tests => 23;
47
48You must have a plan.
49
50
51=over 4
52
53=item B<ok>
54
55 ok( $foo eq $bar, $name );
56 ok( $foo eq $bar );
57
89c1e84a 58ok() is given an expression (in this case C<$foo eq $bar>). If it's
59true, the test passed. If it's false, it didn't. That's about it.
4dd974da 60
61ok() prints out either "ok" or "not ok" along with a test number (it
62keeps track of that for you).
63
64 # This produces "ok 1 - Hell not yet frozen over" (or not ok)
65 ok( get_temperature($hell) > 0, 'Hell not yet frozen over' );
66
67If you provide a $name, that will be printed along with the "ok/not
68ok" to make it easier to find your test when if fails (just search for
69the name). It also makes it easier for the next guy to understand
89c1e84a 70what your test is for. It's highly recommended you use test names.
4dd974da 71
72All tests are run in scalar context. So this:
73
74 ok( @stuff, 'I have some stuff' );
75
d020a79a 76will do what you mean (fail if stuff is empty)
4dd974da 77
78=cut
79
ccbd73a4 80sub ok ($;$) { ## no critic (Subroutines::ProhibitSubroutinePrototypes)
81 return $CLASS->builder->ok(@_);
d020a79a 82}
83
4dd974da 84=back
85
86Test::Simple will start by printing number of tests run in the form
87"1..M" (so "1..5" means you're going to run 5 tests). This strange
88format lets Test::Harness know how many tests you plan on running in
89case something goes horribly wrong.
90
91If all your tests passed, Test::Simple will exit with zero (which is
92normal). If anything failed it will exit with how many failed. If
93you run less (or more) tests than you planned, the missing (or extras)
94will be considered failures. If no tests were ever run Test::Simple
95will throw a warning and exit with 255. If the test died, even after
96having successfully completed all its tests, it will still be
97considered a failure and will exit with 255.
98
99So the exit codes are...
100
101 0 all tests successful
b1ddf169 102 255 test died or all passed but wrong # of tests run
4dd974da 103 any other number how many failed (including missing or extras)
104
105If you fail more than 254 tests, it will be reported as 254.
106
4dd974da 107This module is by no means trying to be a complete testing system.
89c1e84a 108It's just to get you started. Once you're off the ground its
4dd974da 109recommended you look at L<Test::More>.
110
111
112=head1 EXAMPLE
113
114Here's an example of a simple .t file for the fictional Film module.
115
116 use Test::Simple tests => 5;
117
118 use Film; # What you're testing.
119
120 my $btaste = Film->new({ Title => 'Bad Taste',
121 Director => 'Peter Jackson',
122 Rating => 'R',
123 NumExplodingSheep => 1
124 });
5143c659 125 ok( defined($btaste) && ref $btaste eq 'Film, 'new() works' );
4dd974da 126
127 ok( $btaste->Title eq 'Bad Taste', 'Title() get' );
d020a79a 128 ok( $btaste->Director eq 'Peter Jackson', 'Director() get' );
4dd974da 129 ok( $btaste->Rating eq 'R', 'Rating() get' );
130 ok( $btaste->NumExplodingSheep == 1, 'NumExplodingSheep() get' );
131
132It will produce output like this:
133
134 1..5
135 ok 1 - new() works
136 ok 2 - Title() get
137 ok 3 - Director() get
138 not ok 4 - Rating() get
b1ddf169 139 # Failed test 'Rating() get'
140 # in t/film.t at line 14.
4dd974da 141 ok 5 - NumExplodingSheep() get
d020a79a 142 # Looks like you failed 1 tests of 5
4dd974da 143
144Indicating the Film::Rating() method is broken.
145
146
147=head1 CAVEATS
148
149Test::Simple will only report a maximum of 254 failures in its exit
150code. If this is a problem, you probably have a huge test script.
151Split it into multiple files. (Otherwise blame the Unix folks for
152using an unsigned short integer as the exit status).
153
d020a79a 154Because VMS's exit codes are much, much different than the rest of the
155universe, and perl does horrible mangling to them that gets in my way,
156it works like this on VMS.
157
158 0 SS$_NORMAL all tests successful
159 4 SS$_ABORT something went wrong
160
161Unfortunately, I can't differentiate any further.
162
163
164=head1 NOTES
165
166Test::Simple is B<explicitly> tested all the way back to perl 5.004.
167
a344be10 168Test::Simple is thread-safe in perl 5.8.0 and up.
4dd974da 169
170=head1 HISTORY
171
172This module was conceived while talking with Tony Bowden in his
173kitchen one night about the problems I was having writing some really
174complicated feature into the new Testing module. He observed that the
175main problem is not dealing with these edge cases but that people hate
176to write tests B<at all>. What was needed was a dead simple module
177that took all the hard work out of testing and was really, really easy
178to learn. Paul Johnson simultaneously had this idea (unfortunately,
179he wasn't in Tony's kitchen). This is it.
180
181
4dd974da 182=head1 SEE ALSO
183
184=over 4
185
186=item L<Test::More>
187
188More testing functions! Once you outgrow Test::Simple, look at
189Test::More. Test::Simple is 100% forward compatible with Test::More
9631ec48 190(i.e. you can just use Test::More instead of Test::Simple in your
4dd974da 191programs and things will still work).
192
4dd974da 193=back
194
ccbd73a4 195Look in Test::More's SEE ALSO for more testing modules.
196
9631ec48 197
198=head1 AUTHORS
199
200Idea by Tony Bowden and Paul Johnson, code by Michael G Schwern
201E<lt>schwern@pobox.comE<gt>, wardrobe by Calvin Klein.
202
203
204=head1 COPYRIGHT
205
ccbd73a4 206Copyright 2001-2008 by Michael G Schwern E<lt>schwern@pobox.comE<gt>.
9631ec48 207
208This program is free software; you can redistribute it and/or
209modify it under the same terms as Perl itself.
210
a9153838 211See F<http://www.perl.com/perl/misc/Artistic.html>
9631ec48 212
4dd974da 213=cut
214
2151;