Perl Carp:confess unit test

413 Views Asked by At

I am trying to write a perl unit test. I am able to test happy-case scenario for it. However if there is an error generated in the method it prints the error using Carp:confess "<message>". I am not able to catch this case in my test. I tried using

dies_ok( <method call>, 'Expected Error' );

However the test case still fails. It prints the message passed to Carp::confess and then prints

Looks like your test exited with 111 before it could output anything. Dubious, test returned 111 (wstat 28416, 0x6f00)

Is there a way I can catch this? I even tried throws_ok but not working.

Please assist by guiding how I should catch these errors. Am I using these dies_ok and throws_ok incorrectly ?

1

There are 1 best solutions below

0
On

You may just check $@ after eval expression.

use strict;
use warnings;

use Test::More;

use Carp qw(confess);

sub err { confess('Bad thing'); }

eval { err };
like($@, qr/^Bad thing/, "confess('Bad thing')");

done_testing();