nysus has asked for the wisdom of the Perl Monks concerning the following question:
UPDATE:I discovered Test::Fatal and used it like so:
like(exception{ $checker->_get_event_data('qwerty.pl') }, qr/Failed to + retrieve data from file/, 'file does not exist');
logf subroutine required following adjustment:
$logger->fatal("(Line: $line) " . $error ); print "\n"; open (STDOUT, '>/dev/null'); #squelch duplicate error output die $error;
END UPDATE
I've got a simple subroutine utility that I've been using with all my scripts that prints out the details of a script failure using the Log::Log4Perlmodule:
sub logf { my $error = shift; my $line = shift; if (!$line) { $line = [caller(0)]->[2]; } $logger->fatal("(Line: $line) " . $error); die "\n"; }
I want to test the following bit of code:
sub _get_event_data { my $self = shift; my $file = shift; my $data; eval { $data = retrieve($file); }; if ($@) { logf('Failed to retrieve data from file'); ### Line that calls fai +lure subroutine above } my $existing_events = $data->{existing_events}; return $existing_events; }
I want to test to see if the module returns the correct error messages. Here is the test:
throws_ok{ $checker->_get_event_data('qwerty.pl') } qr/Failed to retri +eve data from file/, 'file does not exist';
The test never passes, however because it has no idea what the $logger object printed. I tried changing the logf() function to:
die ($logger->fatal("(Line: $line) " . $error));This did not get the test to pass, however. The following worked but the test script dies and the remaining tests don't run:
$logger->fatal("(Line: $line) " . $error); die "$error\n";
So am I going to have to abandon my user of the Log4Perl module to be able to test the errors my script generates? Is there something else I might try?
$PM = "Perl Monk's";
$MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate";
$nysus = $PM . ' ' . $MCF;
Click here if you love Perl Monks
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Testing output of logger output when dying
by stevieb (Canon) on May 20, 2016 at 15:50 UTC | |
|
Re: Testing output of logger output when dying
by Corion (Patriarch) on May 20, 2016 at 15:33 UTC | |
by nysus (Parson) on May 20, 2016 at 15:45 UTC |