Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re^3: Strategy for conditional logging in tests

by Thilosophy (Curate)
on Apr 02, 2008 at 04:30 UTC ( #677885=note: print w/replies, xml ) Need Help??

in reply to Re^2: Strategy for conditional logging in tests
in thread Strategy for conditional logging in tests

I agree that having your code as a module or not does not make a big difference here, but you should be able to "diddle the logging to appropriate levels in the .t". Is it not possible to turn off logging from the test script for your negative tests, and then turn it back on for the others?
  • Comment on Re^3: Strategy for conditional logging in tests

Replies are listed 'Best First'.
Re^4: Strategy for conditional logging in tests
by andreas1234567 (Vicar) on Apr 02, 2008 at 05:28 UTC
    Well, I could always add functionality to toggle logging on or off and invoke it during tests, e.g. something simple like this:
    sub log_toggle { ($_[0]) ? Log::Log4perl->appender_thresholds_adjust(7) : Log::Log4perl->appender_thresholds_adjust(-7); }
    However I think it's inelegant to add functionality to the script that is going to be used during testing only. I would rather that the test script did that alone.
      Maybe there is something I do not understand about Log4Perl, but could you not call Log::Log4perl->appender_thresholds_adjust from within your test script? The whole point of a configurable logging framework seems to be that you can configure logging from outside of the code that does the actual logging.
        Brilliant, I never thought of that. It works:
        # foo.t use strict; use warnings; use Test::More qw/no_plan/; use Test::Exception; require_ok(q{}); Log::Log4perl->appender_thresholds_adjust(7); # disable logging # ------ negative tests for add() ------ throws_ok { add(); } qr/error/i, q{Expect error when no args}; ... Log::Log4perl->appender_thresholds_adjust(-7); # enable logging # ------ 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__
        This gives me the warning I wanted:
        $ prove foo.t foo......1/? [WARN] 2008/04/02 11:26:41 main::add - possible + overflow foo......ok All tests successful. Files=1, Tests=8, 1 wallclock secs ( 0.01 usr 0.00 sys + 0.08 cusr + 0.01 csys = 0.10 CPU) Result: PASS
        Thanks Thilosophy!

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://677885]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2022-05-18 13:18 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (70 votes). Check out past polls.