Now that we have switched everything to done_testing(), the subtests feature isn't that relevant anymore, but it might still be useful to get better output when running with PROVE_FLAGS=--verbose. Compare before:

t/001_basic.pl ..
1..8
ok 1 - vacuumlo --help exit code 0
ok 2 - vacuumlo --help goes to stdout
ok 3 - vacuumlo --help nothing to stderr
ok 4 - vacuumlo --version exit code 0
ok 5 - vacuumlo --version goes to stdout
ok 6 - vacuumlo --version nothing to stderr
ok 7 - vacuumlo with invalid option nonzero exit code
ok 8 - vacuumlo with invalid option prints error message
ok
All tests successful.
Files=1, Tests=8, 0 wallclock secs ( 0.02 usr 0.00 sys + 0.08 cusr 0.05 csys = 0.15 CPU)
Result: PASS

After (with attached patch):

t/001_basic.pl ..
# Subtest: vacuumlo --help
    ok 1 - exit code 0
    ok 2 - goes to stdout
    ok 3 - nothing to stderr
    1..3
ok 1 - vacuumlo --help
# Subtest: vacuumlo --version
    ok 1 - exit code 0
    ok 2 - goes to stdout
    ok 3 - nothing to stderr
    1..3
ok 2 - vacuumlo --version
# Subtest: vacuumlo options handling
    ok 1 - invalid option nonzero exit code
    ok 2 - invalid option prints error message
    1..2
ok 3 - vacuumlo options handling
1..3
ok
All tests successful.
Files=1, Tests=3, 0 wallclock secs ( 0.02 usr 0.01 sys + 0.11 cusr 0.07 csys = 0.21 CPU)
Result: PASS

I think for deeply nested tests and test routines defined in other modules, this helps structure the test output more like the test source code is laid out, so it makes following the tests and locating failing test code easier.
From 3f50bc5236d7793939904222a38f7e13a2cda47c Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <pe...@eisentraut.org>
Date: Thu, 24 Feb 2022 11:01:47 +0100
Subject: [PATCH v2] Readd use of TAP subtests

Since 405f32fc49609eb94fa39e7b5e7c1fe2bb2b73aa, Test::More must be new
enough to support subtests.

The present patch effectively reverts
7912f9b7dc9e2d3f6cd81892ef6aa797578e9f06.  Many more refactorings like
this are possible; this is just to get started.
---
 src/test/perl/PostgreSQL/Test/Cluster.pm | 14 +++---
 src/test/perl/PostgreSQL/Test/Utils.pm   | 62 +++++++++++++-----------
 2 files changed, 43 insertions(+), 33 deletions(-)

diff --git a/src/test/perl/PostgreSQL/Test/Cluster.pm 
b/src/test/perl/PostgreSQL/Test/Cluster.pm
index be05845248..722bdf8a36 100644
--- a/src/test/perl/PostgreSQL/Test/Cluster.pm
+++ b/src/test/perl/PostgreSQL/Test/Cluster.pm
@@ -2473,14 +2473,16 @@ sub issues_sql_like
 
        my ($self, $cmd, $expected_sql, $test_name) = @_;
 
-       local %ENV = $self->_get_env();
+       subtest $test_name => sub {
+               local %ENV = $self->_get_env();
 
-       my $log_location = -s $self->logfile;
+               my $log_location = -s $self->logfile;
 
-       my $result = PostgreSQL::Test::Utils::run_log($cmd);
-       ok($result, "@$cmd exit code 0");
-       my $log = PostgreSQL::Test::Utils::slurp_file($self->logfile, 
$log_location);
-       like($log, $expected_sql, "$test_name: SQL found in server log");
+               my $result = PostgreSQL::Test::Utils::run_log($cmd);
+               ok($result, "@$cmd exit code 0");
+               my $log = PostgreSQL::Test::Utils::slurp_file($self->logfile, 
$log_location);
+               like($log, $expected_sql, "SQL found in server log");
+       };
        return;
 }
 
