Alan W. Smith
Alan W. Smith

Reputation: 25445

What is the proper way to test perl modules during development?

I'm working on a personal Perl module to build a basic script framework and to help me learn more about the language. I've created a new module called "AWSTools::Framework" with ExtUtils::ModuleMaker via the command line tool modulemaker. I'm trying to figure out the appropriate way to test it during development.

The directory structure that was created includes the following:

./AWSTOOLS/Framework/lib/AWSTools/Framework.pm
./AWSTOOLS/Framework/t/001_load.t

The autogenerated 001_load.t file looks like this:

# -*- perl -*-

# t/001_load.t - check module loading and create testing directory

use Test::More tests => 2;

BEGIN { use_ok( 'AWSTools::Framework' ); }

my $object = AWSTools::Framework->new ();
isa_ok ($object, 'AWSTools::Framework');

If I try to run the script directly (either from the command line or inside my TextMate editor), it fails with:

Can't locate AWSTools/Framework.pm in @INC....

If I try to run prove in the ./AWSTOOLS/Framework directory, it fails as well.

The question is: What is the proper way to run the tests on Perl modules while developing them?

Upvotes: 4

Views: 1752

Answers (5)

brian d foy
brian d foy

Reputation: 132822

If you want to run a single test file, you need to tell perl where to find your modules just like you would for any other program. I use the blib to automatically add the right paths:

 $ perl Makefile.PL; make; perl -Mblib t/some_test.t

You can also use prove to do the same thing. I don't use prove, but you can read its documentation to figure it out. The -b switch should do that, but I've had problems with it not doing the right thing (could just be my own idiocy).

Upvotes: 5

Brad Gilbert
Brad Gilbert

Reputation: 34120

I actually think that Dist::Zilla is sufficiently flexible enough to allow you to use it for all development. If you aren't uploading to CPAN, just make sure you don't have [UploadToCPAN] in your dist.ini. Also make sure to [@Filter] it out of any plugin bundles which provide it.

Dist::Zilla may be too much to install for only one quick module that you aren't going to touch very often. If you have more than one dist in development then it is definitely worth a look.

  • You can easily interface it with your VCS using plugins. (Including Git)
  • You can create a plugin to deploy onto your server. Which would allow you to make sure that all your test files pass before allowing you to deploy ([TestRelease]).
  • If you don't like tabs in your source files, you can test for that without writing the test yourself ([NoTabsTests]).

Minimal dist.ini for non-CPAN dist

name    = Your-Library
author  = E. Xavier Ample <[email protected]>
license = Perl_5
copyright_holder = E. Xavier Ample <[email protected]>
copyright_year   = 2012

version = 0.001

[GatherDir]
[PruneCruft]
[PruneFiles]
filename = dist.ini
filename = TODO.txt
match = ^.*[.]te?mp$

[NoTabsTests]
[TestRelease]
[CheckExtraTests]

[ModuleBuild]
[FakeRelease]

Test the dist:

dzil test
dzil xtest

If at a later date, you decide to upload it to CPAN:

Upvotes: 1

Dynamic
Dynamic

Reputation: 927

In a quick attempt to help you, I would recommend looking at Testing Files and Test Modules.

Upvotes: 0

Josh Y.
Josh Y.

Reputation: 876

If you're using the typical toolchain (ExtUtils::MakeMaker) it will be perl Makefile.PL to generate a makefile, then make test every time afterward. Those commands should be run from the root directory of the module. See http://search.cpan.org/perldoc?ExtUtils::MakeMaker#make_test

Edit: and don't do it all manually, or you will come to hate testing. (Well, more than usual.) You will also want to look at least briefly at Test::Tutorial and https://www.socialtext.net/perl5/testing

You may also want to ask the friendly* people in #perl or related channels on your preferred IRC networks.

*Not actually friendly

Upvotes: 2

Alan W. Smith
Alan W. Smith

Reputation: 25445

Continuing to dig around and experiment, I've found the following two things which work for me:

  1. Use prove -l in the './AWSTOOLS/Framework' directory. According to the prove perldoc page, it adds the "lib" directory to the path when Perl runs all the tests in the "t" directory.

  2. To run the script individually/directly, I'm adding the following to the start of the script above the use Test::More line:

     use FindBin qw($Bin);
     use lib "$Bin/../lib";
    

    This let's me run the script directly via the commad line and in my editor (TextMate). This is based off this page from the Programming Perl book.

Using the -l flag for prove seems very much like the correct thing to do.

As for the "use lib" solution, I doubt that's actually a best practice. If it was, I would expect that modulemaker would have created the 001_load.t test file with that to begin with.

Upvotes: -2

Related Questions