Perl: Basic Unit Testing

Good software engineering practice dictates that we should always write tests for all of our modules and classes. These tests are called Unit Tests and allow us to perform regression tests every time we change a unit – so we can ensure we haven’t broken anything in the process. The problem is, it can be very time consuming and often regarded as ‘boring’ to write these tests. Thankfully, CPAN provides a number of modules to make this very easy in Perl.

In this tutorial, we go through the process of creating the simplest of test scrips and some easy extensions to make your tests more powerful.

We are going to use the Test::Simple CPAN module to build our test script, so include the module at the top of a new Perl file:

use Test::Simple tests => 2;

The basic building block of our tests will be the ok() function. This takes two arguments, an expression in scalar context and the test name (this can be omitted, but it makes debugging easier as your test script grows) and will indicate whether the test passed or failed in the output to STDOUT.

ok ( $value eq $expected, "Test Name" );
ok ( $value < $expected );

The expression will be evaluated when the test is run. The test will pass if the result evaluates to true or fail otherwise. You can use this, along with the fact we are working in scalar context, to perform some more interesting tests. For example, this test will succeed if the array is non-empty:

ok( @my_array, "Array not empty" );

Already we have seen enough to build a reasonably powerful test suite and help us build better software. However, there are some situations where we need a little more flexibility and for these we have Test::More. Test::More is a drop-in replacement for Test::Simple – you can simply change your module include and it will work exactly as before.

use Test::More tests => 2;

Test::More gives us a range of new functions to perform more complex and powerful tests with better diagnostics. First up we have is() and isnt():

This tutorial has only scratched the surface of the big world of unit testing but the key thing to remember is that doing ANY unit testing is better than doing none, so go ahead and create some simple, repeatable tests and improve the quality of your programs.