unittest — Testing user code

This module provides functions to facilitate testing of user code. It provides an interface for writing unit tests and printing out the results. (The printouts are not “pretty”; they are designed to be interpreted by a wrapping script such as the Mars Test Framework.)

The test framework communicates with a wrapper by printing messages to standard output. Such messages are single lines beginning with “:test:”.

A “test case” is a single unit, which can be as big or small as the caller wishes. Zero or more asserts can be made within a single case. The case fails if any one of the asserts fails. The reason for this two-tiered system is so you can try a whole bunch of cascading asserts in a single case, and not have a mountain of failures if one fails.

Test cases must be explicitly begun and ended, in order to make sure rogue assert statements in between test cases are reported by the wrapper (rather than simply being believed to be in the most recent test case).

An “assert” is a test of a single condition. Each assert has a message, which describes what might go wrong. The message of the first failing assert will be printed if a test case fails.

Note once again that the output of this module is not meant to be viewed by a human, but processed by a wrapper and displayed in a nicer format.

begin_case(name :: Array(Int)) :: Int

Begin a new test case. The name of the test case is for output purposes only, and does not have to be unique. It will be printed if the case fails.


name should not contain newlines (this restriction may be relaxed).

begin_case_pending(name :: Array(Int)) :: Int

Same as begin_case, but flag the case as expected to fail (as a temporary measure, so that you may check in code which is broken but which you intend to fix).

Will report an explicit “pass” if none of the asserts fail. Will not report anything if at least one assert fails.

end_case(name :: Array(Int)) :: Int

End the current test case. Must use same name as most recent begin.

assert(value :: Int, message :: Array(Int)) :: Int

Checks that value is nonzero. If value is nonzero, returns 1. Otherwise, prints an assert error with message and returns 0.

Describe in message what went wrong if the case fails. For example, assert_(eq(4, 9), "4 != 9").


message should not contain newlines (this restriction may be relaxed).

assert_eq(x :: a, y :: a, message :: Array(Int)) :: Int

Checks that x equals y, according to eq. If x equals y, returns 1. Otherwise, prints an assert error with message and returns 0.

This is a convenience function, equivalent to assert(eq(x, y), message).

Previous topic

impure — Impure functions

Next topic

urllib — Construction and processing of URLs

This Page