Tuesday, May 13, 2014

Please don't create tests like this in Perl

I just came across a test like:

And of course, it was silently doing something/nothing/everything. I'd guess most experience Perl coders will notice that the eval will only run the test if the request doesn't throw an exception. Which might be kind of ok but there is no catch or check of the $@ later.

If a check is added for $@ later, it will make the test pattern pretty messy

Please don't write this type of test. Please.

Instead use Test::Exception and drop into a sub. Something along the lines of:

This is better for a couple of reasons:
  • test will show some output in case of exception
  • test will stop instead of just pushing on with a bunch of mostly false error (unless the test wants to this to check bad input :)
  • test is simpler to understand (no response, give up)