Saturday, June 18, 2011

Code coverage in Perl

To get code coverage percentage :


/usr/perl -MDevel::Cover <perl scripts> <arguments>


The code coverage database will be stored at a directory called :


cover_db/


It will report out how many percentage hit per run. If you would like to view the total percentage hit result, you'll need to use another command :


/usr/bin/cover


This will report out the overall hit result. However, somehow I can’t' find which line of my perl scripts has not been hit to put more test for my test suite.

Example output :

----------------------------------- ------ ------ ------ ------ ------ ------
File stmt branch cond sub time total
----------------------------------- ------ ------ ------ ------ ------ ------
hello2.pl 90.9 50.0 n/a 100.0 100.0 85.7
Total 90.9 50.0 n/a 100.0 100.0 85.7
----------------------------------- ------ ------ ------ ------ ------ ------



Another way of doing this, but this doesn't report the hit in percentage, but list out how many hits on each "effective" line.


/usr/bin/perl -d:Coverage <perl scripts> <arguments>


This will store the result into a special file, no special report or output on the screen from the run is expected.


<perl scripts>.cvp


To view the result :


/usr/bin/coverperl <perl scripts>.cvp


This will list out the effective line number and how many hits on the runs that you have fired.

Example output :

3    line 5
        1    line 7
        1    line 8
        1    line 13
        1    line 15
        1    line 17
        1    line 18
        0    line 21
        1    line 24

Note :

4 comments:

  1. walau ehh....perl script also need coverage count kah?...i thought u have enuf of coverage headache from RTL...:)

    ReplyDelete
  2. Hi Wayne, thanks for leaving a comment here. Yes, that's life. I should start work on life coverage as well. >_<

    ReplyDelete
  3. Wish U happy B-days earlier before i forgot...

    ReplyDelete