Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Skipping tests if a module is not available

by Jeffrey Kegler (Hermit)
on Oct 31, 2007 at 02:44 UTC ( #648163=perlquestion: print w/replies, xml ) Need Help??
Jeffrey Kegler has asked for the wisdom of the Perl Monks concerning the following question:

My question is in the context of testing modules using Test::More and Test::Harness. Let's suppose you want to write a test to use a CPAN module (not the module being tested), but if it's not available on the site where the test is run, to gently skip one or more tests (up to or including every test in the script).

use_ok() does not seem to be the answer. It's a test in itself, which I don't want. It seems aimed at loading the module to be tested, not a module to be used for the test.

It seems a simple enough task, but I get very perplexing results. Trying this sample test script

use strict; use warnings; use Test::Weaken 0.002002; use Test::More tests => 1; pass("Just so I have a test in the example");
with perl 4monks.t, I get
1..1 ok 1 - Just so I have a test in the example
But using the test target of an ExtUtils-MakeMaker generated Makefile, I get:
t/4monks......Test::Weaken does not define $Test::Weaken::VERSION--ver +sion check failed at t/4monks.t line 4. BEGIN failed--compilation aborted at t/4monks.t line 4. t/4monks......dubious + Test returned status 2 (wstat 512, 0x200)
Seems as if Test::Harness can't find a VERSION number which a raw perl command had no trouble finding. (If you suspect I'm doing something wrong in Test::Weaken 0.002002, it's on CPAN for your inspection.)

I've tried some eval trickery, but that this problem with the mysterious disappearing VERSION number so far has defeated me.

Replies are listed 'Best First'.
Re: Skipping tests if a module is not available
by dragonchild (Archbishop) on Oct 31, 2007 at 03:08 UTC
    This is canonical:
    eval "use Test::Pod 1.14"; plan skip_all => "Test::Pod 1.14 required for testing POD" if $@;

    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
      It's not only canonical, it works! Thanks.

      I am curious what "canonical" means. Is it roughly equivalent to "used in lots and lots of well-written modules"?

      And what is the best way of searching/browsing modules? Before troubling my fellow monks with this inquiry, I tried to find test scripts that tackled this same issue, and was perplexed not to be able to find any. I suspect it was the quality of my search rather than lack of relevant scripts to be found.

        This is one of those things that you wouldn't have known if you haven't written a module for CPAN. In many CPAN distros, you'll find a pod.t and (possibly) a pod_coverage.t. Those use Test::POD and Test::Pod::Coverage respectively. In the POD for those test tools, you'll see this form. But, unless you write for CPAN where your POD syntax and coverage are important, it's unlikely you'll have ever run into those modules.

        I'd estimate that 90% of the usage that I gave you is to test for Test::POD or Test::Pod::Coverage. The rest tends to be for Test::Memory::Cycle and Test::Weaken and the like. For things like Test::Warn or Test::Deep, those are just made build requirements.

        My criteria for good software:
        1. Does it work?
        2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
Re: Skipping tests if a module is not available
by chromatic (Archbishop) on Oct 31, 2007 at 03:07 UTC

    I wonder what you'd see through both invocation schemes with warn "<$INC{Test/}>\n"; before the pass() call. I suspect the library paths may be different than what you think, if you didn't use -Mblib or some other construct on the perl call.

    If that doesn't help, look at what Test::Harness builds up for its arguments to Perl.

      That explains the mysterious disappearing VERSION problem. The Makefile's library paths and my defaults were different and the former caught an old, development version of of my module, one which didn't set VERSION. I feel so dumb sometimes.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://648163]
Approved by GrandFather
Front-paged by bart
[erix]: ( you use anchors to jump to via a #-suffix to the url -- I am sure you know )
[choroba]: ah, these

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2018-06-19 12:43 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (114 votes). Check out past polls.