You’ve heard about the benefits of testing. You know that it can improve your code’s reliability and maintainability as well as your development processes. You may even know about the wide range of available modules and idioms that Perl offers for testing Perl and non-Perl programs. In short, you may know everything except where to start.
The labs in this chapter walk through the most basic steps of running and writing automated tests with Perl. By the end of the chapter, you’ll know how to start and continue testing, how Perl’s testing libraries work, and where to find more libraries to ease your workload.
One of Perl’s greatest strengths is the CPAN, an archive of thousands of reusuable code libraries—generally called modules—for almost any programming problem anyone has ever solved with Perl. This includes writing and running tests. Before you can use these modules, however, you must install them. Fortunately, Perl makes this easy.
On Unix-like platforms (including Mac OS X) as well as on Windows platforms if you have a C compiler available, the easiest way to install modules is by using the
CPAN module that comes with Perl. To install a new version of the
Test::Simple distribution, launch the CPAN shell with the cpan script:
% cpan cpan shell -- CPAN exploration and modules installation (v1.7601) ReadLine support enabled cpan> install Test::Simple Running install for module Test::Simple Running make for M/MS/MSCHWERN/Test-Simple-0.54.tar.gz <...> Appending installation info to /usr/lib/perl5/5.8.6/powerpc-linux/perllocal.pod /usr/bin/make install UNINST=1 -- OK
Test::Simple had any dependencies (it doesn’t), the shell would have detected them and tried to install them first.
If you haven’t used the
CPAN module before, it will prompt you for all sorts of information about your machine and network configuration as well as your installation preferences. Usually the defaults are fine.
By far, most Windows Perl installations use ActiveState’s Active Perl distribution (http://www.activestate.com/Products/ActivePerl/), which includes the ppm utility to download, configure, build, and install modules. With ActivePerl installed, open a console window and type:
C:\>PPM PPM> install Test-Simple
ActivePerl also has distributions for Linux and Solaris, so these instructions also work there.
If the configuration is correct, ppm will download and install the latest
Test::Simple distribution from ActiveState’s repository.
If the module that you want isn’t in the repository at all or if the version in the repository is older than you like, you have a few options.
First, you can search alternate repositories. See PodMaster’s list of ppm repositories at http://crazyinsomniac.perlmonk.org/perl/misc/Repositories.pm. For example, to use dada’s Win32 repository permanently, use the
set repository command within ppm:
C:\>PPM PPM> set repository dada http://dada.perl.it/PPM PPM> set save
If you want to install a pure-Perl module or are working on a platform that has an appropriate compiler, you can download and install the module by hand. First, find the appropriate module—perhaps by browsing http://search.cpan.org/. Then download the file and extract it to its own directory:
$ tar xvzf Test-Simple-0.54.tar.gz Test-Simple-0.54/ <...>
To set up a compilation environment for Perl on Windows, consult the README.win32 file that ships with Perl.
Run the Makefile.PL program, and then issue the standard commands to build and test the module:
$ perl Makefile.PL Checking if your kit is complete... Looks good Writing Makefile for Test::Simple $ make cp lib/Test/Builder.pm blib/lib/Test/Builder.pm cp lib/Test/Simple.pm blib/lib/Test/Simple.pm $ make test
Be sure to download the file marked This Release, not the Latest Dev. Release, unless you plan to help develop the code.
If all of the tests pass, great! Otherwise, do what you can to figure out what failed, why, and if it will hurt you. (See "Running Tests" and "Interpreting Test Results,” later in this chapter, for more information.) Finally, install the module by running
make install (as root, if you’re installing the module system-wide).
Makefile.PL uses a module called
ExtUtils::MakeMaker to configure and install other modules. Some modules use
Module::Build instead of
ExtUtils::MakeMaker. There are two main differences from the installation standpoint. First, they require you to have
Module::Build installed. Second, the installation commands are instead:
$ perl Build.PL $ perl Build $ perl Build test # perl Build install
Otherwise, they work almost identically.
Windows users may need to install Microsoft’s
nmake to install modules by hand. Where Unix users type
make, use the
nmake command instead:
nmake test, and
Q: How do I know the name to type when installing modules through PPM? I tried
install Test-More, but it couldn’t find it!
A: Type the name of the distribution, not the module within the distribution. To find the name of the distribution, search http://search.cpan.org/ for the name of the module that you want. In this example,
Test::More is part of the
Test-Simple distribution. Remove the version and use that name within PPM.
Q: I’m not an administrator on the machine, or I don’t want to install the modules for everyone. How can I install a module to a specific directory?
A: Set the
PREFIX appropriately when installing the module. For example, a
PREFIX of ~/perl/lib will install these modules to that directory (at least on Unix-like machines). Then set the
PERL5LIB environment variable to point there or remember to use the
lib pragma to add that directory to
@INC in all programs in which you want to use your locally installed modules.
If you build the module by hand, run Makefile.PL like this:
$ perl Makefile.PL PREFIX=~/perl/lib
If you use
CPAN, configure it to install modules to a directory under your control. Launch the
CPAN shell with your own user account and follow the configuration questions. When it prompts for the
Every Makefile.PL is run by perl in a separate process. Likewise we run 'make' and 'make install' in processes. If you have any parameters (e.g. PREFIX, LIB, UNINST or the like) you want to pass to the calls, please specify them here. If you don't understand this question, just press ENTER. Parameters for the 'perl Makefile.PL' command? Typical frequently used settings: PREFIX=~/perl non-root users (please see manual for more hints) Your choice: [ ]
add a prefix to a directory where you’d like to store your own modules.
If the module uses
Module::Build, pass the
installbase parameter instead:
$ perl Build.PL --installbase=~/perl
See the documentation for
Module::Build for more details.