📄 simple.pm
字号:
package Test::Simple;use 5.004;use strict 'vars';use vars qw($VERSION);$VERSION = '0.42';use Test::Builder;my $Test = Test::Builder->new;sub import { my $self = shift; my $caller = caller; *{$caller.'::ok'} = \&ok; $Test->exported_to($caller); $Test->plan(@_);}=head1 NAMETest::Simple - Basic utilities for writing tests.=head1 SYNOPSIS use Test::Simple tests => 1; ok( $foo eq $bar, 'foo is bar' );=head1 DESCRIPTION** If you are unfamiliar with testing B<read Test::Tutorial> first! **This is an extremely simple, extremely basic module for writing testssuitable for CPAN modules and other pursuits. If you wish to do morecomplicated testing, use the Test::More module (a drop-in replacementfor this one).The basic unit of Perl testing is the ok. For each thing you want totest your program will print out an "ok" or "not ok" to indicate passor fail. You do this with the ok() function (see below).The only other constraint is you must pre-declare how many tests youplan to run. This is in case something goes horribly wrong during thetest and your test program aborts, or skips a test or whatever. Youdo this like so: use Test::Simple tests => 23;You must have a plan.=over 4=item B<ok> ok( $foo eq $bar, $name ); ok( $foo eq $bar );ok() is given an expression (in this case C<$foo eq $bar>). If itstrue, 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 (itkeeps 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/notok" to make it easier to find your test when if fails (just search forthe name). It also makes it easier for the next guy to understandwhat 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)=cutsub ok ($;$) { $Test->ok(@_);}=backTest::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 strangeformat lets Test::Harness know how many tests you plan on running incase something goes horribly wrong.If all your tests passed, Test::Simple will exit with zero (which isnormal). If anything failed it will exit with how many failed. Ifyou run less (or more) tests than you planned, the missing (or extras)will be considered failures. If no tests were ever run Test::Simplewill throw a warning and exit with 255. If the test died, even afterhaving successfully completed all its tests, it will still beconsidered 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.This module is by no means trying to be a complete testing system.Its just to get you started. Once you're off the ground itsrecommended you look at L<Test::More>.=head1 EXAMPLEHere's an example of a simple .t file for the fictional Film module. use Test::Simple tests => 5; use Film; # What you're testing. my $btaste = Film->new({ Title => 'Bad Taste', Director => 'Peter Jackson', Rating => 'R', NumExplodingSheep => 1 }); ok( defined($btaste) and ref $btaste eq 'Film', 'new() works' ); ok( $btaste->Title eq 'Bad Taste', 'Title() get' ); ok( $btaste->Director eq 'Peter Jackson', 'Director() get' ); ok( $btaste->Rating eq 'R', 'Rating() get' ); ok( $btaste->NumExplodingSheep == 1, 'NumExplodingSheep() get' );It will produce output like this: 1..5 ok 1 - new() works ok 2 - Title() get ok 3 - Director() get not ok 4 - Rating() get # Failed test (t/film.t at line 14) ok 5 - NumExplodingSheep() get # Looks like you failed 1 tests of 5Indicating the Film::Rating() method is broken.=head1 CAVEATSTest::Simple will only report a maximum of 254 failures in its exitcode. If this is a problem, you probably have a huge test script.Split it into multiple files. (Otherwise blame the Unix folks forusing an unsigned short integer as the exit status).Because VMS's exit codes are much, much different than the rest of theuniverse, and perl does horrible mangling to them that gets in my way,it works like this on VMS. 0 SS$_NORMAL all tests successful 4 SS$_ABORT something went wrongUnfortunately, I can't differentiate any further.=head1 NOTESTest::Simple is B<explicitly> tested all the way back to perl 5.004.=head1 HISTORYThis module was conceived while talking with Tony Bowden in hiskitchen one night about the problems I was having writing some reallycomplicated feature into the new Testing module. He observed that themain problem is not dealing with these edge cases but that people hateto write tests B<at all>. What was needed was a dead simple modulethat took all the hard work out of testing and was really, really easyto learn. Paul Johnson simultaneously had this idea (unfortunately,he wasn't in Tony's kitchen). This is it.=head1 SEE ALSO=over 4=item L<Test::More>More testing functions! Once you outgrow Test::Simple, look atTest::More. Test::Simple is 100% forward compatible with Test::More(i.e. you can just use Test::More instead of Test::Simple in yourprograms and things will still work).=item L<Test>The original Perl testing module.=item L<Test::Unit>Elaborate unit testing.=item L<Test::Inline>, L<SelfTest>Embed tests in your code!=item L<Test::Harness>Interprets the output of your test program.=back=head1 AUTHORSIdea by Tony Bowden and Paul Johnson, code by Michael G SchwernE<lt>schwern@pobox.comE<gt>, wardrobe by Calvin Klein.=head1 COPYRIGHTCopyright 2001 by Michael G Schwern E<lt>schwern@pobox.comE<gt>.This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.See F<http://www.perl.com/perl/misc/Artistic.html>=cut1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -