Sal.
Sal.

Reputation: 41

Scoping variables in a Perl Test::More .t file

Is there a way to scope variables for Test::More tests in a .t file? For example:

# 1st Test
$gotResult = $myObject->runMethod1();
$expectedResult = "value1";
is($gotResult, $expectedResult, "validate runMethod1()");

#2nd Test
$gotResult = $myObject->runMethod2();
$expectedResult = "value2";
is($gotResult, $expectedResult, "validate runMethod2()");

#3rd Test
...

I'm looking for a way to discretely manage the individual tests in a .t file so conflicts/errors are not introduced if variable names are reused between tests.

Sal.

Upvotes: 4

Views: 144

Answers (2)

Joel Berger
Joel Berger

Reputation: 20280

To expand on mirod's correct answer: you may scope variables with braces as you would for any Perl program, however you may take it a step further. Test::More has the concept of a subtest, in which you define a subref which contains one or more tests which run together (and of course creating a scope in the process).

subtest 'Subtest description here' => sub {
  # do some setup, then do some tests
  ok 1, 'the simplest test';
};

Upvotes: 8

mirod
mirod

Reputation: 16171

Wrap each test in braces:

{ # test1 ...
}

{ # test 2 ...
}

Upvotes: 5

Related Questions