Subject: | Optional stack traces |
Patch attached allows stack traces for failing test. For convenience
they can be switched on with
use Test::More tests => 1, 'stack_trace';
I also have this in svn but have no idea what to do with it :) Hence the
patch.
Subject: | stack_trace.diff |
Index: lib/Test/Builder.pm
===================================================================
--- lib/Test/Builder.pm (revision 3760)
+++ lib/Test/Builder.pm (working copy)
@@ -195,6 +195,8 @@
$self->{No_Header} = 0;
$self->{No_Ending} = 0;
+ $self->{Stack_Trace} = 0;
+
$self->_dup_stdhandles unless $^C;
return undef;
@@ -456,6 +458,13 @@
else {
$self->diag(qq[ $msg test in $file at line $line.\n]);
}
+ if ($self->stack_trace) {
+ my $frame = 1;
+ while (my ($pack, $file, $line) = $self->caller($frame)) {
+ $self->diag(qq[ Called from $file at line $line.\n]);
+ $frame++;
+ }
+ }
}
return $test ? 1 : 0;
@@ -1067,9 +1076,15 @@
If set to true, no "1..N" header will be printed.
+=item B<stack_trace>
+
+ $Test->stack_trace($stack_trace);
+
+If set to true, test failure diagnostics will inclues a stack trace.
+
=cut
-foreach my $attribute (qw(No_Header No_Ending No_Diag)) {
+foreach my $attribute (qw(No_Header No_Ending No_Diag Stack_Trace )) {
my $method = lc $attribute;
my $code = sub {
Index: lib/Test/More.pm
===================================================================
--- lib/Test/More.pm (revision 3760)
+++ lib/Test/More.pm (working copy)
@@ -177,6 +177,9 @@
if( defined $item and $item eq 'no_diag' ) {
$class->builder->no_diag(1);
}
+ elsif( defined $item and $item eq 'stack_trace' ) {
+ $class->builder->stack_trace(1);
+ }
else {
push @other, $item;
}
@@ -914,6 +917,13 @@
You might remember C<ok() or diag()> with the mnemonic C<open() or
die()>.
+Diagnostics on failed tests show the location of the failing
+test. It's possible to get a longer stack trace instead of just a
+single line by passing the "stack_trace" option to Test::More.
+C<use Test::More tests =E<gt> 1, 'stack_trace'>. This is useful the failing
+test is in a subroutine that is called from several places in your
+test script.
+
B<NOTE> The exact formatting of the diagnostic output is still
changing, but it is guaranteed that whatever you throw at it it won't
interfere with the test.