“Annotations” That Only Appear When a Unit Test Fails

By Adrian Smith9 Feb 2009200 words1 mins to read

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 was written by Adrian Smith on 9 Feb 2009

Follow me: Facebook | Twitter | LinkedIn | Email

More on: Coding | Perl