“Annotations” That Only Appear When a Unit Test Fails

I use JUnitPHPUnit and Perl's Test::Unit. Perl's version has a very cool feature. You can call annotate(string) at any point during the test, and this text will get recorded, and only outputted in the case of the test's failure. 

sub test_foo {
   my $self = shift;
   my $value = setup_something();
   $self->annotate("value = $value n");
   $self->assert_equals(12, do_something($value));

So you can just log arbitrary stuff during the test, and it will be output when you need it (when the test fails), but won't clutter up the test output (when the test succeeds).

I wish this was available from the PHP and Java unit testing frameworks!

This article is © Adrian Smith.
It was originally published on 9 Feb 2009
More on: Coding | Perl