Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions lib/Test/Class.pm
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ sub builder { $Builder };

my $Tests = {};
my @Filters = ();
my %_Test_Failure = ();


my %_Test; # inside-out object field indexed on $self
Expand Down Expand Up @@ -275,6 +276,7 @@ sub _run_method {
unless ( $is_ok ) {
my $class = ref $self;
$Builder->diag( " (in $class->$method)" );
$_Test_Failure{$class}{$method}++;
};
return $is_ok;
};
Expand Down Expand Up @@ -397,9 +399,23 @@ sub runtests {

}
}
if (!$all_passed && $ENV{TEST_VERBOSE}) {
$Builder->diag(_test_failures())
}
return($all_passed);
};

sub _test_failures {
my $message = "Test failures were as follows:\n";
for my $class (sort keys %_Test_Failure) {
$message .= " $class:\n";
for my $method (sort keys %{$_Test_Failure{$class}}) {
$message .= " ->$method\n";
}
}
return $message;
}

sub _find_calling_test_class {
my $level = 0;
while (my $class = caller(++$level)) {
Expand Down Expand Up @@ -1307,6 +1323,15 @@ If the environment variable C<TEST_VERBOSE> is set C<runtests> will display the
# My::Test::Class->another_test
ok 2 - bar

If there are any errors in your tests, and C<TEST_VERBOSE> is set, C<runtests> will display a summary of the failing tests after all the tests have been run:

# Test failures were as follows:
# My::Test::Class
# ->my_test
# ->another_test
# My::Other::Test::Class
# ->this_test

Just like L<expected_tests()|/"expected_tests">, C<runtests> can take an optional list of test object/classes and integers. All of the test object/classes are run. Any integers are added to the total number of tests shown in the test header output by C<runtests>.

For example, you can run all the tests in test classes A, B and C, plus one additional normal test by doing:
Expand Down
44 changes: 44 additions & 0 deletions t/test_verbose_failures.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#! /usr/bin/perl -T

use strict;
use warnings;

package Local::Test;
use base qw(Test::Class);
use Test::More;

sub test1 : Test { pass() };
sub test2 : Test { fail() };
sub test3 : Test { pass() };
sub test4 : Test { fail() };

package main;
use Test::Builder::Tester tests => 1;

my $filename = sub { return (caller)[1] }->();

$ENV{TEST_VERBOSE} = 1;
test_diag("");
test_diag("Local::Test->test1");
test_out("ok 1 - test1");
test_diag("");
test_diag("Local::Test->test2");
test_out("not ok 2 - test2");
test_diag(" Failed test 'test2'");
test_diag(" at $filename line 11.");
test_diag(" (in Local::Test->test2)");
test_diag("");
test_diag("Local::Test->test3");
test_out("ok 3 - test3");
test_diag("");
test_diag("Local::Test->test4");
test_out("not ok 4 - test4");
test_diag(" Failed test 'test4'");
test_diag(" at $filename line 13.");
test_diag(" (in Local::Test->test4)");
test_diag("Test failures were as follows:");
test_diag(" Local::Test:");
test_diag(" ->test2");
test_diag(" ->test4");
Local::Test->runtests;
test_test("TEST_VERBOSE outputs method diagnostic and summary of failures");