xref: /openbsd-src/gnu/usr.bin/perl/cpan/Test-Simple/lib/Test/Simple.pm (revision 3d61058aa5c692477b6d18acfbbdb653a9930ff9)
1b39c5158Smillertpackage Test::Simple;
2b39c5158Smillert
3b39c5158Smillertuse 5.006;
4b39c5158Smillert
5b39c5158Smillertuse strict;
6b39c5158Smillert
7*3d61058aSafresh1our $VERSION = '1.302199';
8b39c5158Smillert
99f11ffb7Safresh1use Test::Builder::Module;
10b39c5158Smillertour @ISA    = qw(Test::Builder::Module);
11b39c5158Smillertour @EXPORT = qw(ok);
12b39c5158Smillert
13b39c5158Smillertmy $CLASS = __PACKAGE__;
14b39c5158Smillert
15b39c5158Smillert=head1 NAME
16b39c5158Smillert
17b39c5158SmillertTest::Simple - Basic utilities for writing tests.
18b39c5158Smillert
19b39c5158Smillert=head1 SYNOPSIS
20b39c5158Smillert
21b39c5158Smillert  use Test::Simple tests => 1;
22b39c5158Smillert
23b39c5158Smillert  ok( $foo eq $bar, 'foo is bar' );
24b39c5158Smillert
25b39c5158Smillert
26b39c5158Smillert=head1 DESCRIPTION
27b39c5158Smillert
28b8851fccSafresh1** If you are unfamiliar with testing B<read L<Test::Tutorial> first!> **
29b39c5158Smillert
30b39c5158SmillertThis is an extremely simple, extremely basic module for writing tests
31b39c5158Smillertsuitable for CPAN modules and other pursuits.  If you wish to do more
32b39c5158Smillertcomplicated testing, use the Test::More module (a drop-in replacement
33b39c5158Smillertfor this one).
34b39c5158Smillert
35b39c5158SmillertThe basic unit of Perl testing is the ok.  For each thing you want to
36b39c5158Smillerttest your program will print out an "ok" or "not ok" to indicate pass
37b8851fccSafresh1or fail.  You do this with the C<ok()> function (see below).
38b39c5158Smillert
39b39c5158SmillertThe only other constraint is you must pre-declare how many tests you
40b39c5158Smillertplan to run.  This is in case something goes horribly wrong during the
41b39c5158Smillerttest and your test program aborts, or skips a test or whatever.  You
42b39c5158Smillertdo this like so:
43b39c5158Smillert
44b39c5158Smillert    use Test::Simple tests => 23;
45b39c5158Smillert
46b39c5158SmillertYou must have a plan.
47b39c5158Smillert
48b39c5158Smillert
49b39c5158Smillert=over 4
50b39c5158Smillert
51b39c5158Smillert=item B<ok>
52b39c5158Smillert
53b39c5158Smillert  ok( $foo eq $bar, $name );
54b39c5158Smillert  ok( $foo eq $bar );
55b39c5158Smillert
56b8851fccSafresh1C<ok()> is given an expression (in this case C<$foo eq $bar>).  If it's
57b39c5158Smillerttrue, the test passed.  If it's false, it didn't.  That's about it.
58b39c5158Smillert
59b8851fccSafresh1C<ok()> prints out either "ok" or "not ok" along with a test number (it
60b39c5158Smillertkeeps track of that for you).
61b39c5158Smillert
62b39c5158Smillert  # This produces "ok 1 - Hell not yet frozen over" (or not ok)
63b39c5158Smillert  ok( get_temperature($hell) > 0, 'Hell not yet frozen over' );
64b39c5158Smillert
65b39c5158SmillertIf you provide a $name, that will be printed along with the "ok/not
66b39c5158Smillertok" to make it easier to find your test when if fails (just search for
67b39c5158Smillertthe name).  It also makes it easier for the next guy to understand
68b39c5158Smillertwhat your test is for.  It's highly recommended you use test names.
69b39c5158Smillert
70b39c5158SmillertAll tests are run in scalar context.  So this:
71b39c5158Smillert
72b39c5158Smillert    ok( @stuff, 'I have some stuff' );
73b39c5158Smillert
74b39c5158Smillertwill do what you mean (fail if stuff is empty)
75b39c5158Smillert
76b39c5158Smillert=cut
77b39c5158Smillert
78b39c5158Smillertsub ok ($;$) {    ## no critic (Subroutines::ProhibitSubroutinePrototypes)
79b39c5158Smillert    return $CLASS->builder->ok(@_);
80b39c5158Smillert}
81b39c5158Smillert
82b39c5158Smillert=back
83b39c5158Smillert
84b39c5158SmillertTest::Simple will start by printing number of tests run in the form
85b39c5158Smillert"1..M" (so "1..5" means you're going to run 5 tests).  This strange
86b8851fccSafresh1format lets L<Test::Harness> know how many tests you plan on running in
87b39c5158Smillertcase something goes horribly wrong.
88b39c5158Smillert
89b39c5158SmillertIf all your tests passed, Test::Simple will exit with zero (which is
90b39c5158Smillertnormal).  If anything failed it will exit with how many failed.  If
91b39c5158Smillertyou run less (or more) tests than you planned, the missing (or extras)
92b39c5158Smillertwill be considered failures.  If no tests were ever run Test::Simple
93b39c5158Smillertwill throw a warning and exit with 255.  If the test died, even after
94b39c5158Smillerthaving successfully completed all its tests, it will still be
95b39c5158Smillertconsidered a failure and will exit with 255.
96b39c5158Smillert
97b39c5158SmillertSo the exit codes are...
98b39c5158Smillert
99b39c5158Smillert    0                   all tests successful
100b39c5158Smillert    255                 test died or all passed but wrong # of tests run
101b39c5158Smillert    any other number    how many failed (including missing or extras)
102b39c5158Smillert
103b39c5158SmillertIf you fail more than 254 tests, it will be reported as 254.
104b39c5158Smillert
105b39c5158SmillertThis module is by no means trying to be a complete testing system.
106b39c5158SmillertIt's just to get you started.  Once you're off the ground its
107b39c5158Smillertrecommended you look at L<Test::More>.
108b39c5158Smillert
109b39c5158Smillert
110b39c5158Smillert=head1 EXAMPLE
111b39c5158Smillert
112b39c5158SmillertHere's an example of a simple .t file for the fictional Film module.
113b39c5158Smillert
114b39c5158Smillert    use Test::Simple tests => 5;
115b39c5158Smillert
116b39c5158Smillert    use Film;  # What you're testing.
117b39c5158Smillert
118b39c5158Smillert    my $btaste = Film->new({ Title    => 'Bad Taste',
119b39c5158Smillert                             Director => 'Peter Jackson',
120b39c5158Smillert                             Rating   => 'R',
121b39c5158Smillert                             NumExplodingSheep => 1
122b39c5158Smillert                           });
12365d9bffcSjasper    ok( defined($btaste) && ref $btaste eq 'Film',     'new() works' );
124b39c5158Smillert
125b39c5158Smillert    ok( $btaste->Title      eq 'Bad Taste',     'Title() get'    );
126b39c5158Smillert    ok( $btaste->Director   eq 'Peter Jackson', 'Director() get' );
127b39c5158Smillert    ok( $btaste->Rating     eq 'R',             'Rating() get'   );
128b39c5158Smillert    ok( $btaste->NumExplodingSheep == 1,        'NumExplodingSheep() get' );
129b39c5158Smillert
130b39c5158SmillertIt will produce output like this:
131b39c5158Smillert
132b39c5158Smillert    1..5
133b39c5158Smillert    ok 1 - new() works
134b39c5158Smillert    ok 2 - Title() get
135b39c5158Smillert    ok 3 - Director() get
136b39c5158Smillert    not ok 4 - Rating() get
137b39c5158Smillert    #   Failed test 'Rating() get'
138b39c5158Smillert    #   in t/film.t at line 14.
139b39c5158Smillert    ok 5 - NumExplodingSheep() get
140b39c5158Smillert    # Looks like you failed 1 tests of 5
141b39c5158Smillert
142b39c5158SmillertIndicating the Film::Rating() method is broken.
143b39c5158Smillert
144b39c5158Smillert
145b39c5158Smillert=head1 CAVEATS
146b39c5158Smillert
147b39c5158SmillertTest::Simple will only report a maximum of 254 failures in its exit
148b39c5158Smillertcode.  If this is a problem, you probably have a huge test script.
149b39c5158SmillertSplit it into multiple files.  (Otherwise blame the Unix folks for
150b39c5158Smillertusing an unsigned short integer as the exit status).
151b39c5158Smillert
152b39c5158SmillertBecause VMS's exit codes are much, much different than the rest of the
153b39c5158Smillertuniverse, and perl does horrible mangling to them that gets in my way,
154b39c5158Smillertit works like this on VMS.
155b39c5158Smillert
156b39c5158Smillert    0     SS$_NORMAL        all tests successful
157b39c5158Smillert    4     SS$_ABORT         something went wrong
158b39c5158Smillert
159b39c5158SmillertUnfortunately, I can't differentiate any further.
160b39c5158Smillert
161b39c5158Smillert
162b39c5158Smillert=head1 NOTES
163b39c5158Smillert
164b39c5158SmillertTest::Simple is B<explicitly> tested all the way back to perl 5.6.0.
165b39c5158Smillert
166b39c5158SmillertTest::Simple is thread-safe in perl 5.8.1 and up.
167b39c5158Smillert
168b39c5158Smillert=head1 HISTORY
169b39c5158Smillert
170b39c5158SmillertThis module was conceived while talking with Tony Bowden in his
171b39c5158Smillertkitchen one night about the problems I was having writing some really
172b39c5158Smillertcomplicated feature into the new Testing module.  He observed that the
173b39c5158Smillertmain problem is not dealing with these edge cases but that people hate
174b39c5158Smillertto write tests B<at all>.  What was needed was a dead simple module
175b39c5158Smillertthat took all the hard work out of testing and was really, really easy
176b39c5158Smillertto learn.  Paul Johnson simultaneously had this idea (unfortunately,
177b39c5158Smillerthe wasn't in Tony's kitchen).  This is it.
178b39c5158Smillert
179b39c5158Smillert
180b39c5158Smillert=head1 SEE ALSO
181b39c5158Smillert
182b39c5158Smillert=over 4
183b39c5158Smillert
184b39c5158Smillert=item L<Test::More>
185b39c5158Smillert
186b39c5158SmillertMore testing functions!  Once you outgrow Test::Simple, look at
187b8851fccSafresh1L<Test::More>.  Test::Simple is 100% forward compatible with L<Test::More>
188b8851fccSafresh1(i.e. you can just use L<Test::More> instead of Test::Simple in your
189b39c5158Smillertprograms and things will still work).
190b39c5158Smillert
191b39c5158Smillert=back
192b39c5158Smillert
193b8851fccSafresh1Look in L<Test::More>'s SEE ALSO for more testing modules.
194b39c5158Smillert
195b39c5158Smillert
196b39c5158Smillert=head1 AUTHORS
197b39c5158Smillert
198b39c5158SmillertIdea by Tony Bowden and Paul Johnson, code by Michael G Schwern
199b39c5158SmillertE<lt>schwern@pobox.comE<gt>, wardrobe by Calvin Klein.
200b39c5158Smillert
201b8851fccSafresh1=head1 MAINTAINERS
202b8851fccSafresh1
203b8851fccSafresh1=over 4
204b8851fccSafresh1
205b8851fccSafresh1=item Chad Granum E<lt>exodist@cpan.orgE<gt>
206b8851fccSafresh1
207b8851fccSafresh1=back
208b39c5158Smillert
209b39c5158Smillert=head1 COPYRIGHT
210b39c5158Smillert
211b39c5158SmillertCopyright 2001-2008 by Michael G Schwern E<lt>schwern@pobox.comE<gt>.
212b39c5158Smillert
213b39c5158SmillertThis program is free software; you can redistribute it and/or
214b39c5158Smillertmodify it under the same terms as Perl itself.
215b39c5158Smillert
216*3d61058aSafresh1See L<https://dev.perl.org/licenses/>
217b39c5158Smillert
218b39c5158Smillert=cut
219b39c5158Smillert
220b39c5158Smillert1;
221