Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 586
Sal. Avatar asked Dec 11 '22 12:12

Sal.


1 Answers

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';
};
like image 53
Joel Berger Avatar answered Jan 06 '23 16:01

Joel Berger