Hack #64. See Test Failure Diagnostics — in Color!

Highlight the unexpected.

If you follow good development practices and write comprehensive unit tests for your programs, you'll be able to develop faster and more reliably. You'll also eventually run into the problem of too many successes hiding the failures—that is, if you keep your tests always succeeding, you only need to know about the tests that fail.

Why not make them stand out?

Perl's standard testing harness, Test::Harness is actually a nice wrapper around Test::Harness::Straps, which is a parser for the TAP format that standard tests follow. If and when the report that Test::Harness writes isn't sufficient, use Test::Harness::Straps to write your own.

The Hack

There are two barriers to this approach. First, the default behavior of Perl's standard testing tools is to write diagnostic output to STDERR. Test::Harness doesn't capture this. Second, Test::Harness goes to a bit of trouble to set up the appropriate command line paths to run tests appropriately.

The first problem is tractable, at least if you can use a module such as IPC::Open3 to capture STDERR and STDOUT. The second problem is a little trickier. The current version of Test::Harness::Straps, which ships with Test::Harness 2.48, doesn't quite provide everything publicly that you need to run tests well. Hopefully a future version will correct this, but for now, the hack is to use a private method, _command_line( ), to generate the appropriate command for running ...

Get Perl Hacks 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.