Blame t/Test/Simple.pm

Packit Service 4a2782
package Test::Simple;
Packit Service 4a2782
Packit Service 4a2782
use 5.004;
Packit Service 4a2782
Packit Service 4a2782
use strict 'vars';
Packit Service 4a2782
our ($VERSION);
Packit Service 4a2782
$VERSION = '0.60';
Packit Service 4a2782
$VERSION = eval $VERSION;    # make the alpha version come out as a number
Packit Service 4a2782
Packit Service 4a2782
Packit Service 4a2782
use Test::Builder;
Packit Service 4a2782
my $Test = Test::Builder->new;
Packit Service 4a2782
Packit Service 4a2782
sub import {
Packit Service 4a2782
    my $self = shift;
Packit Service 4a2782
    my $caller = caller;
Packit Service 4a2782
    *{$caller.'::ok'} = \&ok;
Packit Service 4a2782
Packit Service 4a2782
    $Test->exported_to($caller);
Packit Service 4a2782
    $Test->plan(@_);
Packit Service 4a2782
}
Packit Service 4a2782
Packit Service 4a2782
Packit Service 4a2782
=head1 NAME
Packit Service 4a2782
Packit Service 4a2782
Test::Simple - Basic utilities for writing tests.
Packit Service 4a2782
Packit Service 4a2782
=head1 SYNOPSIS
Packit Service 4a2782
Packit Service 4a2782
  use Test::Simple tests => 1;
Packit Service 4a2782
Packit Service 4a2782
  ok( $foo eq $bar, 'foo is bar' );
Packit Service 4a2782
Packit Service 4a2782
Packit Service 4a2782
=head1 DESCRIPTION
Packit Service 4a2782
Packit Service 4a2782
** If you are unfamiliar with testing B<read Test::Tutorial> first! **
Packit Service 4a2782
Packit Service 4a2782
This is an extremely simple, extremely basic module for writing tests
Packit Service 4a2782
suitable for CPAN modules and other pursuits.  If you wish to do more
Packit Service 4a2782
complicated testing, use the Test::More module (a drop-in replacement
Packit Service 4a2782
for this one).
Packit Service 4a2782
Packit Service 4a2782
The basic unit of Perl testing is the ok.  For each thing you want to
Packit Service 4a2782
test your program will print out an "ok" or "not ok" to indicate pass
Packit Service 4a2782
or fail.  You do this with the ok() function (see below).
Packit Service 4a2782
Packit Service 4a2782
The only other constraint is you must pre-declare how many tests you
Packit Service 4a2782
plan to run.  This is in case something goes horribly wrong during the
Packit Service 4a2782
test and your test program aborts, or skips a test or whatever.  You
Packit Service 4a2782
do this like so:
Packit Service 4a2782
Packit Service 4a2782
    use Test::Simple tests => 23;
Packit Service 4a2782
Packit Service 4a2782
You must have a plan.
Packit Service 4a2782
Packit Service 4a2782
Packit Service 4a2782
=over 4
Packit Service 4a2782
Packit Service 4a2782
=item B<ok>
Packit Service 4a2782
Packit Service 4a2782
  ok( $foo eq $bar, $name );
Packit Service 4a2782
  ok( $foo eq $bar );
Packit Service 4a2782
Packit Service 4a2782
ok() is given an expression (in this case C<$foo eq $bar>).  If it's
Packit Service 4a2782
true, the test passed.  If it's false, it didn't.  That's about it.
Packit Service 4a2782
Packit Service 4a2782
ok() prints out either "ok" or "not ok" along with a test number (it
Packit Service 4a2782
keeps track of that for you).
Packit Service 4a2782
Packit Service 4a2782
  # This produces "ok 1 - Hell not yet frozen over" (or not ok)
Packit Service 4a2782
  ok( get_temperature($hell) > 0, 'Hell not yet frozen over' );
Packit Service 4a2782
Packit Service 4a2782
If you provide a $name, that will be printed along with the "ok/not
Packit Service 4a2782
ok" to make it easier to find your test when if fails (just search for
Packit Service 4a2782
the name).  It also makes it easier for the next guy to understand
Packit Service 4a2782
what your test is for.  It's highly recommended you use test names.
Packit Service 4a2782
Packit Service 4a2782
All tests are run in scalar context.  So this:
Packit Service 4a2782
Packit Service 4a2782
    ok( @stuff, 'I have some stuff' );
