After watching a lot of test-related talks at YAPC::EU::2009, I finally got 'infected' with the "Code Should First Run In A Test" virus, so i'm currently thinking of a way to convert various utility scripts to a testable form. I think I have nailed a workable solution, but some monk wisdom would be welcome.
So far, the requirements are:
- all script should still remain in a single file to not complicate deployment;
- the script should be broken into subroutines that can be imported by an external test script;
- all added magic should be at the beginning of the file, no hidden stuff at the end.
For now, i ended up with something like this (inspired mainly by
How a script becomes a module):
######## foo.pl ############
#!/usr/bin/perl
use strict;
use warnings;
MyApp->import( 'run' );
run() unless caller();
package MyApp;
BEGIN {
use Exporter 'import';
our @EXPORT_OK = qw(run mysub);
}
sub run {
my $result = mysub();
print "$result\n";
}
sub mysub {
return "mysub called";
}
########## foo.t #############
#!/usr/bin/perl
use strict;
use warnings;
use Test::Simple tests => 1;
require 'foo.pl';
MyApp->import(qw(mysub));
ok ( mysub() =~ 'mysub' , '&mysub successfully imported' );
Now I'm satisfied:
- foo.pl does what I mean,
- prove foo.t does what I mean,
- I can still copy only foo.pl on whatever server I want and it would do it's job,
- I've reduced untestable code from 100% to just the tiny main:: package (which is copy-pasteable) and the run() subroutine (whish can be kept concise enough),
- all new magic ends after the BEGIN block in the package (new subs should be added into @EXPORT_OK for the benefit of the test script); the BEGIN block was needed to have @MyApp::EXPORT_OK defined before the import() call, otherwise I would have needed to move the main package to the end of the file.
All this being said, I think it's clear enough to show to people less Perl-inclined and to help preach the benefits of testing, even for sysadmin scripts. Any improvement ideas?
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.