Chapter 17. Essential Testing

As we briefly described in Chapter 16, a distribution contains a testing facility that we can invoke from make test. This allows us to write and run tests during development and maintenance, and it also lets our end user verify that the module works in her environment.

Although we cover testing briefly here, you should check out Perl Testing: A Developer’s Notebook (O’Reilly), which covers the subject in depth.

More Tests Mean Better Code

Why should we test during development? The short answer is that we find out about problems sooner and tests force us to program in much smaller chunks (since they are easier to test), which is generally good programming practice. Although we may think we have extra work to do, that’s only short-term overhead. We win down the line when we spend less time debugging, both because we’ve fixed most of the problems before they were problems and because the tests usually point us right at the problem we need to fix.

Along with that, we’ll find that it’s psychologically easier to modify code because the tests will tell us if we broke something. When we talk to our boss or coworkers, we’ll also have the confidence in our code to answer their queries and questions. The tests tell us how healthy our code is.

Different people have different ideas about testing. One school of thought, known as Test-Driven Development , says that we should write the tests before we write the code that is being tested. On the first go-around, the ...

Get Intermediate Perl now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.