TestBuilderCookbook

From Perl QA

Jump to: navigation, search

This is a collection of problems and solutions for people building Test::Builder-based modules.

[edit] How can I throw an internal error as a test failure so I can test it with Test::Tester?

Use $Test->diag():

 use Test::Builder;
 my $Test = Test::Builder->new;
 sub _could_fail {
   my $arg = shift;
   # This sub is used internally only.
   my $result = eval { this_could_die($arg) };
   $@ ? return : $result;
 }
 sub my_test($$;$) {
   my ($value, $check, $commment) = @_;
   my $result = _could_fail($value)
      ? is($result, $check, $comment)
      : $Test->fail("$arg caused this_could_die to fail with $@");
 }

Or you could use TestException:

 use Test::More;
 use Test::Exception;
 lives_and { is( this_could_die( $arg ), $result ) } $comment;

which also gives better error reporting if this_could_die() returns a false value.

[edit] AUTHORS

  • Joe McMahon (mcmahon@cpan.org)
  • AdrianHoward
Personal tools
Mongers