diff --git a/src/test/perl/PostgreSQL/Test/Utils.pm 
b/src/test/perl/PostgreSQL/Test/Utils.pm
index 46cd746796..5869f060ee 100644
--- a/src/test/perl/PostgreSQL/Test/Utils.pm
+++ b/src/test/perl/PostgreSQL/Test/Utils.pm
@@ -792,13 +792,15 @@ sub program_help_ok
 {
        local $Test::Builder::Level = $Test::Builder::Level + 1;
        my ($cmd) = @_;
-       my ($stdout, $stderr);
-       print("# Running: $cmd --help\n");
-       my $result = IPC::Run::run [ $cmd, '--help' ], '>', \$stdout, '2>',
-         \$stderr;
-       ok($result, "$cmd --help exit code 0");
-       isnt($stdout, '', "$cmd --help goes to stdout");
-       is($stderr, '', "$cmd --help nothing to stderr");
+       subtest "$cmd --help" => sub {
+               my ($stdout, $stderr);
+               print("# Running: $cmd --help\n");
+               my $result = IPC::Run::run [ $cmd, '--help' ], '>', \$stdout, 
'2>',
+                 \$stderr;
+               ok($result, "exit code 0");
+               isnt($stdout, '', "goes to stdout");
+               is($stderr, '', "nothing to stderr");
+       };
        return;
 }
 
@@ -814,13 +816,15 @@ sub program_version_ok
 {
        local $Test::Builder::Level = $Test::Builder::Level + 1;
        my ($cmd) = @_;
-       my ($stdout, $stderr);
-       print("# Running: $cmd --version\n");
-       my $result = IPC::Run::run [ $cmd, '--version' ], '>', \$stdout, '2>',
-         \$stderr;
-       ok($result, "$cmd --version exit code 0");
-       isnt($stdout, '', "$cmd --version goes to stdout");
-       is($stderr, '', "$cmd --version nothing to stderr");
+       subtest "$cmd --version" => sub {
+               my ($stdout, $stderr);
+               print("# Running: $cmd --version\n");
+               my $result = IPC::Run::run [ $cmd, '--version' ], '>', 
\$stdout, '2>',
+                 \$stderr;
+               ok($result, "exit code 0");
+               isnt($stdout, '', "goes to stdout");
+               is($stderr, '', "nothing to stderr");
+       };
        return;
 }
 
@@ -837,13 +841,15 @@ sub program_options_handling_ok
 {
        local $Test::Builder::Level = $Test::Builder::Level + 1;
        my ($cmd) = @_;
-       my ($stdout, $stderr);
-       print("# Running: $cmd --not-a-valid-option\n");
-       my $result = IPC::Run::run [ $cmd, '--not-a-valid-option' ], '>',
-         \$stdout,
-         '2>', \$stderr;
-       ok(!$result, "$cmd with invalid option nonzero exit code");
-       isnt($stderr, '', "$cmd with invalid option prints error message");
+       subtest "$cmd options handling" => sub {
+               my ($stdout, $stderr);
+               print("# Running: $cmd --not-a-valid-option\n");
+               my $result = IPC::Run::run [ $cmd, '--not-a-valid-option' ], 
'>',
+                 \$stdout,
+                 '2>', \$stderr;
+               ok(!$result, "invalid option nonzero exit code");
+               isnt($stderr, '', "invalid option prints error message");
+       };
        return;
 }
 
@@ -860,12 +866,14 @@ sub command_like
 {
        local $Test::Builder::Level = $Test::Builder::Level + 1;
        my ($cmd, $expected_stdout, $test_name) = @_;
-       my ($stdout, $stderr);
-       print("# Running: " . join(" ", @{$cmd}) . "\n");
-       my $result = IPC::Run::run $cmd, '>', \$stdout, '2>', \$stderr;
-       ok($result, "$test_name: exit code 0");
-       is($stderr, '', "$test_name: no stderr");
-       like($stdout, $expected_stdout, "$test_name: matches");
+       subtest $test_name => sub {
+               my ($stdout, $stderr);
+               print("# Running: " . join(" ", @{$cmd}) . "\n");
+               my $result = IPC::Run::run $cmd, '>', \$stdout, '2>', \$stderr;
+               ok($result, "exit code 0");
+               is($stderr, '', "no stderr");
+               like($stdout, $expected_stdout, "stdout matches");
+       };
        return;
 }
 
-- 
2.35.1

Reply via email to