SchemeUnit: Using SchemeUnit

Table of Contents | Organising Your Tests

To create a single test case use the make-test-case macro found in the test module:

        (require (lib "test.ss" "schemeunit"))

        (make-test-case "My test"
                   (assert-equal? 42 (meaning-of-life)))
    

The assert functions should be used to check for success or failure of your test cases. The available test functions are:

        (assert fn expected actual)     -> (union #t exn:test:assertion)
        (assert-eq? expected actual)    -> (union #t exn:test:assertion)
        (assert-eqv? expected actual)   -> (union #t exn:test:assertion)
        (assert-equal? expected actual) -> (union #t exn:test:assertion)
        (assert-exn predicate thunk)    -> (union #t exn:test:assertion)
        (assert-pred predicate value)   -> (union #t exn:test:assertion)
        (assert-true value)             -> (union #t exn:test:assertion)
        (assert-false value)            -> (union #t exn:test:assertion)
    

To collect a bunch of test cases into a single testable collection, use a test-suite:

        (require (lib "test.ss" "schemeunit")

        (define arithmetic-tests
          (make-test-suite
            "Arithmetic tests"
            (make-test-case "Multiply by zero" 
                       (assert = (* 1234 0) 0))
            (make-test-case "Add zero" 
	                   (assert = (+ 1234 0) 1234))
            (make-test-case "Multiply by one"
	                   (assert = (* 123.0 1) 123))))
    

To run your tests use the test/text-ui function in the text-ui module:

        (require (lib "test.ss" "schemeunit")
                 (lib "text-ui.ss" "schemeunit"))

        (test/text-ui
          (make-test-suite
            "Arithmetic tests"
            (make-test-case "Multiply by zero" 
                       (assert = (* 1234 0) 0))
            (make-test-case "Add zero" 
	                   (assert = (+ 1234 0) 1234))
            (make-test-case "Multiply by one"
	                   (assert = (* 123.0 1) 123))))
    

Tests the require some state to run (e.g. the presence of certain files) can make use of setup and teardown functions that are run before and after the test respectively. The setup and teardown functions are specified as optional arguments to the make-test-case macro:

       (make-test-case name test-expr [teardown]) -> Test-Case
       (make-test-case name test-expr [setup] [teardown]) -> Test-Case
    

Noel Welsh
Last modified: Tue May 21 00:20:28 BST 2002