Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
I use Log::Log4perl for logging, and Test::More and Test::Exception for testing. I write a number of negative tests (e.g. to see if a module reacts correctly when given incorrect input), and the output from these tests can be very verbose. This means the unexpected error messages (if any) will be far outnumbered by the expected ones, thus harder to detect when reading the output.

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:

# 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__
# foo.t use strict; use warnings; use Test::More qw/no_plan/; use Test::Exception; require_ok(q{}); # ------ 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__
$ 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
The above code takes an all-or-nothing approach to the logging, which results in the wanted possible overflow warning to be suppressed.

Do any of the Enlightened Monks have thoughts on how to selectively suppress output from tests?


In reply to Strategy for conditional logging in tests by andreas1234567

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others rifling through the Monastery: (7)
    As of 2019-02-20 09:03 GMT
    Find Nodes?
      Voting Booth?
      I use postfix dereferencing ...

      Results (106 votes). Check out past polls.

      • (Sep 10, 2018 at 22:53 UTC) Welcome new users!