talexb has asked for the wisdom of the Perl Monks concerning the following question:

I installed Devel::Cover and ran it against my web application test suite, and got some lovely coverage information about .. my test suite.

My next step was to use Devel::Cover in the web application itself, and to do that I needed to specify the directory where I wanted to leave its database. Simple!

Or not. I tried setting %ENV in a BEGIN clause in the top-level module -- it didn't take. I tried using SetEnv in the httpd.conf file and doing a graceful restart of Apache. Nope -- no luck.

So now I'm wondering if anyone else has managed to do this -- I plan on adding more options so that Devel::Cover doesn't map out all of the parts of CGI::Application that get used -- fascinating information, but not relevant to my goal, which is to find out which paths in my code get used for particular functions.

Update at 1445, February 14, 2007: dragonchild asks, why would I want to do this? Good question.

I'm a one-man web development team, so can't really do the XP thing with anyone else. This means tools like Devel::Cover will help me understand what's happening with my code when I perform certain operations in the web application.

My first shot at using Devel::Cover was yesterday, when I ran it again my test scripts, which use LWP::UserAgent (for REST access) and WWW:Mechanize (for web access) to communicate with my web application. This produced coverage for my test scripts, but I'm more interested in the source code of my application, hence the quest for turning it on, and setting some options, from within Apache.

Alex / talexb / Toronto

"Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

Replies are listed 'Best First'.
Re: Setting Devel::Cover options for a web application
by chargrill (Parson) on Feb 14, 2007 at 19:11 UTC

    I tend to use a Makefile* for running coverage reports. Here's a relevant snippet:

    OPENCMD = open BROWSER = /Applications/ clean: cover -delete test: prove cover: make clean PERL5OPT=-MDevel::Cover=+ignore,.*\.t,-ignore,prove make test 2>&1 cover make report report: $(OPENCMD) $(BROWSER) cover_db/coverage.html

    Customized a bit for a Mac, but the relevant parts are the PERL5OPTS ENV I'm setting, which points out how to ignore things using -ignore and +ignore - as long as you know the syntax it's expecting, the docs should clear things up. In a nutshell, I'm telling Devel::Cover to ignore my .t files, as well as the prove utility. Fascinating yes, but like you, that's not what I really care about :)

    You don't necessarily need a Makefile for this, simply typing the following into an appropriate command line** will work***, too:

    $ PERL5OPT=-MDevel::Cover=+ignore,.*\.t,-ignore,prove prove

    Update: changed the PERL5OPT line a bit, added a little more explanation.

    * I'm not great with Makefiles, but the above works for me.

    ** i.e. some unixish shell, other OSes may not let you specify environment variables like that.

    *** assuming your test files are in `cwd` and named *.t.

    s**lil*; $*=join'',sort split q**; s;.*;grr; &&s+(.(.)).+$2$1+; $; = qq-$_-;s,.*,ahc,;$,.=chop for split q,,,reverse;print for($,,$;,$*,$/)

      Outstanding! I changed the httpd.conf line to

      SetEnv PERL5OPT "-MDevel::Cover=-dir,/opt/FES/data"

      And it Just Worked. The time before that, I had a space instead of the comma. Crazy stuff, man. I think I'm gonna write to the author and pass that along. Thanks again!

      Alex / talexb / Toronto

      "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

        I'd suggest another addition to your httpd.conf:

        <IfDefine COVERAGE> SetEnv PERL5OPT "-MDevel::Cover=-dir,/opt/FES/data" </IfDefine>

        Add a new section in your apachectl (or however you launch apache):

        startcover() { echo -n $"Starting apache with code coverage: " $http_bin $other_options -DCOVERAGE }

        Adjusting where necessary for different apachectl's (ours is heavily customized, but anyone reasonably familiar with apache administration ought to be able to take it from here).

        And then just invoke it thusly:

        $ apachectl startcover

        s**lil*; $*=join'',sort split q**; s;.*;grr; &&s+(.(.)).+$2$1+; $; = qq-$_-;s,.*,ahc,;$,.=chop for split q,,,reverse;print for($,,$;,$*,$/)
      Hi , I know you have done this long back am sure you have great experience on this. So it would be great if you help me in one of application to get coverage. We have web application (apache) run thru xsp files, that xsp file made with perl code so finally we need to find code on perl application. I insatalled Devel::Cover and added 'use Devel::Cover' in all xsp file. Ran application for functionality test. I can see cover_db dir and structure & run folder , aswellas digest file (with proper details) but we i run command 'Cover' then i am not getting coverage properly, getting n/a for all type of coverage. I am not sure where we are going wrong, pls help .