Stuff with the Perl Foundation. A couple of patches in the Perl core. A few CPAN modules. That about sums it up.
Our latest custom Test::More looks something like this:
package Our::Test::More;
use strict;
use warnings;
use Hook::LexWrap;
# XXX don't use 'base' as it can override our signal handlers
use Test::Builder::Module;
our ( @ISA, @EXPORT );
use Test::More;
use Test::Differences;
use Test::Exception;
BEGIN {
@ISA = qw(Test::Builder::Module);
@EXPORT = (
@Test::More::EXPORT,
@Test::Differences::EXPORT,
@Test::Exception::EXPORT,
'explain',
);
if ( Test::Differences->VERSION <= 0.47 ) {
# XXX There's a bug in Test::Differences 0.47 which attempts to render
# an AoH in a cleaner 'table' format.
# http://rt.cpan.org/Public/Bug/Display.html?id=29732
no warnings 'redefine';
*Test::Differences::_isnt_HASH_of_scalars = sub {
return 1 if ref ne "HASH";
return scalar grep ref, values %$_;
};
}
}
sub import {
for my $i (0.. $#_) {
if ('fail' eq $_[$i]) {
splice @_, $i, 1;
wrap 'Test::Builder::ok', post => sub {
if (![ $_[0]->summary ]->[-1]) {
die "Test failed. Halting";
}
};
last;
}
}
# 'magic' goto to avoid updating the callstack
goto &Test::Builder::Module::import;
}
sub explain {
return unless $ENV{TEST_VERBOSE};
Test::More::diag(@_);
}
1;
This does several things:
The three test modules included are ones that we use constantly, so it makes sense to include them.
The explain() function was needed because several developers really want diag() output for times when you want to read the test narrative more clearly. However, that output is useless when running the full test suite in non-verbose mode. It requires the latest version of Test::Harness in subversion because the $ENV{TEST_VERBOSE} functionality was accidentally left out (that would be my fault, I think).
The 'fail' argument to the import list is very handy when you have several hundred tests scrolling by and you hate scrolling back up through reams of junk to find the failure. Use it like this:
use Our::Test::More 'fail', 'no_plan';
BEGIN {
use_ok 'Some::Module', ':all' or die;
}
ok defined &foobar, 'foobar() is in the house!';
throws_ok { foobar( 5 ) }
'My::Exception',
"... but he don't like no arguments";
eq_or_diff foobar(), foobar(),
"... and he repeats himself a lot"'
ok defined &barfoo, 'barfoo() has somehow shown up';
foreach my $thing (barfoo()) {
explain("Testing $thing");
do_some_test($thing);
}
...
As you can see, you get more testing functions, the "explain()" stuff might spew out loads of junk, but you won't see it when you run the full test suite, and it will halt as soon as the first test fails.
explain() should be in Test::More core (Score:1)
Thanks for posting this. In 2005 I provided a patch for an explain() equivalent to Test::More, but it was never added:
http://rt.cpan.org/Public/Bug/Display.html?id=14764 [cpan.org]
It's such a sensible, helpful feature, I don't understand why it remains a feature which people continue to have to rediscover and patch themselves.
Re: (Score:2)
It's getting to the point where I'm thinking about just releasing a custom Test::More::Extra (or something) to handle this stuff. The problem is figuring out which test modules are the most common. Just choosing mine seems a bit cheeky.
Dude, where's my diagnostics? (Score:2)
Re: (Score:2)
Thanks. I'll look into this more closely.
Test::Differences vs Test::Deep (Score:1)
Re: (Score:2)
I just added Test::Deep. Consider the code:
Consider the output: