diff --git a/lib/Test/Class.pm b/lib/Test/Class.pm index 7b6c899..f83a0c6 100644 --- a/lib/Test/Class.pm +++ b/lib/Test/Class.pm @@ -37,6 +37,7 @@ sub builder { $Builder }; my $Tests = {}; my @Filters = (); +my %_Test_Failure = (); my %_Test; # inside-out object field indexed on $self @@ -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; }; @@ -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)) { @@ -1307,6 +1323,15 @@ If the environment variable C is set C will display the # My::Test::Class->another_test ok 2 - bar +If there are any errors in your tests, and C is set, C 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, C 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. For example, you can run all the tests in test classes A, B and C, plus one additional normal test by doing: diff --git a/t/test_verbose_failures.t b/t/test_verbose_failures.t new file mode 100644 index 0000000..73dad27 --- /dev/null +++ b/t/test_verbose_failures.t @@ -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");