http://www.perlmonks.org?node_id=193664

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

We've written a simple script to do a simple task, still we'd like to include unit tests...

The problem is that if we don't run in test mode, we still get this message printed to stderr:

# No tests run!

Since the code will run from cron, this is a small hassle. The relivant bits are:

use Test::More qw(no_plan); if ( @ARGV == 0 ) { main(); } elsif ( @ARGV == 2 && lc( $ARGV[0] ) eq 'test' ) { Run_Test( $ARGV[1] ); } else { die "bad arguments: the comments may help in $0\n"; }


email: mandog

Replies are listed 'Best First'.
Re: self testing code Test::More
by dws (Chancellor) on Aug 29, 2002 at 05:01 UTC
    Try something like
    my $TESTING = 1; ... if ( $TESTING ) { require Test::More; import Test::More qw(no_plan); Run_Test(...); }
Re: self testing code Test::More
by jmcnamara (Monsignor) on Aug 29, 2002 at 08:17 UTC

    You could use the autouse pragma to defer loading of the Test::More functions until they are required:
    #!/usr/bin/perl -w use autouse Test::More => 'no_plan'; if ( @ARGV == 0 ) { main(); } elsif ( @ARGV == 2 && lc( $ARGV[0] ) eq 'test' ) { Run_Test( $ARGV[1] ); } else { die "bad arguments: the comments may help in $0\n"; }

    --
    John.