This section describes RackUnit’s facilities for customizing the behavior of checks and tests and for creating new kinds of test runners.
(current-check-handler handler) → void? handler : (-> any/c any)
Just like with checks, there are several parameters that control the semantics of compound testing forms.
(current-test-name name) → void? name : (or/c string? false/c)
RackUnit provides an API for running tests, from which custom UIs can be created.
(struct exn:test:check exn:test (stack) #:extra-constructor-name make-exn:test:check) stack : (listof check-info)
(struct test-result (test-case-name) #:extra-constructor-name make-test-result) test-case-name : (or/c string #f)
(struct test-failure test-result (result) #:extra-constructor-name make-test-failure) result : any
(struct test-error test-result (result) #:extra-constructor-name make-test-error) result : exn
(struct test-success test-result (result) #:extra-constructor-name make-test-success) result : any
→ (flat-murec-contract ([R (listof (or/c test-result? R))]) R) test : (or/c test-case? test-suite?)
(run-test (test-suite "Dummy" (test-case "Dummy" (check-equal? 1 2))))
(fold-test-results result-fn seed test #:run run #:fdown fdown #:fup fup) → 'a result-fn : ('b 'c ... 'a . -> . 'a) seed : 'a test : (or/c test-case? test-suite?) run : (string (() -> any) . -> . 'b 'c ...) fdown : (string 'a . -> . 'a) fup : (string 'a . -> . 'a)
This function is useful for writing custom folds (and hence UIs) over test results without you having to take care of all the expected setup and teardown. For example, fold-test-results will run test suite before and after actions for you. However it is still flexible enough, via its keyword arguments, to do almost anything that foldts-test-suite can. Hence it should be used in preference to foldts-test-suite.
The result-fn argument is a function from the results of run (defaults to a test-result) and the seed to a new seed.
The seed argument is any value.
The test argument is a test case or test suite.
The run argument is a function from a test case name (string) and action (thunk) to any values. The values produced by run are fed into the result-fn.
The fdown argument is a function from a test suite name (string) and the seed, to a new seed.
The fup argument is a function from a test suite name (string) and the seed, to a new seed.
The following code counts the number of successes:
(define (count-successes test) (fold-test-results (lambda (result seed) (if (test-success? result) (add1 seed) seed)) 0 test))
The following code returns the symbol 'burp instead of running test cases. Note how the result-fn receives the value of run.
(define (burp test) (fold-test-results (lambda (result seed) (cons result seed)) null test #:run (lambda (name action) 'burp)))
(foldts-test-suite fdown fup fhere seed test) → 'a
fdown : (test-suite string thunk thunk 'a -> 'a) fup : (test-suite string thunk thunk 'a 'a -> 'a) fhere : (test-case string thunk 'a -> 'a) seed : 'a test : (or/c test-case? test-suite?)
The fdown argument is a function of test suite, test suite name, before action, after action, and the seed. It is run when a test suite is encountered on the way down the tree (pre-order).
The fup argument is a function of test suite, test suite name, before action, after action, the seed at the current level, and the seed returned by the children. It is run on the way up the tree (post-order).
The fhere argument is a function of the test case, test case name, the test case action, and the seed. (Note that this might change in the near future to just the test case. This change would be to allow fhere to discriminate subtypes of test-case, which in turn would allow test cases that are, for example, ignored).
(define (fold-test-results suite-fn case-fn seed test) (foldts-test-suite (lambda (suite name before after seed) (before) (suite-fn name seed)) (lambda (suite name before after seed kid-seed) (after) kid-seed) (lambda (case name action seed) (case-fn (run-test-case name action) seed)) seed test))
If you’re used to folds you’ll probably be a bit surprised that the functions you pass to foldts-test-suite receive both the structure they operate on, and the contents of that structure. This is indeed unusual. It is done to allow subtypes of test-case and test-suite to be run in customised ways. For example, you might define subtypes of test case that are ignored (not run), or have their execution time recorded, and so on. To do so the functions that run the test cases need to know what type the test case has, and hence is is necessary to provide this information.
If you’ve made it this far you truly are a master RackUnit hacker. As a bonus prize we’ll just mention that the code in hash-monad.rkt and monad.rkt might be of interest for constructing user interfaces. The API is still in flux, so isn’t documented here. However, do look at the implementation of run-tests for examples of use.