Packit Service 4a2782
Packit Service 4a2782
will do what you mean (fail if stuff is empty)
Packit Service 4a2782
Packit Service 4a2782
=cut
Packit Service 4a2782
Packit Service 4a2782
sub ok ($;$) {
Packit Service 4a2782
    $Test->ok(@_);
Packit Service 4a2782
}
Packit Service 4a2782
Packit Service 4a2782
Packit Service 4a2782
=back
Packit Service 4a2782
Packit Service 4a2782
Test::Simple will start by printing number of tests run in the form
Packit Service 4a2782
"1..M" (so "1..5" means you're going to run 5 tests).  This strange
Packit Service 4a2782
format lets Test::Harness know how many tests you plan on running in
Packit Service 4a2782
case something goes horribly wrong.
Packit Service 4a2782
Packit Service 4a2782
If all your tests passed, Test::Simple will exit with zero (which is
Packit Service 4a2782
normal).  If anything failed it will exit with how many failed.  If
Packit Service 4a2782
you run less (or more) tests than you planned, the missing (or extras)
Packit Service 4a2782
will be considered failures.  If no tests were ever run Test::Simple
Packit Service 4a2782
will throw a warning and exit with 255.  If the test died, even after
Packit Service 4a2782
having successfully completed all its tests, it will still be
Packit Service 4a2782
considered a failure and will exit with 255.
Packit Service 4a2782
Packit Service 4a2782
So the exit codes are...
Packit Service 4a2782
Packit Service 4a2782
    0                   all tests successful
Packit Service 4a2782
    255                 test died
Packit Service 4a2782
    any other number    how many failed (including missing or extras)
Packit Service 4a2782
Packit Service 4a2782
If you fail more than 254 tests, it will be reported as 254.
Packit Service 4a2782
Packit Service 4a2782
This module is by no means trying to be a complete testing system.
Packit Service 4a2782
It's just to get you started.  Once you're off the ground its
Packit Service 4a2782
recommended you look at L<Test::More>.
Packit Service 4a2782
Packit Service 4a2782
Packit Service 4a2782
=head1 EXAMPLE
Packit Service 4a2782
Packit Service 4a2782
Here's an example of a simple .t file for the fictional Film module.
Packit Service 4a2782
Packit Service 4a2782
    use Test::Simple tests => 5;
Packit Service 4a2782
Packit Service 4a2782
    use Film;  # What you're testing.
Packit Service 4a2782
Packit Service 4a2782
    my $btaste = Film->new({ Title    => 'Bad Taste',
Packit Service 4a2782
                             Director => 'Peter Jackson',
Packit Service 4a2782
                             Rating   => 'R',
Packit Service 4a2782
                             NumExplodingSheep => 1
Packit Service 4a2782
                           });
Packit Service 4a2782
    ok( defined($btaste) && ref $btaste eq 'Film,     'new() works' );
Packit Service 4a2782
Packit Service 4a2782
    ok( $btaste->Title      eq 'Bad Taste',     'Title() get'    );
Packit Service 4a2782
    ok( $btaste->Director   eq 'Peter Jackson', 'Director() get' );
Packit Service 4a2782
    ok( $btaste->Rating     eq 'R',             'Rating() get'   );
Packit Service 4a2782
    ok( $btaste->NumExplodingSheep == 1,        'NumExplodingSheep() get' );
Packit Service 4a2782
Packit Service 4a2782
It will produce output like this:
Packit Service 4a2782
Packit Service 4a2782
    1..5
Packit Service 4a2782
    ok 1 - new() works
Packit Service 4a2782
    ok 2 - Title() get
Packit Service 4a2782
    ok 3 - Director() get
Packit Service 4a2782
    not ok 4 - Rating() get
Packit Service 4a2782
    #    Failed test (t/film.t at line 14)
Packit Service 4a2782
    ok 5 - NumExplodingSheep() get
Packit Service 4a2782
    # Looks like you failed 1 tests of 5
Packit Service 4a2782
Packit Service 4a2782
Indicating the Film::Rating() method is broken.
Packit Service 4a2782
Packit Service 4a2782
Packit Service 4a2782
=head1 CAVEATS
Packit Service 4a2782
Packit Service 4a2782
Test::Simple will only report a maximum of 254 failures in its exit
Packit Service 4a2782
code.  If this is a problem, you probably have a huge test script.
Packit Service 4a2782
Split it into multiple files.  (Otherwise blame the Unix folks for
Packit Service 4a2782
using an unsigned short integer as the exit status).
Packit Service 4a2782
Packit Service 4a2782
Because VMS's exit codes are much, much different than the rest of the
Packit Service 4a2782
universe, and perl does horrible mangling to them that gets in my way,
Packit Service 4a2782
it works like this on VMS.
Packit Service 4a2782
Packit Service 4a2782
    0     SS$_NORMAL        all tests successful
Packit Service 4a2782
    4     SS$_ABORT         something went wrong
Packit Service 4a2782
Packit Service 4a2782
Unfortunately, I can't differentiate any further.
Packit Service 4a2782
Packit Service 4a2782
Packit Service 4a2782
=head1 NOTES
Packit Service 4a2782
Packit Service 4a2782
Test::Simple is B<explicitly> tested all the way back to perl 5.004.
Packit Service 4a2782
Packit Service 4a2782
Test::Simple is thread-safe in perl 5.8.0 and up.
Packit Service 4a2782
Packit Service 4a2782
=head1 HISTORY
Packit Service 4a2782
Packit Service 4a2782
This module was conceived while talking with Tony Bowden in his
Packit Service 4a2782
kitchen one night about the problems I was having writing some really
Packit Service 4a2782
complicated feature into the new Testing module.  He observed that the
Packit Service 4a2782
main problem is not dealing with these edge cases but that people hate
Packit Service 4a2782
to write tests B<at all>.  What was needed was a dead simple module
Packit Service 4a2782
that took all the hard work out of testing and was really, really easy
Packit Service 4a2782
to learn.  Paul Johnson simultaneously had this idea (unfortunately,
Packit Service 4a2782
he wasn't in Tony's kitchen).  This is it.
Packit Service 4a2782
Packit Service 4a2782
Packit Service 4a2782
=head1 SEE ALSO
Packit Service 4a2782
Packit Service 4a2782
=over 4
Packit Service 4a2782
Packit Service 4a2782
=item L<Test::More>
Packit Service 4a2782
Packit Service 4a2782
More testing functions!  Once you outgrow Test::Simple, look at
Packit Service 4a2782
Test::More.  Test::Simple is 100% forward compatible with Test::More
Packit Service 4a2782
(i.e. you can just use Test::More instead of Test::Simple in your
Packit Service 4a2782
programs and things will still work).
Packit Service 4a2782
Packit Service 4a2782
=item L<Test>
Packit Service 4a2782
Packit Service 4a2782
The original Perl testing module.
Packit Service 4a2782
Packit Service 4a2782
=item L<Test::Unit>
Packit Service 4a2782
Packit Service 4a2782
Elaborate unit testing.
Packit Service 4a2782
Packit Service 4a2782
=item L<Test::Inline>, L<SelfTest>
Packit Service 4a2782
Packit Service 4a2782
Embed tests in your code!
Packit Service 4a2782
Packit Service 4a2782
=item L<Test::Harness>
Packit Service 4a2782
Packit Service 4a2782
Interprets the output of your test program.
Packit Service 4a2782
Packit Service 4a2782
=back
Packit Service 4a2782
Packit Service 4a2782
Packit Service 4a2782
=head1 AUTHORS
Packit Service 4a2782
Packit Service 4a2782
Idea by Tony Bowden and Paul Johnson, code by Michael G Schwern
Packit Service 4a2782
E<lt>schwern@pobox.comE<gt>, wardrobe by Calvin Klein.
Packit Service 4a2782
Packit Service 4a2782
Packit Service 4a2782
=head1 COPYRIGHT
Packit Service 4a2782
Packit Service 4a2782
Copyright 2001, 2002, 2004 by Michael G Schwern E<lt>schwern@pobox.comE<gt>.
Packit Service 4a2782
Packit Service 4a2782
This program is free software; you can redistribute it and/or 
Packit Service 4a2782
modify it under the same terms as Perl itself.
Packit Service 4a2782
Packit Service 4a2782
See F<http://www.perl.com/perl/misc/Artistic.html>
Packit Service 4a2782
Packit Service 4a2782
=cut
Packit Service 4a2782
Packit Service 4a2782
1;