I have written a small Perl script and now I would like to create a test suite for it. I thought it would be nice to be able to use
the script as a module, import the subs defined in the script and test these. Is there a way to have the script both standalone Perl script and Perl module? (I do not want to split the script into a separate module and the “executable”, since I plan to distribute the script as a single file.)
Or is there a better way to test the script?
Then all you have to do is visit the file with your browser, eg. http://www.yoursite.com/cgi-bin/test-perl.pl. print "Content-Type: text/htmlnn"; print "Perl is working!
Write Test Cases with Test::Simple #!/usr/bin/perl use strict; use warnings; use Test::Simple tests => 2; sub hello_world { return "Hello world!"; } sub get_number { return int(rand(1000)); } ok( hello_world( ) eq "Hello world!", "My Testcase 1" ); ok( get_number( ) > 0, "My Testcase 2" );
Since brian seems to be asleep right now, here's a pointer to what he calls modulinos. This is basically a recipe for building scripts that act like modules or modules that can be run like scripts. Sounds exactly like what you are looking for.
Mastering Perl is definitely a book worth reading (and buying).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With