Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Passing Dynamic Value to Test::More tests

by jesuashok (Curate)
on Jan 19, 2006 at 03:47 UTC ( #524120=perlquestion: print w/ replies, xml ) Need Help??
jesuashok has asked for the wisdom of the Perl Monks concerning the following question:

Hi All,

use Test::More tests => 2;
How can I pass a dynamic Value to the test count Value.

like :-
$test_count = 2; use Test::More tests => $test_count;
It says that above is Invaid.

Could anyone tell me the way to pass dyanamic test count values.

"Keep pouring your ideas"

Comment on Passing Dynamic Value to Test::More tests
Select or Download Code
Re: Passing Dynamic Value to Test::More tests
by stonecolddevin (Vicar) on Jan 19, 2006 at 04:00 UTC
    can you give us some sort of error message?
    meh.
Re: Passing Dynamic Value to Test::More tests
by Madcap Laughs (Acolyte) on Jan 19, 2006 at 04:19 UTC
    Try:
    BEGIN {$test_count = 2} use Test::More tests => $test_count;
    Cheers,
    Rob
      hi

      I tried that. but that is not working.

      "Keep pouring your ideas"

        Are you using my in there? That part has to be outside the BEGIN block.

        my $test_count; BEGIN { $test_count = 2 } use Test::More tests => $test_count;

        -xdg

        Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

        Works for me:
        use warnings; BEGIN {$test_count = 2} use Test::More tests => $test_count; ok(1); ok(3);
        Outputs:

        1..2
        ok 1
        ok 2

        Cheers,
        Rob
Re: Passing Dynamic Value to Test::More tests
by Mandrake (Chaplain) on Jan 19, 2006 at 05:35 UTC
    This one works for me.
    BEGIN { $test_count = 2; eval ("use Test::More tests => ".$test_count) ; }
    Thanks..
Re: Passing Dynamic Value to Test::More tests
by chromatic (Archbishop) on Jan 19, 2006 at 06:48 UTC

    An alternate approach is:

    # compile time use Test::More; # run time my $test_count = 2; plan( tests => $test_count );

    You can use this when you don't have tests that execute in BEGIN, CHECK, or INIT blocks.

      That's a great point. I use that pattern also for when I may want to skip a test file entirely if a test dependency isn't available.

      use Test::More; eval { require Some::Crazy::Dependency }; if ($@) { plan skip_all => "Some::Crazy::Dependency not available"; } else { plan tests => 42; }

      It could be done in a BEGIN block, too, but I find it more readable without them.

      -xdg

      Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://524120]
Approved by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2014-09-30 22:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (385 votes), past polls