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

perl, unit-testing

Solution

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';
};

Problem

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.

Original source