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 https://perlmaven.com/modulino-both-script-and-module).

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.


Dave


In reply to Re: How to write testable command line script? by davido
in thread How to write testable command line script? by thechartist

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.