I need to build unit tests for an environment with a very old version Test::More(perl5.8 s $Test::More::VERSION being '0.80') that precedes the addition done_testing().
Upgrading to a newer test :: There can be no more talk for practical reasons. And I try to avoid using no_tests- it's usually a bad idea that doesn't break when your unit test comes out prematurely - let's say because some logic doesn't execute when you expected this.
What is the most idiomatic way to run a custom number of tests if you don't use no_testsor done_testing()?
More details:
My unit tests usually take the form:
use Test :: More;
my @test_set = (
["Test # 1", $ param1, $ param2, ...]
, ["Test # 1", $ param1, $ param2, ...]
#, ...
);
foreach my $ test (@test_set) {
run_test ($ test);
}
sub run_test {
# $ expected_tests + = count_tests ($ test);
ok (test1 ($ test)) || diag ("Test1 failed");
# ...
}
The standard approach use Test::More tests => 23;either BEGIN {plan tests => 23}does not work, because both of them are executed before being @testsknown.
My current approach involves creating @testsglobal and defining it in a block BEGIN {}as follows:
use Test :: More;
BEGIN {
our @test_set = (); # Same set of tests as above
my $ expected_tests = 0;
foreach my $ test (@tests) {
my $ expected_tests + = count_tests ($ test);
}
plan tests => $ expected_tests;
}
our @test_set; # Must do!!! Since first "our" was in BEGIN scope :(
foreach my $test (@test_set) { run_test($test); } # Same
sub run_test {} # Same
, , , . our @test_test - BEGIN{} .
- done_testing(), Test::More->builder->plan(tests=>$total_tests_calculated). , -.