📄 test::builder.3
字号:
\& $Test\->skip($why);.Ve.SpSkips the current test, reporting \f(CW$why\fR..IP "\fBtodo_skip\fR" 4.IX Item "todo_skip".Vb 2\& $Test\->todo_skip;\& $Test\->todo_skip($why);.Ve.SpLike \fIskip()\fR, only it will declare the test as failing and \s-1TODO\s0. Similarto.Sp.Vb 1\& print "not ok $tnum # TODO $why\en";.Ve.Sh "Test building utility methods".IX Subsection "Test building utility methods"These methods are useful when writing your own test methods..IP "\fBmaybe_regex\fR" 4.IX Item "maybe_regex".Vb 2\& $Test\->maybe_regex(qr/$regex/);\& $Test\->maybe_regex(\*(Aq/$regex/\*(Aq);.Ve.SpConvenience method for building testing functions that take regularexpressions as arguments, but need to work before perl 5.005..SpTakes a quoted regular expression produced by qr//, or a stringrepresenting a regular expression..SpReturns a Perl value which may be used instead of the correspondingregular expression, or undef if it's argument is not recognised..SpFor example, a version of \fIlike()\fR, sans the useful diagnostic messages,could be written as:.Sp.Vb 7\& sub laconic_like {\& my ($self, $this, $regex, $name) = @_;\& my $usable_regex = $self\->maybe_regex($regex);\& die "expecting regex, found \*(Aq$regex\*(Aq\en"\& unless $usable_regex;\& $self\->ok($this =~ m/$usable_regex/, $name);\& }.Ve.IP "\fBis_fh\fR" 4.IX Item "is_fh".Vb 1\& my $is_fh = $Test\->is_fh($thing);.Ve.SpDetermines if the given \f(CW$thing\fR can be used as a filehandle..Sh "Test style".IX Subsection "Test style".IP "\fBlevel\fR" 4.IX Item "level".Vb 1\& $Test\->level($how_high);.Ve.SpHow far up the call stack should \f(CW$Test\fR look when reporting where thetest failed..SpDefaults to 1..SpSetting \f(CW$Test::Builder::Level\fR overrides. This is typically usefullocalized:.Sp.Vb 2\& sub my_ok {\& my $test = shift;\&\& local $Test::Builder::Level = $Test::Builder::Level + 1;\& $TB\->ok($test);\& }.Ve.SpTo be polite to other functions wrapping your own you usually want to increment \f(CW$Level\fR rather than set it to a constant..IP "\fBuse_numbers\fR" 4.IX Item "use_numbers".Vb 1\& $Test\->use_numbers($on_or_off);.Ve.SpWhether or not the test should output numbers. That is, this if true:.Sp.Vb 3\& ok 1\& ok 2\& ok 3.Ve.Spor this if false.Sp.Vb 3\& ok\& ok\& ok.Ve.SpMost useful when you can't depend on the test output order, such aswhen threads or forking is involved..SpDefaults to on..IP "\fBno_diag\fR" 4.IX Item "no_diag".Vb 1\& $Test\->no_diag($no_diag);.Ve.SpIf set true no diagnostics will be printed. This includes calls to\&\fIdiag()\fR..IP "\fBno_ending\fR" 4.IX Item "no_ending".Vb 1\& $Test\->no_ending($no_ending);.Ve.SpNormally, Test::Builder does some extra diagnostics when the testends. It also changes the exit code as described below..SpIf this is true, none of that will be done..IP "\fBno_header\fR" 4.IX Item "no_header".Vb 1\& $Test\->no_header($no_header);.Ve.SpIf set to true, no \*(L"1..N\*(R" header will be printed..Sh "Output".IX Subsection "Output"Controlling where the test output goes..PPIt's ok for your test to change where \s-1STDOUT\s0 and \s-1STDERR\s0 point to,Test::Builder's default output settings will not be affected..IP "\fBdiag\fR" 4.IX Item "diag".Vb 1\& $Test\->diag(@msgs);.Ve.SpPrints out the given \f(CW@msgs\fR. Like \f(CW\*(C`print\*(C'\fR, arguments are simplyappended together..SpNormally, it uses the \fIfailure_output()\fR handle, but if this is for a\&\s-1TODO\s0 test, the \fItodo_output()\fR handle is used..SpOutput will be indented and marked with a # so as not to interferewith test output. A newline will be put on the end if there isn't onealready..SpWe encourage using this rather than calling print directly..SpReturns false. Why? Because \fIdiag()\fR is often used in conjunction witha failing test (\f(CW\*(C`ok() || diag()\*(C'\fR) it \*(L"passes through\*(R" the failure..Sp.Vb 1\& return ok(...) || diag(...);.Ve.IP "\fBoutput\fR" 4.IX Item "output".Vb 2\& $Test\->output($fh);\& $Test\->output($file);.Ve.SpWhere normal \*(L"ok/not ok\*(R" test output should go..SpDefaults to \s-1STDOUT\s0..IP "\fBfailure_output\fR" 4.IX Item "failure_output".Vb 2\& $Test\->failure_output($fh);\& $Test\->failure_output($file);.Ve.SpWhere diagnostic output on test failures and \fIdiag()\fR should go..SpDefaults to \s-1STDERR\s0..IP "\fBtodo_output\fR" 4.IX Item "todo_output".Vb 2\& $Test\->todo_output($fh);\& $Test\->todo_output($file);.Ve.SpWhere diagnostics about todo test failures and \fIdiag()\fR should go..SpDefaults to \s-1STDOUT\s0..IP "carp" 4.IX Item "carp".Vb 1\& $tb\->carp(@message);.Ve.SpWarns with \f(CW@message\fR but the message will appear to come from thepoint where the original test function was called (\f(CW\*(C`$tb\-\*(C'\fRcaller>)..IP "croak" 4.IX Item "croak".Vb 1\& $tb\->croak(@message);.Ve.SpDies with \f(CW@message\fR but the message will appear to come from thepoint where the original test function was called (\f(CW\*(C`$tb\-\*(C'\fRcaller>)..Sh "Test Status and Info".IX Subsection "Test Status and Info".IP "\fBcurrent_test\fR" 4.IX Item "current_test".Vb 2\& my $curr_test = $Test\->current_test;\& $Test\->current_test($num);.Ve.SpGets/sets the current test number we're on. You usually shouldn'thave to set this..SpIf set forward, the details of the missing tests are filled in as 'unknown'.if set backward, the details of the intervening tests are deleted. Youcan erase history if you really want to..IP "\fBsummary\fR" 4.IX Item "summary".Vb 1\& my @tests = $Test\->summary;.Ve.SpA simple summary of the tests so far. True for pass, false for fail.This is a logical pass/fail, so todos are passes..SpOf course, test #1 is \f(CW$tests\fR[0], etc....IP "\fBdetails\fR" 4.IX Item "details".Vb 1\& my @tests = $Test\->details;.Ve.SpLike \fIsummary()\fR, but with a lot more detail..Sp.Vb 7\& $tests[$test_num \- 1] = \& { \*(Aqok\*(Aq => is the test considered a pass?\& actual_ok => did it literally say \*(Aqok\*(Aq?\& name => name of the test (if any)\& type => type of test (if any, see below).\& reason => reason for the above (if any)\& };.Ve.Sp\&'ok' is true if Test::Harness will consider the test to be a pass..Sp\&'actual_ok' is a reflection of whether or not the test literallyprinted 'ok' or 'not ok'. This is for examining the result of 'todo'tests..Sp\&'name' is the name of the test..Sp\&'type' indicates if it was a special test. Normal tests have a typeof ''. Type can be one of the following:.Sp.Vb 4\& skip see skip()\& todo see todo()\& todo_skip see todo_skip()\& unknown see below.Ve.SpSometimes the Test::Builder test counter is incremented without itprinting any test output, for example, when \fIcurrent_test()\fR is changed.In these cases, Test::Builder doesn't know the result of the test, soit's type is 'unkown'. These details for these tests are filled in.They are considered ok, but the name and actual_ok is left undef..SpFor example \*(L"not ok 23 \- hole count # \s-1TODO\s0 insufficient donuts\*(R" wouldresult in this structure:.Sp.Vb 7\& $tests[22] = # 23 \- 1, since arrays start from 0.\& { ok => 1, # logically, the test passed since it\*(Aqs todo\& actual_ok => 0, # in absolute terms, it failed\& name => \*(Aqhole count\*(Aq,\& type => \*(Aqtodo\*(Aq,\& reason => \*(Aqinsufficient donuts\*(Aq\& };.Ve.IP "\fBtodo\fR" 4.IX Item "todo".Vb 2\& my $todo_reason = $Test\->todo;\& my $todo_reason = $Test\->todo($pack);.Ve.Sp\&\fItodo()\fR looks for a \f(CW$TODO\fR variable in your tests. If set, all testswill be considered 'todo' (see Test::More and Test::Harness fordetails). Returns the reason (ie. the value of \f(CW$TODO\fR) if running astodo tests, false otherwise..Sp\&\fItodo()\fR is about finding the right package to look for \f(CW$TODO\fR in. Ituses the \fIexported_to()\fR package to find it. If that's not set, it'spretty good at guessing the right package to look at based on \f(CW$Level\fR..SpSometimes there is some confusion about where \fItodo()\fR should be lookingfor the \f(CW$TODO\fR variable. If you want to be sure, tell it explicitlywhat \f(CW$pack\fR to use..IP "\fBcaller\fR" 4.IX Item "caller".Vb 3\& my $package = $Test\->caller;\& my($pack, $file, $line) = $Test\->caller;\& my($pack, $file, $line) = $Test\->caller($height);.Ve.SpLike the normal \fIcaller()\fR, except it reports according to your \fIlevel()\fR..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..SH "THREADS".IX Header "THREADS"In perl 5.8.1 and later, Test::Builder is thread-safe. The testnumber is shared amongst all threads. This means if one thread setsthe test number using \fIcurrent_test()\fR they will all be effected..PPWhile versions earlier than 5.8.1 had threads they contain too manybugs to support..PPTest::Builder is only thread-aware if threads.pm is loaded \fIbefore\fRTest::Builder..SH "EXAMPLES".IX Header "EXAMPLES"\&\s-1CPAN\s0 can provide the best examples. Test::Simple, Test::More,Test::Exception and Test::Differences all use Test::Builder..SH "SEE ALSO".IX Header "SEE ALSO"Test::Simple, Test::More, Test::Harness.SH "AUTHORS".IX Header "AUTHORS"Original code by chromatic, maintained by Michael G Schwern<schwern@pobox.com>.SH "COPYRIGHT".IX Header "COPYRIGHT"Copyright 2002, 2004 by chromatic <chromatic@wgz.org> and Michael G Schwern <schwern@pobox.com>..PPThis program is free software; you can redistribute it and/or modify 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 + -