in reply to Looking for help for unit tests and code coverage on an existing perl script
I would extract a module from your script and unit test the module directly via Test::More and the prove command. I like to keep my script mainlines as short as is practicable, with all the heavy lifting done in (unit-tested) module/s. There are many examples of this general approach on the CPAN; see, for example, the perltidy command, part of the Perl-Tidy distribution and the perlcritic command, part of the Perl-Critic distribution.
As an alternative, and perhaps a bit less work, you could re-structure your script as a "modulino", that is, a script that can masquerade as a module for ease of testing. This approach is described at:
- How a script becomes a module by brian_d_foy
- Effective Perl Programming, 2nd edition: Item 91, "Write programs as modulinos for easy testing"
- Dr Dobbs article by brian_d_foy
- Modulino: both script and module in Perl (perlmaven) by szabgab
... though modulinos are too sneaky/clever for my tastes ("scripts should use modules, not pretend to be modules").
References Added Later
- Modulino-Demo on CPAN by brian_d_foy
- Modulino-Demo code (brian's source code on github)
- Rescue legacy code with modulinos by brian_d_foy (perl.com article 2014)
- A quick pitch about perl modulinos by Fergus McMenemie, London PM 2019 (youtube: 4:38)
PM Nodes:
- Modulinos and CPAN-appropriateness (2007)
- Modulino trick for cgi scripts (2008)
- How do you test end-user scripts? (2010)
- Modulino, perl -d question. (2012)
- modulino and $VERSION (2014)
- modulino and perldoc (2014)
- Using guards for script execution? (2017)
- TDD of non-module code by davies (2012)
- Re: printing output of a perl script to the Email body by davies (2021)
- [Solved] Parameter injection for testing by davies (2022)
- Modulino to report ip address changes by davies (2022)
- Unable to release modulino to CPAN by davies (2022)
- Running modulino inside BEGIN by LanX (2024)
Updated: Many extra references were added long after the original reply was made.