Saturday, December 11, 2010

Day 11: less boilerplate in your test files with Test::Most

If you are still using Test::More for your test files, then have a look at Test::Most. For me, it took only a look at the synopsis to be convinced. :)

This:
use strict;
use warnings;
use Test::Exception;
use Test::Differences;
use Test::Deep;
use Test::Warn;
use Test::More tests => ...;

shrinks down to
use Test::Most tests => ...;

It can't become any shorter ... :)

But I want to show you two other features:
use Test::Most die => tests => 25;

Notice the die? The execution of your tests will stop after the first failure. This is often sensible, because one failure often leads to serveral others. And what you still see on the screen is not the real problem. You can also induce this behaviour via the environment variable DIE_ON_FAIL.

The next feature is explain. Test::More has something similar ('note explain ...'), but I did not use it until I saw it in Test::Most. It prints a diagnostic message, with all references going through Data::Dumper:
my $res = $ua->get(...);
explain 'Response: ', $res;
...

If you are using prove (which I recommend), you have to use the verbose switch (-v) to see it.

Links:

1 comment:

  1. I didn't know about explain and die. They are nice enough that I think I'll try Test::Most out to replace Test::More et al. Thanks.

    ReplyDelete

Note: Only a member of this blog may post a comment.