How to set up rackunit tests to test the REPL?

Skip to first unread message

Kuang-Chen Lu

Sep 8, 2021, 9:31:01 PMSep 8
to Racket Users

What are the recommended ways to create unit tests that test both run and REPL (#%top-interaction)?

Background: I created a custom language and have some unit tests. My updated language passed all unit tests. After delivery, a client ran into a bug that only happens in REPL. I could have found the bug if the REPL was also tested.



Ryan Culpepper

Sep 9, 2021, 9:20:56 AMSep 9
to Kuang-Chen Lu, Racket Users
This is one of the few (IMO) legitimate uses of `eval`.

Your test suite should create a namespace, set it up by requiring your language's module, and then eval interactions expressed as quoted S-expressions or syntax objects. Here's a basic example for testing `match`:

    #lang racket/base
    (require syntax/strip-context rackunit)

    (define test-ns (make-base-empty-namespace))
    (parameterize ((current-namespace test-ns))
      (namespace-require 'racket/base)
      (namespace-require 'racket/match))

    ;; test-eval : (U Syntax S-expr) -> Any
    (define (test-eval expr)
      (parameterize ((current-namespace test-ns))
        (eval `(#%top-interaction
                . ,(cond [(syntax? expr)
                           (strip-context expr))]
                         [else expr])))))

    (check-equal? (test-eval
                   '(match (list 1 2 3)
                      [(cons x ys) x]
                      [_ #f]))

    (void (test-eval '(define null? zero?))) ;; !!!

    (check-equal? (test-eval
                   #'(match 0
                       [(? null?) 'ok]
                       [_ 'no]))

The call to `strip-syntax` is necessary in the second test to make `null?` refer to the redefinition in the testing namespace instead of the normal binding visible to the testing module.


You received this message because you are subscribed to the Google Groups "Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To view this discussion on the web visit
Reply all
Reply to author
0 new messages