Menu

#23 Show failing line number

open
nobody
None
5
2008-10-04
2008-10-04
chrisvogt
No

In case of a failed test, TextReporter shows the imploded "breadcrump" for tests, consisting of the names of test function, class and file where the test failed. It does however not print the actual line number, which would help to pinpoint the failure even more quickly. This patch adds line number printing to paintException. The same technique could be used in paintError and paintFail.

The patch is based on 1.0.1.

Index: simpletest/reporter.php

--- simpletest/reporter.php (revision 1811)
+++ simpletest/reporter.php (working copy)
@@ -281,9 +281,15 @@
$breadcrumb = $this->getTestList();
array_shift($breadcrumb);
print "\tin " . implode("\n\tin ", array_reverse($breadcrumb));
+ foreach( $exception->getTrace() as $frame ){
+ if( $frame['file'] === $breadcrumb[0] ){
+ echo ":".$frame['line'];
+ break;
+ }
+ }
print "\n";
}

Discussion

  • Nobody/Anonymous

    Also, there was a bug in the patch. This makes more sense:

    Index: simpletest/reporter.php

    --- simpletest/reporter.php (revision 1811)
    +++ simpletest/reporter.php (working copy)
    @@ -281,9 +281,15 @@
    $breadcrumb = $this->getTestList();
    array_shift($breadcrumb);
    print "\tin " . implode("\n\tin ", array_reverse($breadcrumb));
    + foreach( array_reverse($exception->getTrace()) as $frame ){
    + if( $frame['file'] === $breadcrumb[0] ){
    + echo ":".$frame['line'];
    + break;
    + }
    + }
    print "\n";
    }

     
  • Nobody/Anonymous

    The same works for paintFail, if you replace $exception->getTrace() by debug_backtrace() .

    To apply the same for paintError it would be required to somehow get the stack trace from where the exception occurred. I suggest to store it in a variable ($trace = debug_backtrace()); in SimpleTestErrorHandler and pass as an argument along with $severity, $message, $filename, $line, $trace until it finally reaches paintError.

    To motivate this patch again: It is VERY helpful to know the line that actually failed. Having the stack available in the paint* method can also allows to print more information that can help to understand how an error or exception happened.

     

Log in to post a comment.