This document provides tips on writing new tests for Module::Build. Please note that many existing tests were written prior to these guidelines and have many different styles. Please don't copy/paste old tests by rote without considering better ways to test. See sample.t for a starter test file.
sample.t
Every Module::Build test should begin with the same preamble to ensure that the test library is set properly and that the correct version of Module::Build is being tested.
use strict; use lib 't/lib'; use MBTest tests => 2; # or 'no_plan' blib_load('Module::Build');
The MBTest module is in t/lib/ and subclasses Test::More. When loaded it cleans up several environment variables that could cause problems, tweaks @INC and exports several helper functions. See that module for details.
MBTest
t/lib/
@INC
The DistGen module in t/lib/ should be used to create sample distributions for testing. It provides numerous helpful methods to create a skeleton distribution, add files, change files, and so on. Run perldoc on t/lib/DistGen.pm to see the documentation.
DistGen
perldoc
t/lib/DistGen.pm
# CREATE A TEST DISTRIBUTION use DistGen; # create dist object in a temp directory my $dist = DistGen->new; # enter the test distribution directory before further testing $dist->chdir_in; # generate the skeleton files $dist->regen;
From inside the test distribution, you can get the Module::Build object configured in Build.PL using the new_from_context method on the dist object. This is just like Module::Build's new_from_context except it passes quiet => 1 to avoid sending output to the terminal. Use the Module::Build object to test the programmatic API.
new_from_context
quiet => 1
my $mb = $dist->new_from_context( quiet => 1 ); isa_ok( $mb, "Module::Build" ); is( $mb->dist_name, "Simple", "dist_name is 'Simple'" );
The command line API is tested by running subprocesses, not via a Module::Build object. The DistGen object has helper methods for running Build.PL and Build and passing arguments on the command line.
Build.PL
Build
$dist->run_build_pl( '--quiet' ); $dist->run_build( 'test' );
The typical testing cycle is to generate or modify a test distribution, either through the DistGen object or directly in the filesystem, then regenerate the distribution and test it (or run command line tests and observe the result.)
# Modify the distribution $dist->change_build_pl( { module_name => $dist->name, license => 'artistic', } ); $dist->regen; # Get a new build object and test it $mb = $dist->new_from_context; is( $mb->license, "artistic", "saw 'artistic' license" );
This documentation is Copyright (C) 2009 by David Golden. You can redistribute it and/or modify it under the same terms as Perl 5.10.0.
To install P5re, copy and paste the appropriate command in to your terminal.
cpanm
cpanm P5re
CPAN shell
perl -MCPAN -e shell install P5re
For more information on module installation, please visit the detailed CPAN module installation guide.