📄 test::more.3
字号:
Sometimes running a test under certain conditions will cause thetest script to die. A certain function or method isn't implemented(such as \fIfork()\fR on MacOS), some resource isn't available (like a net connection) or a module isn't available. In these cases it'snecessary to skip tests, or declare that they are supposed to failbut will work in the future (a todo test)..PPFor more details on the mechanics of skip and todo tests seeTest::Harness..PPThe way Test::More handles this is with a named block. Basically, ablock of tests which can be skipped over or made todo. It's best if Ijust show you....IP "\fB\s-1SKIP:\s0 \s-1BLOCK\s0\fR" 4.IX Item "SKIP: BLOCK".Vb 2\& SKIP: {\& skip $why, $how_many if $condition;\&\& ...normal testing code goes here...\& }.Ve.SpThis declares a block of tests that might be skipped, \f(CW$how_many\fR teststhere are, \f(CW$why\fR and under what \f(CW$condition\fR to skip them. An example isthe easiest way to illustrate:.Sp.Vb 2\& SKIP: {\& eval { require HTML::Lint };\&\& skip "HTML::Lint not installed", 2 if $@;\&\& my $lint = new HTML::Lint;\& isa_ok( $lint, "HTML::Lint" );\&\& $lint\->parse( $html );\& is( $lint\->errors, 0, "No errors found in HTML" );\& }.Ve.SpIf the user does not have HTML::Lint installed, the whole block ofcode \fIwon't be run at all\fR. Test::More will output special ok'swhich Test::Harness interprets as skipped, but passing, tests..SpIt's important that \f(CW$how_many\fR accurately reflects the number of testsin the \s-1SKIP\s0 block so the # of tests run will match up with your plan.If your plan is \f(CW\*(C`no_plan\*(C'\fR \f(CW$how_many\fR is optional and will default to 1..SpIt's perfectly safe to nest \s-1SKIP\s0 blocks. Each \s-1SKIP\s0 block must havethe label \f(CW\*(C`SKIP\*(C'\fR, or Test::More can't work its magic..SpYou don't skip tests which are failing because there's a bug in yourprogram, or for which you don't yet have code written. For that youuse \s-1TODO\s0. Read on..IP "\fB\s-1TODO:\s0 \s-1BLOCK\s0\fR" 4.IX Item "TODO: BLOCK".Vb 2\& TODO: {\& local $TODO = $why if $condition;\&\& ...normal testing code goes here...\& }.Ve.SpDeclares a block of tests you expect to fail and \f(CW$why\fR. Perhaps it'sbecause you haven't fixed a bug or haven't finished a new feature:.Sp.Vb 2\& TODO: {\& local $TODO = "URI::Geller not finished";\&\& my $card = "Eight of clubs";\& is( URI::Geller\->your_card, $card, \*(AqIs THIS your card?\*(Aq );\&\& my $spoon;\& URI::Geller\->bend_spoon;\& is( $spoon, \*(Aqbent\*(Aq, "Spoon bending, that\*(Aqs original" );\& }.Ve.SpWith a todo block, the tests inside are expected to fail. Test::Morewill run the tests normally, but print out special flags indicatingthey are \*(L"todo\*(R". Test::Harness will interpret failures as being ok.Should anything succeed, it will report it as an unexpected success.You then know the thing you had todo is done and can remove the\&\s-1TODO\s0 flag..SpThe nice part about todo tests, as opposed to simply commenting out ablock of tests, is it's like having a programmatic todo list. You knowhow much work is left to be done, you're aware of what bugs there are,and you'll know immediately when they're fixed..SpOnce a todo test starts succeeding, simply move it outside the block.When the block is empty, delete it..Sp\&\fB\s-1NOTE\s0\fR: \s-1TODO\s0 tests require a Test::Harness upgrade else it willtreat it as a normal failure. See \*(L"\s-1CAVEATS\s0 and \s-1NOTES\s0\*(R")..IP "\fBtodo_skip\fR" 4.IX Item "todo_skip".Vb 2\& TODO: {\& todo_skip $why, $how_many if $condition;\&\& ...normal testing code...\& }.Ve.SpWith todo tests, it's best to have the tests actually run. That wayyou'll know when they start passing. Sometimes this isn't possible.Often a failing test will cause the whole program to die or hang, eveninside an \f(CW\*(C`eval BLOCK\*(C'\fR with and using \f(CW\*(C`alarm\*(C'\fR. In these extremecases you have no choice but to skip over the broken tests entirely..SpThe syntax and behavior is similar to a \f(CW\*(C`SKIP: BLOCK\*(C'\fR except thetests will be marked as failing but todo. Test::Harness willinterpret them as passing..IP "When do I use \s-1SKIP\s0 vs. \s-1TODO\s0?" 4.IX Item "When do I use SKIP vs. TODO?"\&\fBIf it's something the user might not be able to do\fR, use \s-1SKIP\s0.This includes optional modules that aren't installed, running underan \s-1OS\s0 that doesn't have some feature (like \fIfork()\fR or symlinks), or maybeyou need an Internet connection and one isn't available..Sp\&\fBIf it's something the programmer hasn't done yet\fR, use \s-1TODO\s0. Thisis for any code you haven't written yet, or bugs you have yet to fix,but want to put tests in your testing script (always a good idea)..Sh "Test control".IX Subsection "Test control".IP "\fB\s-1BAIL_OUT\s0\fR" 4.IX Item "BAIL_OUT".Vb 1\& BAIL_OUT($reason);.Ve.SpIndicates to the harness that things are going so badly all testingshould terminate. This includes the running any additional test scripts..SpThis is typically used when testing cannot continue such as a criticalmodule failing to compile or a necessary external utility not beingavailable such as a database connection failing..SpThe test will exit with 255..Sh "Discouraged comparison functions".IX Subsection "Discouraged comparison functions"The use of the following functions is discouraged as they are notactually testing functions and produce no diagnostics to help figureout what went wrong. They were written before \fIis_deeply()\fR existedbecause I couldn't figure out how to display a useful diff of twoarbitrary data structures..PPThese functions are usually used inside an \fIok()\fR..PP.Vb 1\& ok( eq_array(\e@got, \e@expected) );.Ve.PP\&\f(CW\*(C`is_deeply()\*(C'\fR can do that better and with diagnostics..PP.Vb 1\& is_deeply( \e@got, \e@expected );.Ve.PPThey may be deprecated in future versions..IP "\fBeq_array\fR" 4.IX Item "eq_array".Vb 1\& my $is_eq = eq_array(\e@got, \e@expected);.Ve.SpChecks if two arrays are equivalent. This is a deep check, somulti-level structures are handled correctly..IP "\fBeq_hash\fR" 4.IX Item "eq_hash".Vb 1\& my $is_eq = eq_hash(\e%got, \e%expected);.Ve.SpDetermines if the two hashes contain the same keys and values. Thisis a deep check..IP "\fBeq_set\fR" 4.IX Item "eq_set".Vb 1\& my $is_eq = eq_set(\e@got, \e@expected);.Ve.SpSimilar to \fIeq_array()\fR, except the order of the elements is \fBnot\fRimportant. This is a deep check, but the irrelevancy of order onlyapplies to the top level..Sp.Vb 1\& ok( eq_set(\e@got, \e@expected) );.Ve.SpIs better written:.Sp.Vb 1\& is_deeply( [sort @got], [sort @expected] );.Ve.Sp\&\fB\s-1NOTE\s0\fR By historical accident, this is not a true set comparison.While the order of elements does not matter, duplicate elements do..Sp\&\fB\s-1NOTE\s0\fR \fIeq_set()\fR does not know how to deal with references at the toplevel. The following is an example of a comparison which might not work:.Sp.Vb 1\& eq_set([\e1, \e2], [\e2, \e1]);.Ve.SpTest::Deep contains much better set comparison functions..Sh "Extending and Embedding Test::More".IX Subsection "Extending and Embedding Test::More"Sometimes the Test::More interface isn't quite enough. Fortunately,Test::More is built on top of Test::Builder which provides a single,unified backend for any test library to use. This means two testlibraries which both use Test::Builder \fBcan be used together in thesame program\fR..PPIf you simply want to do a little tweaking of how the tests behave,you can access the underlying Test::Builder object like so:.IP "\fBbuilder\fR" 4.IX Item "builder".Vb 1\& my $test_builder = Test::More\->builder;.Ve.SpReturns the Test::Builder object underlying Test::More for you to playwith..SH "EXIT CODES".IX Header "EXIT CODES"If all your tests passed, Test::Builder 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::Builderwill 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..PPSo the exit codes are....PP.Vb 3\& 0 all tests successful\& 255 test died or all passed but wrong # of tests run\& any other number how many failed (including missing or extras).Ve.PPIf you fail more than 254 tests, it will be reported as 254..PP\&\fB\s-1NOTE\s0\fR This behavior may go away in future versions..SH "CAVEATS and NOTES".IX Header "CAVEATS and NOTES".IP "Backwards compatibility" 4.IX Item "Backwards compatibility"Test::More works with Perls as old as 5.004_05..IP "Overloaded objects" 4.IX Item "Overloaded objects"String overloaded objects are compared \fBas strings\fR (or in \fIcmp_ok()\fR'scase, strings or numbers as appropriate to the comparison op). Thisprevents Test::More from piercing an object's interface allowingbetter blackbox testing. So if a function starts returning overloadedobjects instead of bare strings your tests won't notice thedifference. This is good..SpHowever, it does mean that functions like \fIis_deeply()\fR cannot be used totest the internals of string overloaded objects. In this case I wouldsuggest Test::Deep which contains more flexible testing functions forcomplex data structures..IP "Threads" 4.IX Item "Threads"Test::More will only be aware of threads if \*(L"use threads\*(R" has been done\&\fIbefore\fR Test::More is loaded. This is ok:.Sp.Vb 2\& use threads;\& use Test::More;.Ve.SpThis may cause problems:.Sp.Vb 2\& use Test::More\& use threads;.Ve.Sp5.8.1 and above are supported. Anything below that has too many bugs..IP "Test::Harness upgrade" 4.IX Item "Test::Harness upgrade"no_plan and todo depend on new Test::Harness features and fixes. Ifyou're going to distribute tests that use no_plan or todo yourend-users will have to upgrade Test::Harness to the latest one on\&\s-1CPAN\s0. If you avoid no_plan and \s-1TODO\s0 tests, the stock Test::Harnesswill work fine..SpInstalling Test::More should also upgrade Test::Harness..SH "HISTORY".IX Header "HISTORY"This is a case of convergent evolution with Joshua Pritikin's Testmodule. I was largely unaware of its existence when I'd firstwritten my own \fIok()\fR routines. This module exists because I can'tfigure out how to easily wedge test names into Test's interface (alongwith a few other problems)..PPThe goal here is to have a testing utility that's simple to learn,quick to use and difficult to trip yourself up with while stillproviding more flexibility than the existing Test.pm. As such, thenames of the most common routines are kept tiny, special cases andmagic side-effects are kept to a minimum. \s-1WYSIWYG\s0..SH "SEE ALSO".IX Header "SEE ALSO"Test::Simple if all this confuses you and you just want to writesome tests. You can upgrade to Test::More later (it's forwardcompatible)..PPTest is the old testing module. Its main benefit is that it hasbeen distributed with Perl since 5.004_05..PPTest::Harness for details on how your test results are interpretedby Perl..PPTest::Differences for more ways to test complex data structures.And it plays well with Test::More..PPTest::Class is like XUnit but more perlish..PPTest::Deep gives you more powerful complex data structure testing..PPTest::Unit is XUnit style testing..PPTest::Inline shows the idea of embedded testing..PPBundle::Test installs a whole bunch of useful test modules..SH "AUTHORS".IX Header "AUTHORS"Michael G Schwern <schwern@pobox.com> with much inspirationfrom Joshua Pritikin's Test module and lots of help from BarrieSlaymaker, Tony Bowden, blackstar.co.uk, chromatic, Fergal Daly andthe perl-qa gang..SH "BUGS".IX Header "BUGS"See \fIhttp://rt.cpan.org\fR to report and view bugs..SH "COPYRIGHT".IX Header "COPYRIGHT"Copyright 2001\-2002, 2004\-2006 by Michael G Schwern <schwern@pobox.com>..PPThis program is free software; you can redistribute it and/ormodify it under the same terms as Perl itself..PPSee \fIhttp://www.perl.com/perl/misc/Artistic.html\fR
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -