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

I am working on a personal Perl module to create a basic script structure and help me learn more about the language. I created a new module called "AWSTools :: Framework" with ExtUtils :: ModuleMaker using the modulemaker command-line modulemaker . I am trying to find a suitable way to test it during development.

The created directory structure includes the following:

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

The autogenerated file 001_load.t as follows:

 # -*- 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 from 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, this will not work either.

Question: What is the correct way to run tests on Perl modules during their development?

+4
source share
5 answers

If you want to run one test file, you need to tell perl where to find your modules in the same way as for any other program. I use blib to automatically add the correct paths:

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

You can also use prove to do the same. I do not use prove , but you can read its documentation to figure it out. The -b switch should do this, but I had problems not doing the right thing (maybe just my own idiocy).

+5
source

If you use a typical toolchain (ExtUtils :: MakeMaker), it will perl Makefile.PL generate a makefile, and then make test every time after that. These commands must 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, otherwise you will 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 can also ask friendly * people on #perl or related channels in your preferred IRC networks.

* Not quite friendly

+2
source

I really think that Dist :: Zilla is flexible enough so that you can use it for the whole development. If you are not downloading CPAN , just make sure you do not have [UploadToCPAN] in dist.ini . Also make sure that [@Filter] is from any plugin packages that provide it.

Dist :: Zilla may be too much to install for just one quick module, which you are not going to touch very often. If you have more than one level in development, then this is definitely worth a look.

  • You can easily link it to your VCS using plugins. (Including git )
  • You can create a plugin for deployment on your server. This will ensure that all of your test files pass before allowing deployment ( [TestRelease] ).
  • If you don’t like the tabs in the source files, you can check this without clicking the test yourself ( [NoTabsTests] ).

Minimum dist.ini for non-CPAN dist

 name = Your-Library author = E. Xavier Ample < example@example.org > license = Perl_5 copyright_holder = E. Xavier Ample < example@example.org > copyright_year = 2012 version = 0.001 [GatherDir] [PruneCruft] [PruneFiles] filename = dist.ini filename = TODO.txt match = ^.*[.]te?mp$ [NoTabsTests] [TestRelease] [CheckExtraTests] [ModuleBuild] [FakeRelease] 

Check dist:

 dzil test dzil xtest 

If you later decide to upload it to CPAN:

+1
source

In a quick attempt to help you, I recommend looking at Testing files and test modules .

0
source

Continuing to dig and experiment, I found the following two things that work for me:

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

  • To run the script separately / directly, I add the following to the top of the script above the line use Test::More :

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

    Let me run the script directly through the commad line and in my editor (TextMate). This is based on this page from the Perl programming book .

Using the -l flag for proof seems very correct.

Regarding the "use lib" solution, I doubt it is actually the best practice. If that were the case, I expected the module maker to create a test file 001_load.t with this to begin with.

-2
source

Source: https://habr.com/ru/post/1389975/


All Articles