April 2008
M T W T F S S
« Mar   Jul »
 123456
78910111213
14151617181920
21222324252627
282930  
Links

Testing Perl Code - Suppressing Warnings

A lot of developers I have worked with have been amazed to see that I expand a suite of tests they developed by a factor of four. This is almost always because I am stressing error handling which is often not tested at all. Unfortunately, these tests will often cause many warnings. Sometimes I want to verify the exact warning. Other times it just clutters the output being sent to the screen. This can be very dangerous - it may lead someone running the test to believe there is a newly introduced bug.

Luckily, perl has a well defined mechanism for overriding the warn behavior. If you want to suppress warnings from being displayed in a section of tests do the following:

    • Scope the section of code where you want to suppress the warnings:
      { ... }
      Define your own warning handler inside the scoped code:


      {
           local $SIG{__WARN__}=sub{}; #Temporarily suppress warnings


           # Insert test code here
      }

  • Once the scoping ends, whatever warning handler was in effect before will be active again.

     

    One Response to “Testing Perl Code - Suppressing Warnings”

    1. chandrashekhar says:

      hi,
      i m new to testing, i would like to know hw to test already developed code in perl

    Leave a Reply