Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Ovid (2709)

Ovid
  (email not shown publicly)
http://publius-ovidius.livejournal.com/
AOL IM: ovidperl (Add Buddy, Send Message)

Stuff with the Perl Foundation. A couple of patches in the Perl core. A few CPAN modules. That about sums it up.

Journal of Ovid (2709)

Monday July 14, 2008
03:22 AM

Customizing Test::Most

[ #36909 ]

At work, we've a couple of extra, continual needs with Test::Most. If you're not familiar with this module, it bundles all of the following test libraries into one handy library:

  • Test::More
  • Test::Exception
  • Test::Differences
  • Test::Deep
  • Test::Warn

So instead of having a bunch of different libraries being used all of the time, you only need to use this one (assuming the above list comprises the modules you need). It also allows deferred plans, halting at the first test failure, and a few other nifty functions.

However, just because it offers all of these goodies in one, easy to use package, doesn't mean that it does everything you need. It certainly isn't enough for us at the BBC, so here's my custom extension (minus POD):

package Pips3::Test::More;

use strict;
use warnings;

use Log::Log4perl;

# XXX don't use 'base' as it can override our signal handlers
use Test::Builder::Module;
our ( @ISA, @EXPORT );

use Test::Most;

BEGIN {
    @ISA    = qw(Test::Builder::Module);
    @EXPORT = @Test::Most::EXPORT;
}

sub import {
    # Initialize logging behaviour
    my $allow_logging = $ENV{PIPS3_DEBUG};
    for my $i ( 0 .. $#_ ) {
        if ( 'log' eq ( $_[$i] || '' ) ) {
            splice @_, $i, 1;
            $allow_logging = 1;
        }
    }

    unless ( $allow_logging ) {
        local $Log::Log4perl::Config::CONFIG_INTEGRITY_CHECK = 0;
        Log::Log4perl->init( \"log4perl.threshold = OFF" );
    }

    for my $i ( 0 .. $#_ ) {
        if ( 'trace' eq ( $_[$i] || '' ) ) {
            splice @_, $i, 1;
            $SIG{__DIE__} = sub {
                require Carp;
                Carp::confess(@_);
            };
        }
    }

    # 'magic' goto to avoid updating the callstack
    goto &Test::Most::import;
}

1;

By default, our Log::Log4perl setup spits out tons of messages and we usually don't want this when running a test suite, so unless you specify 'log' in the Pips3::Test::More import list, you won't see those messages.

Also, you can add 'trace' to the import list and automatically get a stack trace on failure. I'm finding this custom functions common enough that I really need to get around to making this functionality generic, officially supported and exposed. Oh, and add 'on_test_failure' callbacks.

(Or I could go the who hog and allow callbacks on just about any type of test result, but one thing at a time. Pushing this into the generator instead of the parser can be extremely useful at times, but I'm not sure I want to encourage that.)

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.