Search code examples
racketrackunit

How to get rackunit to display stack trace?


I use rackunit like this in my code

(run-tests (test-suite "suite-name"
  (test-case "case name"
     ...
   ))

I'm trying to practice TDD. When an assertion fails, I'm able to see which assertion failed. But in case of an error, unfortunately rackunit just shows me the error encountered and the test case name that failed.

If I execute the code normally, I see stack trace with line numbers. How can I make rackunit show this information?


Solution

  • Use errortrace.

    As an example, the following program

    #lang racket/base
    
    (require rackunit
             rackunit/text-ui)
    
    (define suite
      (test-suite "suite-name"
        (test-case "case name"
          (check-equal? (1) 3))))
    
    (run-tests suite)
    

    produces:

    --------------------
    suite-name > case name
    ERROR
    
    application: not a procedure;
     expected a procedure that can be applied to arguments
      given: 1
      arguments...: [none]
    --------------------
    0 success(es) 0 failure(s) 1 error(s) 1 test(s) run
    1
    

    when you run with racket test.rkt.

    But if we run it with racket -l errortrace -t test.rkt, then it produces:

    --------------------
    suite-name > case name
    application: not a procedure;
     expected a procedure that can be applied to arguments
      given: 1
      arguments...: [none]
      errortrace...:
       /Users/sorawee/racket/test.rkt:9:20: (1)
       /Applications/Racket v7.7/share/pkgs/rackunit-lib/rackunit/private/check.rkt:115:13: ((let-values (((...it/private/check.rkt:115:14) check-impl) ((temp3) (idY31 lifted/19 (....))) ((temp4) (quote (....)))) ((checked-procedure-check-and-extract struct:keyword-procedure ...it/private/check.rkt:115:14 keyword-procedure-extract (....) ....)...
       /Applications/Racket v7.7/share/pkgs/rackunit-lib/rackunit/private/test-case.rkt:58:2: (let-values (((...rivate/test-case.rkt:58:2) run-test-case5) ((temp1) (lambda () (void) (....))) ((temp2) (lifted/20 (quote ....)))) (if (variable-reference-constant? (#%variable-reference run-test-case5)) (run-test-case temp2 temp1) ((checked-procedure...
       /Users/sorawee/racket/test.rkt:11:0: (run-tests suite)
    
    application: not a procedure;
     expected a procedure that can be applied to arguments
      given: 1
      arguments...: [none]
    --------------------
    0 success(es) 0 failure(s) 1 error(s) 1 test(s) run
    1