The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Test::Exception - Convenience routines for testing exception based code

SYNOPSIS

  use Test::More tests => 4;
  use Test::Exception;

  # Check that something died
  dies_ok {$foo->method1} 'expecting to die';

  # Check that something did not die
  lives_ok {$foo->method2} 'expecting to live';

  # Check that the stringified exception matches given regex
  throws_ok {$foo->method3} qr/division by zero/, 'zero caught okay';

  # Check an exception of the given class (or subclass) is thrown
  throws_ok {$foo->method4} 'Error::Simple', 'simple error thrown';

DESCRIPTION

This module provides a few convenience methods for testing exception based code. It is built with Test::Builder and plays happily with Test::More and friends.

If you are not already familiar with Test::More now would be the time to go take a look.

dies_ok BLOCK TEST_NAME

Tests to see that BLOCK exits by dying, rather than by exiting normally. For example:

    sub div {
        my ($a, $b) = @_;
        return( $a / $b );
    };

    dies_ok { div(1, 0) } 'divide by zero detected';

A true value is returned if the test succeeds, false otherwise. $@ is guaranteed to be the cause of death (if any).

The test name is optional, but recommended.

lives_ok BLOCK TEST_NAME

Tests to see that BLOCK exits normally, and doesn't die. For example:

    sub read_file {
        my $file = shift;
        local $/ = undef;
        open(FILE, $file) or die "open failed ($!)\n";
        $file = <FILE>;
        close(FILE);
        return($file);
    };

    my $file;
    lives_ok { $file = read_file('test.txt') } 'file read';

Should a lives_ok test fail it produces appropriate diagnostic messages. For example:

    not ok 1 - file read
    #     Failed test (test.t at line 15)
    # died: open failed (No such file or directory)

A true value is returned if the test succeeds, false otherwise. $@ is guaranteed to be the cause of death (if any).

The test name is optional, but recommended.

throws_ok BLOCK REGEX, TEST_NAME
throws_ok BLOCK CLASS, TEST_NAME

Tests to see that BLOCK throws a specific exception.

In the first form the test passes if the stringified exception matches the give regular expression. For example:

    throws_ok { 
        read_file('test.txt') 
    } qr/No such file/, 'no file';

If your perl does not support qr// you can also pass a regex-like string, for example:

    throws_ok { 
        read_file('/etc/kcpassword') 
    } '/Permission denied/', 'no permissions';

NOTE: Any line in the exception containing the string Test::Exception::throws_ok( is ignored by the regex. Otherwise tests on exceptions that includes a stacktrace can match because the regex appears in the stacktrace as an argument to throws_ok.

The second form of throws_ok test passes if the exception is of the same class as the one supplied, or a subclass of that class. For example:

    throws_ok {$foo->bar} "Error::Simple", 'simple error';

Will only pass if the bar method throws an Error::Simple exception, or a subclass of an Error::Simple exception.

You can get the same effect by passing an instance of the exception you want to look for. The following is equivalent to the previous example:

    my $SIMPLE = Error::Simple->new();
    throws_ok {$foo->bar} $SIMPLE, 'simple error';

Should a throws_ok test fail it produces appropriate diagnostic messages. For example:

    not ok 3 - simple error
    #     Failed test (test.t at line 48)
    # expecting: Error::Simple exception
    # found: normal exit

A true value is returned if the test succeeds, false otherwise. $@ is guaranteed to be the cause of death (if any).

The test name is optional, but recommended.

BUGS

None known at the time of writing.

If you find any please let me know by e-mail, or report the problem with <http://rt.cpan.org/>.

TO DO

Nothing at the time of writing.

If you think this module should do something that it doesn't do at the moment please let me know.

SEE ALSO

Test::Builder provides a consistent backend for building test libraries. The following modules are all built with Test::Builder and work well together.

Test::Simple & Test::More

Basic utilities for writing tests.

Test::Differences

Test strings and data structures and show differences if not ok.

Test::Inline

Inlining your tests next to the code being tested.

AUTHOR

Adrian Howard <adrianh@quietstars.com>

If you can spare the time, please drop me a line if you find this module useful.

ACKNOWLEGEMENTS

Thanks to Michael G Schwern and Mark Fowler for suggestions and comments on initial versions of this module.

This module wouldn't be possible without the excellent Test::Builder. Thanks to chromatic <chromatic@wgz.org> and Michael G Schwern <schwern@pobox.com> for creating such a useful module.

LICENCE

Copyright 2002 Adrian Howard, All Rights Reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.