Since today morning, Scheme Power Tools includes out-of-the-box support for unit tests. This article serves as a short introduction to the testing framework. If you are only looking for the documentation, you can find it at http://mpacula.com/scheme-tools/docs/unit-testing.html.

Writing Tests

The testing framework was designed to be simple and hassle-free. It exposes two primary constructs: unit-test and test-suite. The unit-test macro takes a name and a sequence of expressions, and constructs a new unit test. For example:

(unit-test "example test"
   (assert-equal 5 (+ 2 3))
   (assert-equal-fp 0 1e-14 1e-13)
   (assert (even? 3) "Expected the number to be even")

Multiple unit-test‘s can be combined using a test-suite. For instance:

(test-suite "Example Suite"
  (unit-test "example test"
   (assert-equal 5 (+ 2 3))
   (assert-equal-fp 0 1e-14 1e-13)
   (assert (even? 3) "Expected 3 to be even"))

  (test-suite "My Embedded Suite"
   (unit-test "test1"
             (assert-equal 4 (* 2 2))
             (assert-equal 4 (+ 2 2)))

   (unit-test "test2"
              (assert-equal 10 (* 3 3))
              (assert-equal 6 (+ 3 3)))))

The code above defines a test suite “Example Suite“, with one unit test called “example test“, and an embedded test suite “My Embedded Suite“.

Running Unit Tests

Tests are run by calling enable-unit-tests first, and then invoking run-suite on the test suite of interest. The need to explicitly call enable-unit-tests allows tests to be embedded directly in the source files whose functionality they are testing, without them being executed every time the files are loaded.

When running a test, all errors are automatically handled and printed on the screen. Summaries with the number of passed and failed tests are also printed on a per-suite basis. For instance, when running the “Example Suite” defined earlier, the output should look like the following:

* Running "Example Suite"... 
    * Running "example test"... FAILED:
      Expected the number to be even
    * Running "My Embedded Suite"... 
        * Running "test1"... OK
        * Running "test2"... FAILED:
          Assertion failed. Expected:  10  but got:  9
        -------------------------------------
        SUITE SUMMARY (My Embedded Suite)
        1/2 tests passed.
        There was 1 failure.
        -------------------------------------

    -------------------------------------
    SUITE SUMMARY (Example Suite)
    1/3 tests passed.
    There were 2 failures.
    -------------------------------------

Asserts

The framework supports the following assert functions:

  • assert: the most generic of all asserts. Takes a value and an optional error message, and fails if the value is #f.
  • assert-not: the inverse of assert – fails if the supplied value is not #f.
  • assert-equal: takes an expected and actual value, and verifies that the two are equal (using equal?).
  • assert-equal-fp: like assert-equal, but for floating point numbers. Takes an optional third parameter precision (default value 1e-9), which specifies the maximum amount by which the two numbers can differ.

As always, your feedback is greatly appreciated. Happy coding!

Comment now!
















Trackbacks