In Perl, subtesting allows you to group tests together in a single test block, making it easier to see which tests are related. Table-driven tests, on the other hand, streamline your tests by running them based on data from a hash or array. Here's a short example demonstrating both techniques.
use Test::More;
# Start a test plan, with a total of 3 tests
use Test::More tests => 3;
# Example of subtesting
subtest 'Testing sum function' => sub {
is(sum(1, 2), 3, '1 + 2 should equal 3');
is(sum(-1, 1), 0, '(-1) + 1 should equal 0');
};
# Example of table-driven tests
my @tests = (
[1, 2, 3],
[-1, 1, 0],
[5, 5, 10]
);
foreach my $test (@tests) {
my ($a, $b, $expected) = @$test;
is(sum($a, $b), $expected, "$a + $b should equal $expected");
}
sub sum {
my ($x, $y) = @_;
return $x + $y;
}
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?