Test - provides a simple framework for writing test scripts
- use strict;
- use Test;
- # use a BEGIN block so we print our plan before MyModule is loaded
- BEGIN { plan tests => 14, todo => [3,4] }
- # load your module...
- use MyModule;
- # Helpful notes. All note-lines must start with a "#".
- print "# I'm testing MyModule version $MyModule::VERSION\n";
- ok(0); # failure
- ok(1); # success
- ok(0); # ok, expected failure (see todo list, above)
- ok(1); # surprise success!
- 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/
- ok(sub { 1+1 }, 2); # success: '2' eq '2'
- ok(sub { 1+1 }, 3); # failure: '2' ne '3'
- my @list = (0,0);
- ok @list, 3, "\@list=".join(',',@list); #extra notes
- ok 'segmentation fault', '/(?i)success/'; #regex match
- skip(
- $^O =~ m/MSWin/ ? "Skip if MSWin" : 0, # whether to skip
- $foo, $bar # arguments just like for ok(...)
- );
- skip(
- $^O =~ m/MSWin/ ? 0 : "Skip unless MSWin", # whether to skip
- $foo, $bar # arguments just like for ok(...)
- );
This module simplifies the task of writing test files for Perl modules, such that their output is in the format that Test::Harness expects to see.
To write a test for your new (and probably not even done) module, create a new file called t/test.t (in a new t directory). If you have multiple test files, to test the "foo", "bar", and "baz" feature sets, then feel free to call your files t/foo.t, t/bar.t, and t/baz.t
This module defines three public functions, plan(...)
, ok(...)
,
and skip(...)
. By default, all three are exported by
the use Test;
statement.
plan(...)
- BEGIN { plan %theplan; }
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, and so on.
Typical usage is just:
- use Test;
- BEGIN { plan tests => 23 }
These are the things that you can put in the parameters to plan:
tests => <i>number</i>
The number of tests in your script. This means all ok() and skip() calls.
todo => [<i>1,5,14</i>]
A reference to a list of tests which are allowed to fail. See TODO TESTS.
onfail => sub { ... }
onfail => \&some_sub
A subroutine reference to be run at the end of the test script, if any of the tests fail. See ONFAIL.
You must call plan(...)
once and only once. You should call it
in a BEGIN {...}
block, like so:
- BEGIN { plan tests => 23 }
- my $value = _to_value($input);
Converts an C
- ok(1 + 1 == 2);
- ok($have, $expect);
- ok($have, $expect, $diagnostics);
This function is the reason for C
In its most basic usage, C
- # Examples of ok(scalar)
- 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
The expression is evaluated in scalar context. So the following will work:
A special case is if the expression is a subroutine reference (in either C syntax or C<\&foo> syntax). In that case, it is executed and its value (true or false) determines if the test passes or fails. For example,
In its two-argument form, C
- # Example of ok(scalar, scalar)
- ok( "this", "that" ); # not ok, 'this' ne 'that'
- ok( "", undef ); # not ok, "" is defined
The second argument is considered a regex if it is either a regex object or a string that looks like a regex. Regex objects are constructed with the qr// operator in recent versions of perl. A string is considered to look like a regex if its first and last characters are "/", or if the first character is "m" and its second and last characters are both the same non-alphanumeric non-whitespace character. These regexp
Regex examples:
- ok( 'JaffO', '/Jaff/' ); # ok, 'JaffO' =~ /Jaff/
- ok( 'JaffO', 'm|Jaff|' ); # ok, 'JaffO' =~ m|Jaff|
- ok( 'JaffO', qr/Jaff/ ); # ok, 'JaffO' =~ qr/Jaff/;
- ok( 'JaffO', '/(?i)jaff/ ); # ok, 'JaffO' =~ /jaff/i;
If either (or both!) is a subroutine reference, it is run and used as the value for comparing. For example:
The above test passes two values to C
Finally, you can append an optional third argument, in
C
Unfortunately, a note cannot be used with the single argument
style of C
All of the above special cases can occasionally cause some problems. See L.
This is used for tests that under some conditions can be skipped. It's basically equivalent to:
- if( $skip_if_true ) {
- ok(1);
- } else {
- ok( args... );
- }
...except that the C
The arguments after the I
Example usage:
- my $if_MSWin =
- $^O =~ m/MSWin/ ? 'Skip if under MSWin' : '';
- # A test to be skipped if under MSWin (i.e., run except under MSWin)
- skip($if_MSWin, thing($foo), thing($bar) );
Or, going the other way:
- my $unless_MSWin =
- $^O =~ m/MSWin/ ? '' : 'Skip unless under MSWin';
- # A test to be skipped unless under MSWin (i.e., run only under MSWin)
- skip($unless_MSWin, thing($foo), thing($bar) );
The tricky thing to remember is that the first parameter is true if
you want to I
Also, when your I
Note that in the above cases, C
- skip( $unless_MSWin,
- sub {
- # This code returns true if the test passes.
- # (But it doesn't even get called if the test is skipped.)
- thing($foo) eq thing($bar)
- }
- );
or even this, which is basically equivalent:
That is, both are like this:
These tests are expected to succeed. Usually, most or all of your tests
are in this category. If a normal test doesn't succeed, then that
means that something is I
The C
TODO tests are designed for maintaining an B
Packages should NOT be released with succeeding TODO tests. As soon as a TODO test starts working, it should be promoted to a normal test, and the newly working feature should be documented in the release notes or in the change log.
Although test failures should be enough, extra diagnostics can be
triggered at the end of a test run. C
The I
C
- ok( $fileglob, '/path/to/some/*stuff/' );
will fail, since Test.pm considers the second argument to be a regex! The best bet is to use the one-argument form:
- ok( $fileglob eq '/path/to/some/*stuff/' );
C
- $foo = "1.0";
- ok( $foo, 1 ); # not ok, "1.0" ne 1
Your best bet is to use the single argument form:
- ok( $foo == 1 ); # ok "1.0" == 1
As you may have inferred from the above documentation and examples,
C
This almost definitely doesn't do what you expect:
- ok $thingy->can('some_method');
Why? Because C
- ok $thingy->can('some_method')->();
What you probably want instead is this:
- ok $thingy->can('some_method') && 1;
If the C
The syntax for C
Moreover, users may expect this:
- skip $unless_mswin, foo($bar), baz($quux);
to not evaluate C
You could do this:
But that's not terribly pretty. You may find it simpler or clearer in the long run to just do things like this:
But be quite sure that C
If C
A past developer of this module once said that it was no longer being actively developed. However, rumors of its demise were greatly exaggerated. Feedback and suggestions are quite welcome.
Be aware that the main value of this module is its simplicity. Note
that there are already more ambitious modules out there, such as
L
Some earlier versions of this module had docs with some confusing
typoes in the description of C
L
L
L
L
L
Copyright (c) 1998-2000 Joshua Nathaniel Pritikin. All rights reserved.
Copyright (c) 2001-2002 Michael G. Schwern.
Copyright (c) 2002-2004 and counting Sean M. Burke.
Current maintainer: Sean M. Burke. E
This package is free software and is provided "as is" without express or implied warranty. It may be used, redistributed and/or modified under the same terms as Perl itself.