What is the most idiomatic way to emulate Perl Test :: More :: done_testing?

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). , -.

+3
4

, ? :

use strict;
use warnings;
use Test::More;

BEGIN {
    my @ts = (
        [ 'Test 1', 1, 1 ],
        [ 'Test 2', 3, 3 ],
    );

    plan tests => scalar @ts;

    sub test_sets { return @ts }
}

for my $ts ( test_sets() ){
    run_test($ts);
}

sub run_test {
    my ($msg, $val, $exp) = @{shift()};
    is $val, $exp, $msg;
}
+1

, Test:: More. . t/lib ( , blib/lib), use lib "t/lib" .

+3

:

use warnings;
use strict;
use Test::More;
use List::Util 'sum';

sub count_tests {1}

BEGIN {
    plan tests => sum map {
        count_tests($_)
    } @test::set = (
        [ "Test #1", '$param1, $param2, ...' ],
        [ "Test #1", '$param1, $param2, ...' ],
    )
}

run_test($_) for @test::set;

our, @::test_set, , - test::. map sum List::Util BEGIN. , , plan , , BEGIN.

+1

, .

use Test::More;

my $Asserts_Per_Set = 10;
my %Tests = (
    "Test #1" => { foo => "bar", this => "that" },
    "Test #2" => { foo => "yar", this => 42     },
    ...
);

plan tests => keys %Tests * $Asserts_Per_Set;

for my $name (keys %Tests) {
    run_tests($name, $Tests{$name});
}

- run_tests , skip, if, .

SKIP: {
    skip "Can't run foo test on frobnitz", 2 if $test->{foo} and $test->{frobnitz};

    is foo(), $test->{foo};
    is bar(), $test->{foo} + 9;
}

- , , BEGIN.

use Test::More;
my $Count;

BEGIN { $Count += X }

...run X tests...

BEGIN { $Count += Y }

...run Y tests...

BEGIN { plan tests => $Count }

, , , , , . , BEGIN.

, Test:: More subtest, .

+1
source

Source: https://habr.com/ru/post/1746036/


All Articles