teamassociated has asked for the wisdom of the Perl Monks concerning the following question:

Hi! I want to print test::more is_deeply's output to a log file or conjure some other way to notify via email when any one test fails. Please share your ideas? Thank you!
use strict; use warnings; use File::Find; use Mail::Mailer; use Test::More tests => 2; use vars ('*name', '*dir', '*prune'); *name = *File::Find::name; *dir = *File::Find::dir; *prune = *File::Find::prune; $| = 1; #snip #.... my $runlog = qq(/var/log/stgpool_lun_compare.log); my $errlog = qq(/var/log/stgpool_lun_compare.errlog); my (%lsmap15a,$lsm15a,$client15a,$status15a,$bdev15a,$lsmap15a); my (%lsmap15b,$lsm15b,$client15b,$status15b,$bdev15b,$lsmap15b); my (%lsmap16a,$lsm16a,$client16a,$status16a,$bdev16a,$lsmap16a); my (%lsmap16b,$lsm16b,$client16b,$status16b,$bdev16b,$lsmap16b,$mesg); open ( RLOG, "+>", $runlog ) or warn "$runlog did not open $!\n"; open ( ELOG, "+>", $errlog ) or warn "$errlog did not open $!\n"; my $regex = qr/\-$|^svsa.*$|^mirrored.*|^physloc.*|^$/i; open (LSMAP16B, "+< $lsmap16b") or die $!; while ( $lsm16b = (<LSMAP16B>) ) { next if $lsm16b =~ m/$regex/; if ( $lsm16b =~ m/vtopt\d+/i ) { my $skip = 3; <LSMAP16B> while $skip-- >0 and not eof LSMAP16B; next; } if ( $lsm16b =~ /^vhost\d+\s+\w+\.\d+\w\.\w+\-\w+\-\w+\s+(0x\w+$)/ +i ) { $client16b = $1; } if ( $lsm16b =~ /^status\s+(\w+$)/i ) { $status16b = $1; push ( @{$lsmap16b{$client16b}}, $status16b ); next; } if ( $lsm16b =~ /^backing\sdevice\s+(\w+_\w+\.\w+$)/i ) { $bdev16b = $1; push ( @{$lsmap16b{$client16b}}, $bdev16b ); next; } } ### These 2 print lines below are not printing to the log ### print RLOG is_deeply(\%lsmap16a, \%lsmap16b, 'vios16a-vios16b-map-comp +are' ); print ELOG grep {/failed.*/i}<RLOG>; __STDOUT__ # Failed test 'vios16a-vios16b-map-compare' # at stgpool_lun_compare.plx line 194. # Structures begin differing at: # $got->{0x00000012}[1] = 'twcsapxrqdb1_r1.c24452093bce96e2f4 +8808ed3f7d7eab' # $expected->{0x00000012}[1] = 'twcsapxrqdb1_r1.c24452093bce96e2f4 +8808ed3f7d7eac' $VAR1 = { '0x00000019' => [ 'Available', 'sapepd00_r1.b2094c34c4a13b0a726a39f92a9e9 +ed7', 'Available', 'sapepd00_r2.96db0cd89258769f4ab05247ac2b3 +fa2', 'Available', 'sapepd00_e1.b56cbd304aa61c6ca9ae9edc1b68b +5e4', 'Available', 'sapepd00_e2.f41786148a3beeeef9c7b1b53fe54 +0e8', 'Available', 'sapepd00_p1.4a2facfe0f6f4237817de94c06f2f +ccd' ], '0x00000012' => [ 'Available', 'twcsapxrqdb1_r1.c24452093bce96e2f48808ed3 +f7d7eab', 'Available', 'twcsapxrqdb1_r2.dd39252cef8d45a4c24d59be4 +38ef60d', 'Available', 'twcsapxrqdb1_x1.4d9b58368e93e12fa7ab4a2d1 +950ec13', 'Available', 'twcsapxrqdb1_x2.51369af641d03701e977f7e95 +0c00905', 'Available', 'twcsapxrqdb1_x3.24ebd57b77a43ba21d46da58c +7fa5323', 'Available', 'twcsapxrqdb1_p1.60dc076867436ed6af776adb4 +05fe269' ] };

Replies are listed 'Best First'.
Re: getting is_deeply test::more to print to log ( Test::More->builder->failure_output )
by 1nickt (Canon) on Aug 28, 2015 at 15:30 UTC

    Print to a log file:

    #!/usr/bin/perl use strict; use warnings; use Test::More; my @valid = qw/ foo bar baz /; my @compare = qw/ foo qux baz /; my $log_file = 'log.txt'; my $err_file = 'err.txt'; Test::More->builder->output( $log_file ); Test::More->builder->failure_output( $err_file ); is_deeply( \@compare, \@valid, 'My Test' ); done_testing; __END__
    $ cat log.txt not ok 1 - My Test 1..1 $
    $ cat err.txt # Failed test 'My Test' # at 1140351.pl line 17. # Structures begin differing at: # $got->[1] = 'qux' # $expected->[1] = 'bar' # Looks like you failed 1 test of 1. $

    E-mail from there would be up to you, or maybe there's a module already on CPAN for it.

    The way forward always starts with a minimal test.
Re: getting is_deeply test::more to print to log
by vinoth.ree (Monsignor) on Aug 28, 2015 at 15:29 UTC

    are you looking for this?

    # run perl tests in DIRTESTS, save results in DIRLOG # and send email alarm if tests fail LOG=$DIRLOG/my_tests_`date +%y%m%d.log` cd $DIRTESTS prove *.t | tee $LOG if [ `cat $LOG | grep PASS | wc -l` == '0' ]; then ( echo "FROM: prove" echo "TO: you@somewhere.com" echo "SUBJECT: test failed" cat $LOG ) | sendmail -t fi

    All is well. I learn by answering your questions...