5 RackUnit Internals and Extension API
This section describes RackUnit’s facilities for customizing the behavior of checks and tests and for creating new kinds of test runners.
5.1 Customizing Check Evaluation
The semantics of checks are determined by the parameters current-check-around and current-check-handler. Other testing form such as test-begin and test-suite change the value of these parameters.
parameter
(current-check-handler) → (-> any/c any)
(current-check-handler handler) → void? handler : (-> any/c any)
parameter
(current-check-around) → (-> (-> any) any)
(current-check-around check) → void? check : (-> (-> any) any)
5.2 Customizing Test Evaluation
Just like with checks, there are several parameters that control the semantics of compound testing forms.
parameter
(current-test-name) → (or/c string? false/c)
(current-test-name name) → void? name : (or/c string? false/c)
parameter
(current-test-case-around) → (-> (-> any) any)
(current-test-case-around handler) → void? handler : (-> (-> any) any)
procedure
(test-suite-test-case-around thunk) → any
thunk : (-> any)
procedure
(test-suite-check-around thunk) → any/c
thunk : (-> any/c)
5.3 Programmatically Running Tests and Inspecting Results
RackUnit provides an API for running tests, from which custom UIs can be created.
5.3.1 Result Types
struct
(struct exn:test exn:fail () #:extra-constructor-name make-exn:test)
struct
(struct exn:test:check exn:test (stack) #:extra-constructor-name make-exn:test:check) stack : (listof check-info)
struct
(struct test-result (test-case-name) #:extra-constructor-name make-test-result) test-case-name : (or/c string #f)
struct
(struct test-failure test-result (result) #:extra-constructor-name make-test-failure) result : any
struct
(struct test-error test-result (result) #:extra-constructor-name make-test-error) result : exn
struct
(struct test-success test-result (result) #:extra-constructor-name make-test-success) result : any
5.3.2 Functions to Run Tests
procedure
(run-test-case name action) → test-result
name : (or/c string #f) action : (-> any)
procedure
(run-test test)
→ (flat-murec-contract ([R (listof (or/c test-result? R))]) R) test : (or/c test-case? test-suite?)
Example:
(run-test (test-suite "Dummy" (test-case "Dummy" (check-equal? 1 2))))
procedure
(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.
Examples:
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)))
procedure
(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).
Example:
Here’s the implementation of fold-test-results in terms of foldts-test-suite:
(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.