| home / programming / awperl / 1 | [previous] |
|
|
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4
5 use lib qw(..);
6 use Test::MailMessage tests => 2;
7
8 msg_ok(<<EOM, "okay");
9 from: ok
| |
|
|
| 10 |
subject: whatever |
|
| 11 |
|
|
| |
|
|
| 12 |
body |
|
| 13 |
EOM |
|
| 14 |
msg_ok(\*DATA, |
"bogus"); |
| 15 |
|
|
| 16 |
__END__ |
|
| 17 |
bogus mail |
|
| 18 |
message |
|
The result of running this is:
1..2
ok 1 - okay
not ok 2 - bogus
# Failed test (./test at line 14)
# Invalid header at ./test line 14
# Looks like you failed 1 tests of 2.
Although we only used one Test:: module, we could have used others, for example:
use Test::MailMessage tests z=> 2;
use Test::Exception; use Test::More;
Only one of the use statements for Test::Modules should give the number of tests
to be run. Do not think that each use statement is supposed to number the tests
run by functions of that module; instead, one use statement gives the total
number of tests to be run.
brian d foy4 used Test::Builder to create Test::Pod,5 which is
also worth covering.
Documentation in Perl need not be entirely unstructured. The Plain Old Documentation (POD) format for storing documentation in the Perl source code (see the perlpod manual page) is a markup language and therefore it is possible to commit syntax errors. So rather than wait until your users try to look at your documentation (okay, play along with me here—imagine that you have users who want to read your documentation), and get errors from their POD viewer, you can make sure in advance that the POD is good.
Test::Pod exports a single function, pod_ok(), which checks the POD in the
file named by its argument. I’ll show an example of its use later in this
chapter.
If you’re thinking that tests deserve to be inside the code they’re testing just as much as documentation does, then you want Test::Inline. This module by Michael Schwern enables you to embed tests in code just like POD, because, in fact, it uses POD for that embedding.
Fergal Daly’s Test::NoWarnings (formerly Test::Warn::None) lets you verify that your code is free of warnings. In its simplest usage, you just use the module, and increment the number of tests you’re running, because Test::NoWarnings adds one more. So if your test starts:
use Test::More tests => 17;
then change it to:
use Test::NoWarnings;
use Test::More tests => 18;
t/01load....ok t/02tie ok
t/03use ok
t/04pod ok
All tests successful. Files=4, Tests=24, 2 1.82 CPU)
wallclock secs ( 1.51 cusr + 0.31 csys =
and the final test will be that no warnings were generated in the running of the other tests.
Test::Harness is how you combine multiple tests. It predates every other Test::
module, and you’ll find it in every version of Perl 5. Test::Harness exports
a function, runtests(), which runs all the test files whose names are passed
to it as arguments and summarizes their results. You won’t see one line
printed per test; runtests() intercepts those lines of output. Rather you’ll
see one line printed per test file, followed by a summary of the results of
the tests in that file. Then it prints a global summary line. Here’s an
example of the output:
As it runs, before printing “ok” on each line, you’ll see
a count of the tests being run updating in place, finally to be overwritten
by “ok”. If any fail, you’ll see something appropriate instead
of “ok”.
You can use Test::Harness quite easily, for instance:
% perl -MTest::Harness -e 'runtests(glob "*.t")'
but it’s seldom necessary even to do that, because a standard Perl module makefile will do it for you. I’ll show you how shortly.
Test::Harness turns your regression tests into a full-fledged deliverable. Managers just love to watch the numbers whizzing around.
Next week (in part 2), you'll take what you've learned in this section and put it to use in developing an actual application.
This chapter, titled 'Test Now, Test Forever (Diagnosis)' is excerpted from
the new book, "Perl
Medic: Transforming Legacy Code" (ISBN 0201795264) by Peter
J. Scott. This excerpt is posted with permission from publisher Addison-Wesley,
copyright 2004, all rights reserved.
| home / programming / awperl / 1 | [previous] |
Created: March 27, 2003
Revised: March 24, 2004
URL: http://webreference.com/programming/awperl/1