Class-Object
view release on metacpan or search on metacpan
t/lib/Test/Simple.pm view on Meta::CPAN
ok( $foo eq $bar );
ok() is given an expression (in this case C<$foo eq $bar>). If its
true, the test passed. If its false, it didn't. That's about it.
ok() prints out either "ok" or "not ok" along with a test number (it
keeps track of that for you).
# This produces "ok 1 - Hell not yet frozen over" (or not ok)
ok( get_temperature($hell) > 0, 'Hell not yet frozen over' );
If you provide a $name, that will be printed along with the "ok/not
ok" to make it easier to find your test when if fails (just search for
the name). It also makes it easier for the next guy to understand
what your test is for. Its highly recommended you use test names.
All tests are run in scalar context. So this:
ok( @stuff, 'I have some stuff' );
will do what you mean (fail if stuff is empty).
=cut
sub ok ($;$) {
my($test, $name) = @_;
unless( $Have_Plan ) {
die "You tried to use ok() without a plan! Gotta have a plan.\n".
" use Test::Simple tests => 23; for example.\n";
}
$Num_Tests++;
# Make sure the print doesn't get interfered with.
local($\, $,);
_print *TESTERR, <<ERR if defined $name and $name !~ /\D/;
You named your test '$name'. You shouldn't use numbers for your test names.
Very confusing.
ERR
# We must print this all in one shot or else it will break on VMS
my $msg;
unless( $test ) {
$msg .= "not ";
$Test_Results[$Num_Tests-1] = 0;
}
else {
$Test_Results[$Num_Tests-1] = 1;
}
$msg .= "ok $Num_Tests";
$msg .= " - $name" if @_ == 2;
$msg .= "\n";
_print *TESTOUT, $msg;
#'#
unless( $test ) {
my($pack, $file, $line) = (caller)[0,1,2];
if( $pack eq 'Test::More' ) {
($file, $line) = (caller(1))[1,2];
}
_print *TESTERR, "# Failed test ($file at line $line)\n";
}
return $test;
}
=back
Test::Simple will start by printing number of tests run in the form
"1..M" (so "1..5" means you're going to run 5 tests). This strange
format lets Test::Harness know how many tests you plan on running in
case something goes horribly wrong.
If all your tests passed, Test::Simple will exit with zero (which is
normal). If anything failed it will exit with how many failed. If
you run less (or more) tests than you planned, the missing (or extras)
will be considered failures. If no tests were ever run Test::Simple
will throw a warning and exit with 255. If the test died, even after
having successfully completed all its tests, it will still be
considered a failure and will exit with 255.
So the exit codes are...
0 all tests successful
255 test died
any other number how many failed (including missing or extras)
If you fail more than 254 tests, it will be reported as 254.
=begin _private
=over 4
=item B<_sanity_check>
_sanity_check();
Runs a bunch of end of test sanity checks to make sure reality came
through ok. If anything is wrong it will die with a fairly friendly
error message.
=cut
#'#
sub _sanity_check {
_whoa($Num_Tests < 0, 'Says here you ran a negative number of tests!');
_whoa(!$Have_Plan and $Num_Tests,
'Somehow your tests ran without a plan!');
_whoa($Num_Tests != @Test_Results,
'Somehow you got a different number of results than tests ran!');
}
=item B<_whoa>
_whoa($check, $description);
A sanity check, similar to assert(). If the $check is true, something
( run in 4.930 seconds using v1.01-cache-2.11-cpan-5511b514fd6 )