.\" Automatically generated by Pod::Man v1.34, Pod::Parser v1.13 .\" .\" Standard preamble: .\" ======================================================================== .de Sh \" Subsection heading .br .if t .Sp .ne 5 .PP \fB\\$1\fR .PP .. .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. | will give a .\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to .\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C' .\" expand to `' in nroff, nothing in troff, for use with C<>. .tr \(*W-|\(bv\*(Tr .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' 'br\} .\" .\" If the F register is turned on, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . nr % 0 . rr F .\} .\" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .hy 0 .if n .na .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "Test 3" .TH Test 3 "2002-11-24" "perl v5.8.0" "Perl Programmers Reference Guide" .SH "NAME" Test \- provides a simple framework for writing test scripts .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 2 \& use strict; \& use Test; .Ve .PP .Vb 2 \& # use a BEGIN block so we print our plan before MyModule is loaded \& BEGIN { plan tests => 14, todo => [3,4] } .Ve .PP .Vb 2 \& # load your module... \& use MyModule; .Ve .PP .Vb 2 \& ok(0); # failure \& ok(1); # success .Ve .PP .Vb 2 \& ok(0); # ok, expected failure (see todo list, above) \& ok(1); # surprise success! .Ve .PP .Vb 4 \& ok(0,1); # failure: '0' ne '1' \& ok('broke','fixed'); # failure: 'broke' ne 'fixed' \& ok('fixed','fixed'); # success: 'fixed' eq 'fixed' \& ok('fixed',qr/x/); # success: 'fixed' =~ qr/x/ .Ve .PP .Vb 3 \& ok(sub { 1+1 }, 2); # success: '2' eq '2' \& ok(sub { 1+1 }, 3); # failure: '2' ne '3' \& ok(0, int(rand(2)); # (just kidding :-) .Ve .PP .Vb 3 \& my @list = (0,0); \& ok @list, 3, "\e@list=".join(',',@list); #extra diagnostics \& ok 'segmentation fault', '/(?i)success/'; #regex match .Ve .PP .Vb 1 \& skip($feature_is_missing, ...); #do platform specific test .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" \&\fB\s-1STOP\s0!\fR If you are writing a new test, we \fIhighly suggest\fR you use the new Test::Simple and Test::More modules instead. .PP Test::Harness expects to see particular output when it executes tests. This module aims to make writing proper test scripts just a little bit easier (and less error prone :\-). .Sh "Functions" .IX Subsection "Functions" All the following are exported by Test by default. .IP "\fBplan\fR" 4 .IX Item "plan" .Vb 1 \& BEGIN { plan %theplan; } .Ve .Sp This should be the first thing you call in your test script. It declares your testing plan, how many there will be, if any of them should be allowed to fail, etc... .Sp Typical usage is just: .Sp .Vb 2 \& use Test; \& BEGIN { plan tests => 23 } .Ve .Sp Things you can put in the plan: .Sp .Vb 7 \& tests The number of tests in your script. \& This means all ok() and skip() calls. \& todo A reference to a list of tests which are allowed \& to fail. See L. \& onfail A subroutine reference to be run at the end of \& the test script should any of the tests fail. \& See L. .Ve .Sp You must call \fIplan()\fR once and only once. .IP "\fBok\fR" 4 .IX Item "ok" .Vb 3 \& ok(1 + 1 == 2); \& ok($have, $expect); \& ok($have, $expect, $diagnostics); .Ve .Sp This is the reason for Test's existance. Its the basic function that handles printing \*(L"ok\*(R" or \*(L"not ok\*(R" along with the current test number. .Sp In its most basic usage, it simply takes an expression. If its true, the test passes, if false, the test fails. Simp. .Sp .Vb 5 \& ok( 1 + 1 == 2 ); # ok if 1 + 1 == 2 \& ok( $foo =~ /bar/ ); # ok if $foo contains 'bar' \& ok( baz($x + $y) eq 'Armondo' ); # ok if baz($x + $y) returns \& # 'Armondo' \& ok( @a == @b ); # ok if @a and @b are the same length .Ve .Sp The expression is evaluated in scalar context. So the following will work: .Sp .Vb 3 \& ok( @stuff ); # ok if @stuff has any elements \& ok( !grep !defined $_, @stuff ); # ok if everything in @stuff is \& # defined. .Ve .Sp A special case is if the expression is a subroutine reference. In that case, it is executed and its value (true or false) determines if the test passes or fails. .Sp In its two argument form it compares the two values to see if they equal (with \f(CW\*(C`eq\*(C'\fR). .Sp .Vb 1 \& ok( "this", "that" ); # not ok, 'this' ne 'that' .Ve .Sp If either is a subroutine reference, that is run and used as a comparison. .Sp Should \f(CW$expect\fR either be a regex reference (ie. qr//) or a string that looks like a regex (ie. '/foo/') \fIok()\fR will perform a pattern match against it rather than using eq. .Sp .Vb 3 \& ok( 'JaffO', '/Jaff/' ); # ok, 'JaffO' =~ /Jaff/ \& ok( 'JaffO', qr/Jaff/ ); # ok, 'JaffO' =~ qr/Jaff/; \& ok( 'JaffO', '/(?i)jaff/ ); # ok, 'JaffO' =~ /jaff/i; .Ve .Sp Finally, an optional set of \f(CW$diagnostics\fR will be printed should the test fail. This should usually be some useful information about the test pertaining to why it failed or perhaps a description of the test. Or both. .Sp .Vb 4 \& ok( grep($_ eq 'something unique', @stuff), 1, \& "Something that should be unique isn't!\en". \& '@stuff = '.join ', ', @stuff \& ); .Ve .Sp Unfortunately, a diagnostic cannot be used with the single argument style of \fIok()\fR. .Sp All these special cases can cause some problems. See \*(L"\s-1BUGS\s0 and \s-1CAVEATS\s0\*(R". .SH "TEST TYPES" .IX Header "TEST TYPES" .IP "\(bu \s-1NORMAL\s0 \s-1TESTS\s0" 4 .IX Item "NORMAL TESTS" These tests are expected to succeed. If they don't something's screwed up! .IP "\(bu \s-1SKIPPED\s0 \s-1TESTS\s0" 4 .IX Item "SKIPPED TESTS" Skip is for tests that might or might not be possible to run depending on the availability of platform specific features. The first argument should evaluate to true (think \*(L"yes, please skip\*(R") if the required feature is not available. After the first argument, skip works exactly the same way as do normal tests. .IP "\(bu \s-1TODO\s0 \s-1TESTS\s0" 4 .IX Item "TODO TESTS" \&\s-1TODO\s0 tests are designed for maintaining an \fBexecutable \s-1TODO\s0 list\fR. These tests are expected \s-1NOT\s0 to succeed. If a \s-1TODO\s0 test does succeed, the feature in question should not be on the \s-1TODO\s0 list, now should it? .Sp Packages should \s-1NOT\s0 be released with succeeding \s-1TODO\s0 tests. As soon as a \s-1TODO\s0 test starts working, it should be promoted to a normal test and the newly working feature should be documented in the release notes or change log. .SH "ONFAIL" .IX Header "ONFAIL" .Vb 1 \& BEGIN { plan test => 4, onfail => sub { warn "CALL 911!" } } .Ve .PP While test failures should be enough, extra diagnostics can be triggered at the end of a test run. \f(CW\*(C`onfail\*(C'\fR is passed an array ref of hash refs that describe each test failure. Each hash will contain at least the following fields: \f(CW\*(C`package\*(C'\fR, \f(CW\*(C`repetition\*(C'\fR, and \&\f(CW\*(C`result\*(C'\fR. (The file, line, and test number are not included because their correspondence to a particular test is tenuous.) If the test had an expected value or a diagnostic string, these will also be included. .PP The \fBoptional\fR \f(CW\*(C`onfail\*(C'\fR hook might be used simply to print out the version of your package and/or how to report problems. It might also be used to generate extremely sophisticated diagnostics for a particularly bizarre test failure. However it's not a panacea. Core dumps or other unrecoverable errors prevent the \f(CW\*(C`onfail\*(C'\fR hook from running. (It is run inside an \f(CW\*(C`END\*(C'\fR block.) Besides, \f(CW\*(C`onfail\*(C'\fR is probably over-kill in most cases. (Your test code should be simpler than the code it is testing, yes?) .SH "BUGS and CAVEATS" .IX Header "BUGS and CAVEATS" \&\fIok()\fR's special handling of subroutine references is an unfortunate \&\*(L"feature\*(R" that can't be removed due to compatibility. .PP \&\fIok()\fR's use of string eq can sometimes cause odd problems when comparing numbers, especially if you're casting a string to a number: .PP .Vb 2 \& $foo = "1.0"; \& ok( $foo, 1 ); # not ok, "1.0" ne 1 .Ve .PP Your best bet is to use the single argument form: .PP .Vb 1 \& ok( $foo == 1 ); # ok "1.0" == 1 .Ve .PP \&\fIok()\fR's special handing of strings which look like they might be regexes can also cause unexpected behavior. An innocent: .PP .Vb 1 \& ok( $fileglob, '/path/to/some/*stuff/' ); .Ve .PP will fail since Test.pm considers the second argument to a regex. Again, best bet is to use the single argument form: .PP .Vb 1 \& ok( $fileglob eq '/path/to/some/*stuff/' ); .Ve .SH "NOTE" .IX Header "NOTE" This module is no longer actively being developed, only bug fixes and small tweaks (I'll still accept patches). If you desire additional functionality, consider Test::More or Test::Unit. .SH "SEE ALSO" .IX Header "SEE ALSO" Test::Simple, Test::More, Test::Harness, Devel::Cover .PP Test::Builder for building your own testing library. .PP Test::Unit is an interesting XUnit-style testing library. .PP Test::Inline and SelfTest let you embed tests in code. .SH "AUTHOR" .IX Header "AUTHOR" Copyright (c) 1998\-2000 Joshua Nathaniel Pritikin. All rights reserved. Copyright (c) 2001\-2002 Michael G Schwern. .PP Current maintainer, Michael G Schwern .PP This package is free software and is provided \*(L"as is\*(R" without express or implied warranty. It may be used, redistributed and/or modified under the same terms as Perl itself.