I’ve got a perl subroutine that should return the number of primes between a and b inclusive. I’ve got a test for this in testSub.t:
my $arrayref = MyPackage::generatePrimes(1,2);
my @array = @$arrayref;
is ( scalar(@array), 1, "One primes between (1,2)");
I need to rerun MyPackage::generatePrimes with a few other inputs (1..3, 10..15 etc). I could just do:
my $arrayref;
my @array;
# Test between (1,2)
$arrayref = MyPackage::generatePrimes(1,2);
@array = @$arrayref;
is ( scalar(@array), 1, "One primes between (1,2)");
# Test between (1,3)
$arrayref = MyPackage::generatePrimes(1,3);
@array = @$arrayref;
is ( scalar(@array), 2, "Two primes between (1,3)");
which works fine, in this case. If I’ve got slightly more complex routines and tests, though, this doesn’t seem very clean in that there could be leakage between the tests when I re-use the variables.
What’s the correct (“clean”) method to add a second test? Is there a clean way to do this in testSub.t (tear-down arrayref and other variables that were generated by the above test before I test the subroutine again), or do I just write another test in testSub2.t?
You could simply wrap your testing code in a subroutine.
Since each test is inside the subroutine the tests are seperate from each other.
If you have more arguments you have to pass to your subroutine, you should think about passing the arguments in a hash.