14. Testing

Created Saturday 18 October 2014

Basic tests

Consider:
sub reciprocal { return 1 / shift }

Using Test::More

use strict;
use warnings;
use Test::More tests => 3; # The plan; for fewer or more tests than 3 a failure will be reported
ok 7, '7 should evaluate to a true value'; # ok() and is() are each considered a singe test;
is 5 - 2, 3 , '... and 5 - 2 should equal three'; # thus, there are 3 tests in this sample
ok 0, 'This test is designed to fail';
use strict;
use warnings;
use Test::More 'no_plan';
ok( 7, '7 should evaluate to a true value' );
is( 5 - 2, 3, '... and 5 - 2 should equal three' );
ok( 0, 'This test is designed to fail' );
use Test::More;
# write a bunch of tests here
done_testing;

Writing tests

.
├── lib
│   └── TestMe.pm
├── MyProject.komodoproject
└── t
└── testit.t
package TestMe;
use strict;
use warnings;
use Exporter::NoWork; # Automatically allows exporting of functions in the package that do not start with an underscore
sub reciprocal { return 1 / shift }
1;

Understanding the prove Utility

$ prove -l -v t/testit.t
t/testit.t ..
ok 1 - this is a test!
1..1
ok
All tests successful.
Files=1, Tests=1, 0 wallclock secs ( 0.03 usr 0.01 sys + 0.04 cusr 0.00 csys = 0.08 CPU)
Result: PASS

Understanding Test::More test functions

Using ok

Using is



Backlinks: