rpetre has asked for the wisdom of the Perl Monks concerning the following question:
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:
Now I'm satisfied:######## 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' );
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?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Guidelines for creating self-contained testable scripts
by Marshall (Canon) on Aug 11, 2009 at 05:54 UTC | |
|
Re: Guidelines for creating self-contained testable scripts
by Anonymous Monk on Aug 11, 2009 at 02:11 UTC | |
by rpetre (Sexton) on Aug 11, 2009 at 09:11 UTC | |
by Anonymous Monk on Aug 11, 2009 at 09:39 UTC | |
|
Re: Guidelines for creating self-contained testable scripts
by pileofrogs (Priest) on Aug 12, 2009 at 17:17 UTC | |
by jdrago_999 (Hermit) on Aug 12, 2009 at 21:24 UTC | |
by rpetre (Sexton) on Aug 13, 2009 at 09:11 UTC |