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

A Test::More question:

I've written a script that when run with a --test flag, calls a subroutine that performs unit tests:

sub run_tests { use Test::More qw/no_plan/; ...(tests follow)... }
Unfortunately, during normal operation (in which this sub is never called), Test::More produces the output:
#No tests run!

How I can suppress that?

Is this really a question about how to conditionally load a module?

Thanks!
David

Replies are listed 'Best First'.
Re: No tests run!
by Zaxo (Archbishop) on Jul 24, 2003 at 02:38 UTC
    Is this really a question about how to conditionally load a module?

    I believe so. I think that loading Test::More at runtime, with require, will do as you want:

    sub run_tests { require Test::More; Test::More->import 'no_plan'; # ... }
    Untested.

    It is customary to separate testing from your code's runtime by using ExtUtil::MakeMaker's conventions to produce a standalone test suite. The 'make test' step of installation runs the tests before installation, relieving the code of keeping them around every time it's run.

    You may be interested in Test::Inline.

    After Compline,
    Zaxo

      Zaxo,

      Thanks! Your idea also works (with added parens around 'no_plan').

      I've heard of MakeMaker and intend to give it a look, along with ExtUtils::ModuleMaker.

      -David

Re: No tests run!
by sauoq (Abbot) on Jul 24, 2003 at 02:33 UTC
    Is this really a question about how to conditionally load a module?

    Yes. And in your case, I'd use eval.

    sub run_tests { eval q( use Test::More qw/no_plan/ ); # ... }

    -sauoq
    "My two cents aren't worth a dime.";
    
      Sauoq,
      Worked like a charm. Thanks!
      -David