tags:

views:

79

answers:

2

from perldoc -f use
syntax of the function use:

   use Module VERSION LIST
   use Module VERSION
   use Module LIST
   use Module
   use VERSION

but in this case:

use Test::More tests => 5;

(it sets number of tests to 5)

What is data type of the expresion tests => 5?
Is it LIST or something else?

How can I use this parameter tests after declaration?

+8  A: 

Yes, it's the LIST mentioned - the => is just a fancy way of writing this:

use Test::More ("tests", 5);

Which in turn calls Test::More->import("tests", 5) after loading the module.

zigdon
@zigdon: thanks, can i get this parameter outside the module? I want to use it in function skip (skip $why, $how_many)
oraz
I don't think you can directly, but why not just 'use Test::More', then later call 'plan()'? That way you can store the number of tests in a variable in your script?
zigdon
@oraz: you can get the number of tests you planned for with `$tests = Test::Builder->new->has_plan` -- see the docs for [Test::Builder](http://search.cpan.org/perldoc?Test::Builder). But if you want to get this value so you know how many to skip, you should really be calling `skip_all` instead.
Ether
+6  A: 

You can ask Test::More to give you its builder object:

use Test::More tests => 5;

my $plan = Test::More->builder->has_plan;

print "I'm going to run $plan tests\n";

You don't have to make the number of tests a literal. You can compute it and store it in a variable:

use vars qw($tests);

BEGIN { $tests = ... some calculation ... }
use Test::More tests => $tests;

print "I'm going to run $tests tests\n";

You don't have to declare the plan ahead of time though:

use Test::More;

my $tests = 5;
plan( tests => $tests );

print "I'm going to run $tests tests\n";

You asked about skipping tests. If you want to skip all of the tests, you can use skip_all instead of tests:

use Test::More;

$condition = 1;

plan( $condition ? ( skip_all => "Some message" ) : ( tests => 4 ) );

pass() for 1 .. 5;

You can also do that when you want to segment the tests into groups. You figure out the number of tests in each group and sum those to create the plan. Later you know how many to skip:

use Test::More;

my( $passes, $fails ) = ( 3, 5 );
my( $skip_passes, $skip_fails ) = ( 0, 1 );

plan( tests => $passes + $fails );

SKIP: {
    skip "Skipping passes", $passes if $skip_passes;
    pass() for 1 .. $passes;
    }

SKIP: {
    skip "Skipping fails", $fails if $skip_fails;
    fail() for 1 .. $fails;
    }
brian d foy