Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

A Better 't/00-load.t'

by Ovid (Cardinal)
on Oct 03, 2007 at 08:02 UTC ( #642327=sourcecode: print w/replies, xml ) Need Help??
Category: Testing
Author/Contact Info Ovid

In test suites, you often see a test program named something like t/00-load.t. The leading '00' generally ensures that this test is run before others. Usually this test is designed to load all of your packages and ensure that they compile. There's no point in continuing testing if they don't. Unfortunately, it's easy to forget to add a new module to the list if you forget about it. I do that all the time and I hate that.

The following 'improved' load tester has the following features:

  • Finds and loads your modules automatically
  • Is taint safe
  • Should run on Perls as old as 5.004 (I think)
  • Cross-platform
  • Calculates the test plan for you
  • Optionally verifies that all packages have same version number
#!/usr/bin/perl -wT

use strict;

use File::Spec;
use File::Find;
use Test::More;
use constant DISTRIBUTION  => 'TAP::Parser';

# Set this to true if all packages have the same version number
use constant CHECK_VERSION => 1;

sub file_to_pm {
    my ( $dir, $file ) = @_;
    $file =~ s/\.pm$// || return;    # we only care about .pm files
    $file =~ s{\\}{/}g;              # to make win32 happy
    $dir  =~ s{\\}{/}g;              # to make win32 happy
    $file =~ s/^$dir//;
    my $_package = join '::' => grep $_ => File::Spec->splitdir($file)

    # untaint that puppy!
    my ($package) = $_package =~ /^([\w]+(?:::[\w]+)*)$/;
    return DISTRIBUTION eq $package ? () : $package;

    my $dir = 'lib';

    my @classes;
        {   no_chdir => 1,      # keeps it taint safe
            wanted   => sub {
                -f && /\.pm$/
                  && push @classes => file_to_pm( $dir, $File::Find::n
+ame );
    my $tests_per_class = CHECK_VERSION ? 2 : 1;
    plan tests => $tests_per_class + $tests_per_class * @classes;

    foreach my $class ( DISTRIBUTION, sort @classes ) {
        use_ok $class or BAIL_OUT("Could not load $class");
        if (CHECK_VERSION) {
            is $class->VERSION, DISTRIBUTION->VERSION,
              "... and $class should have the correct version";
    diag("Testing Test::Harness $Test::Harness::VERSION, Perl $], $^X"
Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://642327]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2018-01-21 19:28 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (230 votes). Check out past polls.