Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Re: How to write testable command line script?

by davido (Cardinal)
on Nov 21, 2018 at 16:13 UTC ( #1226132=note: print w/replies, xml ) Need Help??

in reply to How to write testable command line script?

In answer to the question "How to write testable command line script?", you do so by separating the view from the model, and you do THAT by putting the business logic (the algorithms, in this case) in a module that can be loaded up by your tests. If the model is small and the view (how input is received and how output is rendered) is small, you can do both in the same file in the form of a Modulino (See Mastering Perl, or have a look at

If your presentation is tightly coupled with your algorithms (your business logic) then the first step is to get tests around the full life cycle of the script, and then begin refactoring to achieve the level of decoupling needed to facilitate more thorough unit testing.

It does appear that your current script at least has a main() subroutine, but I suspect that your problem in how you are invoking main() is that you are passing all the fields as a single string, whereas the script is expecting each field to be an element passed by @ARGV. But we haven't seen the target code so that's mostly a guess. If your code looks like this:

sub main { my @fields = scalar(@_) ? @_ : @ARGV; .... return @result_set; }

Then at least main accepts a parameter list. If it does not, then you probably are only working with @ARGV, so you'll need to set @ARGV before each test call, and not bother passing args to main(). But if main() does take args, you're partway there, but probably need to pass them as a list rather than a single string. Also, presumably your script prints the result to STDOUT. But your tests are looking in the return value of main for a string. That likely is broken. You'll probably need to assure that results are always returned by main, and that you are testing the results as a list rather than just a string.

This is where separating the view and controller from the model is important: You actually need two views -- one for when this is invoked from the command line, and one for when main() is called directly, because when main() is called directly you probably don't need to be sending output to STDOUT.

If you absolutely must put output on STDOUT rather than as a return value, you can test that using Test::Output or Capture::Tiny.

Update: I see that you have started down the path, and are passing @ARGV to main, so you're partway there.


Replies are listed 'Best First'.
Re^2: How to write testable command line script?
by thechartist (Scribe) on Nov 21, 2018 at 22:48 UTC

    Thanks for the input.

    I was working on the "modulino" approach first, before I start separating things out into modules. I have a few Perl scripts that I'd like to re-write in a more verifiable fashion, so I am jumping around the testing tools and docs to see what I can get done first.

    I'd also like to help out more, particularly in the PDL modules, but I have to learn a lot more about the testing tools before I could be useful to anyone.

    My questions regarding return values:

  • 1. How are return values propagated throughout the call chain? Ie. If I indirectly call my reduce sub through a number of other functions (in this case, I have the root main sub, an add sub and finally the reduce sub that calculates the final answer and exits. What should each of those subroutines return to make testing consistent?
  • 2. How do you deploy scripts written in the modular style you advocate? I assume the *.pm files are called by a top level *.pl file that acts like the file that holds the main function in a C program, if that makes sense. Ie. I write that imports subs and structures from any number of *.pm files. The user invokes and has no need to worry about any of the *.pm files if they are installed correctly.

      Each subroutine should be tested individually. Consider this contrived and silly example:

      use List::Util qw(sum); use Scalar::Util qw(looks_like_number); use Test::More; ok looks_like_number(1), 'Found a number.'; ok !looks_like_number('a'), 'Rejected a non-number.'; is_deeply [map {$_ + 1} (1,2,3,4)], [2,3,4,5], 'Correct mapping.'; is_deeply [grep {looks_like_number($_)} qw(a 1 b 2 c 3 d 4)], [1,2,3,4 +], 'Correct filter.'; cmp_ok sum(1,2,3,4), '==', 10, 'Sum was correct.'; cmp_ok sum_of_incremented_nums(qw(1 a 2 b c 3 d 4)), '==', 10, 'summed + dirty list properly.'; # Integration: sub sum_of_incremented_nums { return sum(map{$_+1} grep {looks_like_number($_)} @_); }

      Here we've tested (minimally) all the components individually, and then tested the thing that uses the components.

      How to deploy? A really simple way is to use the features of ExtUtils::MakeMaker. It can place your modules where modules live, and your executables where they're supposed to live on a given system. And the user is able to specify alternate locations based on environment settings and on how Perl was compiled and where it lives. You'll have a Makefile.PL that generates a makefile customized for your specific needs. The makefile will create the proper make directives, and you'll have 90% of what goes into a CPAN distribution when you're done. Consider any module on CPAN that bundles an executable script as part of the distribution as prior art. I haven't looked recently, but Carton, App::cpanoutdated, App::cpanminus, Devel::NYTProf, Perl::Critic, and Perl::Tidy are all examples of CPAN modules that bundle executables.

      That said, you might also consider a minimal packaging system like Carton. Or combine that with something like Docker where you have more control over the isolated environment.

      As for a structure, I typically do something like this:

      ./projectdir \ \ - projectdir/lib/ - projectdir/bin/ - projectdir/t/ - projectdir/xt/ - projectdir/README

      In your executable (projectdir/bin/foo) you might do something like this:

      #!/usr/bin/env perl use strict; use warnings; use FindBin qw($Bin); use lib "$Bin/../lib"; use MyModule; ...

      This works in situations where you aren't deploying the module to a location known to PERL5LIB and not known to some tool such as Carton.


        Thanks for revisiting this. I have seen a number of good solutions here, and have a better understanding of the testing tools.

        My current goal is to clean up the initial script so that the test code actually runs. I've done some work on this in the morning, and I see that my tests are using numeric equality improperly.

        I will be looking at some of the subs in Test::More first, then I will explore some of the ideas presented in the re-writes of my code. Once it is cleaned up, it might make for a good tutorial series to introduce newcomers the concept of testing right at the beginning of their studies.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1226132]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (3)
As of 2019-05-22 04:35 GMT
Find Nodes?
    Voting Booth?
    Do you enjoy 3D movies?

    Results (138 votes). Check out past polls.

    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!