![]() ![]() ![]() ![]() |
Perl Testing: A Developer's Notebook
by Ian Langworth and chromatic
O'Reilly Media, 2005
ISBN 0-596-10092-2
Reviewed by Peter Scott
The Developer's Notebook series is, according to the cover blurb, "the often frantic scribbling and notes that [...] you'll need [...] It's the code [...] stripped of commentary that can serve as more of a paperweight than an epiphany." That's certainly what you get in this book. Whether it was necessary to go to the extent of using a squared background that mimics a workbook, the faux coffee stains on the chapter headings, or the handwriting font used in marginal notes, you'll have to ask someone who understands graphic elements in textbook comprehension.. What's clear to me is that this book delivers. The most concrete evidence I can give of that is that while I had read a perl.com article and at least one other text describing the Test::Mock* modules before, I still didn't get them until I read this book. Now I do, and the difference it has made to my testing and code style is immense.
The commentary in this book is much more informal than you would expect in a textbook, which is certainly a welcome relief for me. Most books about any application of Perl would start with a (usually unnecessary) overview of syntax, but this one understands that you already have that and dives right into how to install testing modules and run tests. When there's a point where a digression into syntax might be called for, like explaining @INC, a marginal note refers the reader to perldoc instead.
It's hard for me to realize these days that there might still be people reading this that haven't constructed unit tests for Perl code, but they are out there, and this book starts from the beginning with using Test::More and Test::Harness to create .t files that you run with the prove command to measure the correctness of code. Then it shows how to test modules, and the various types of conditional testing. Condensing a lot of information into a little space - this book is under 200 pages - the next chapter shows how to simply get tests to also perform coverage analysis. (Test::TestCoverage wasn't out when this book was written; you might want to check it out as well.)
With code examples on virtually every page, it shows how to write a test module of your own, explains the Test Anything Protocol, then shows how to integrate with Module::Build. At this point it addresses the topic of black box testing, where it explains Test::MockModule and Test::MockObject, my new favorite testing modules. Say you are testing a module that calls out to a database. Testing it would normally be a pain because of the need to ensure database access, that the necessary data was there to begin with, and that the test cleaned up after itself. Not so now. Instead, you can mock the database class so that when the module being tested uses it, it calls your mocked methods instead, which give some plausible answer without bothering a database. It took me a while to grok the difference between Test::MockModule and Test::MockObject, but it has been worth the effort. But sometimes that isn't good enough and you really need to speak SQL to something; so it shows how you can mock up a test database that does that so that again your real database doesn't have to be involved. Then it shows how to use Apache::TestRun to create test instances of the Apache web server for testing Apache modules.
There is a chapter on the very object-oriented Test::Class that will make xUnit useers happy but left me a bit cold. To each his own. It winds up with simple instructions on how to make programs as testable as modules, and how to test interactive programs.
If you're not testing everything in your code, you're making work for yourself. If you want to find out how to make it easy to do that, get this book.
© Pacific Systems Design Technologies
Revised