andreas1234567 has asked for the wisdom of the Perl Monks concerning the following question:
I'm looking for a strategy on how to suppress output from negative tests (warnings, output from die etc.), while keeping the output enabled for the positive tests (where output is unexpected and might indicate a problem).
The dilemma is probably best shown with an example:
foo.pl
foo.t# foo.pl use strict; use warnings; use Log::Log4perl; my $conf = qq( log4perl.category.Script = INFO, ScreenAppender log4perl.appender.ScreenAppender = Log::Log4perl::Appender:: +Screen log4perl.appender.ScreenAppender.layout = PatternLayout log4perl.appender.ScreenAppender.layout.ConversionPattern=[%p] %d %M + %F:%L - %m%n ); Log::Log4perl::init( \$conf ); my $log = Log::Log4perl::->get_logger(q(Script)); # Add two numbers, no larger than 32 each sub add { my ($i, $j) = @_; $log->logdie("type error") unless ($i and $j and $i =~ m/^\d+/ and $ +j =~ m/^\d+/); $log->warn("possible overflow") unless ($i < 2**5 and $j < 2**5); return $i + $j; } # When testing: Disable all logging, # When not testing: call add() caller() ? Log::Log4perl->appender_thresholds_adjust(7) : print add(@A +RGV); 1; __END__
Run# foo.t use strict; use warnings; use Test::More qw/no_plan/; use Test::Exception; require_ok(q{foo.pl}); # ------ negative tests for add() ------ throws_ok { add(); } qr/error/i, q{Expect error when no args}; throws_ok { add('a'); } qr/error/i, q{Expect error with non-numeric in +put}; throws_ok { add('a1'); } qr/error/i, q{Expect error with non-numeric i +nput}; throws_ok { add(1,'b'); } qr/error/i, q{Expect error with non-numeric +input}; throws_ok { add(1,'b1'); } qr/error/i, q{Expect error with non-numeric + input}; # ------ positive tests for add() ------ cmp_ok(add(2,2), q{==}, 4, q{Expect 2+2=4}); # Note: I want a warning here: cmp_ok(add(1000,1000), q{==}, 2000, q{Expect 1000+1000=2000}); __END__
The above code takes an all-or-nothing approach to the logging, which results in the wanted possible overflow warning to be suppressed.$ prove --version TAP::Harness v3.10 and Perl v5.10.0 $ prove foo.t foo......ok All tests successful. Files=1, Tests=8, 0 wallclock secs ( 0.02 usr 0.00 sys + 0.08 cusr + 0.00 csys = 0.10 CPU) Result: PASS
Do any of the Enlightened Monks have thoughts on how to selectively suppress output from tests?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Strategy for conditional logging in tests
by herveus (Prior) on Apr 01, 2008 at 13:45 UTC | |
by andreas1234567 (Vicar) on Apr 01, 2008 at 16:38 UTC | |
by Thilosophy (Curate) on Apr 02, 2008 at 04:30 UTC | |
by andreas1234567 (Vicar) on Apr 02, 2008 at 05:28 UTC | |
by Thilosophy (Curate) on Apr 02, 2008 at 08:42 UTC | |
| |
|
Re: Strategy for conditional logging in tests
by wade (Pilgrim) on Apr 01, 2008 at 16:15 UTC |