Biocomputing II

Software Testing in Perl

Notes from James Heald

James explored the Perl Test harness for Biocomputing I and has kindly supplied his notes and examples. I have reproduced his comments below.

Dear all,

Further to the online lecture on testing that Irilenia has linked, if anyone is interested in what unit tests can look like in Perl, it's something I had a go at exploring a little in my submission for last term's 8-mers and 15-mers problem, which I've uploaded.

A list of what the various files in the zip are for can be found in the README.txt file.

The unit tests can be found in the two files


and they can be run using either

   perl t/match_test.t
   perl t/file_parse.t

which print out a detailed list of tests passed, or

   prove t/*.t

which gives a quick validation summary. (The package Test::More may need to be installed from CPAN; if so, then the 'prove' utility should install itself automatically with it).

There are also more sophisticated programs that can parse the Test output format, and do things with it, see eg the list at: but I didn't get into any of them.

The syntax is very easy -- each test is only a line or two: you basically just say what the result should be for calling a subroutine with a particular set of values, and the test program gets on and does it.

But because it's so simple, it makes it very easy to add new tests to the test script as you add more twists and functionality to your subroutines (or as you discover more particular problem cases that the code then has to get extended to fix) -- while very quickly confirming that the old behaviour all still works.

I also found it quite a good discipline, encouraging me to break my code up into chunks that were small enough to be simple enough to just give some test data and confirm that they gave back the result I was expecting.

(It's also easy to check that the code gives an error properly, if you give it a particular type of bad input).

Plus, the test calls can be quite useful as a fully worked-out example of what parameters the subroutines are expecting, and what they mean.

So just in case anyone was interested in trying out something like this, I thought the code might be useful to put up.

One other thing the code explored was how you can used Perl's documentation language POD together with the Getopt::Long module for processing command-line options to get a script to automatically print a short 'usage' message if you run e.g.

    perl --help

or a detailed 'man' page, if you run

    perl --man

This may or may not be so useful for scripts running in a CGI environment, rather than a command line; but the example of POD in use for documentation may be of interest anyway.

There are also tools to take documentation in POD out of a perl script and translate it into a variety of formates, eg the command pod2html

Anyhow, I thought I'd send this, just in case it was of interest.

All best,