Unit Testing

Defines a set of functions to write test scripts.

(mosh test) uses (srfi :64 testing) as backend.

Example

(import (rnrs)
        (mosh test)
  (test-begin "number predicate")
  (test-false (number? 'a))
  (test-end))
Summary
Unit TestingDefines a set of functions to write test scripts.
(mosh test)Unit Testing library
Functions
test-beginA test-begin enters a new test group.
test-endA test-end leaves the current test group.
test-groupThis is usually equivalent to executing the decl-or-exprs within the named test group.
test-assertThis evaluates the expression.
test-trueRun the test and check the result is not #f.
test-falseRun the test and check the result is #f.
test-eqvThis is equivalent to
test-eqThis is equivalent to
test-equalThis is equivalent to
test-approximateThis is equivalent to (except that each argument is only evaluated once)
test-read-eval-stringThis function parses string (using read) and evaluates the result.
test-errorEvaluating test-expr is expected to signal an error.The kind of error is indicated by error-type.

(mosh test)

Unit Testing library

Summary
Functions
test-beginA test-begin enters a new test group.
test-endA test-end leaves the current test group.
test-groupThis is usually equivalent to executing the decl-or-exprs within the named test group.
test-assertThis evaluates the expression.
test-trueRun the test and check the result is not #f.
test-falseRun the test and check the result is #f.
test-eqvThis is equivalent to
test-eqThis is equivalent to
test-equalThis is equivalent to
test-approximateThis is equivalent to (except that each argument is only evaluated once)
test-read-eval-stringThis function parses string (using read) and evaluates the result.
test-errorEvaluating test-expr is expected to signal an error.The kind of error is indicated by error-type.

Functions

test-begin

A test-begin enters a new test group.  The suite-name becomes the current test group name, and is added to the end of the test group path.

Portable test suites should use a sting literal for suite-name; the effect of expressions or other kinds of literals is unspecified.

Prototype

(test-begin suite-name [count])

Parameters

suite-nametest suite name.
countThe optional count must match the number of test-cases executed by this group.  (Nested test groups count as a single test case for this count.)  This extra test may be useful to catch cases where a test doesn’t get executed because of some unexpected error.

Returns

unspecified.

test-end

A test-end leaves the current test group.  An error is reported if the suite-name does not match the current test group name.

Additionally, if the matching test-begin installed a new test-runner, then the test-end will de-install it, after reporting the accumulated test results in an implementation-defined manner.

Prototype

(test-end [suite-name])

Parameters

suite-nametest suite name.

Returns

unspecified.

test-group

This is usually equivalent to executing the decl-or-exprs within the named test group.

However, the entire group is skipped if it matched an active test-skip (see later).

Also, the test-end is executed in case of an exception.

Equivalent to

(if (not (test-to-skip% suite-name))
  (dynamic-wind
    (lambda () (test-begin suite-name))
    (lambda () decl-or-expr ...)
    (lambda () (test-end suite-name))))

Prototype

(test-group suite-name decl-or-expr ...)

Parameters

suite-nametest suite name.
decl-or-exprdecl-or-expr

Returns

unspecified.

test-assert

This evaluates the expression.

The test passes if the result is true; if the result is false, a test failure is reported.  The test also fails if an exception is raised.

The test-name is a string that names the test case.

(Though the test-name is a string literal in the examples, it is an expression.  It is evaluated only once.)

It is used when reporting errors, and also when skipping tests, as described below.  It is an error to invoke test-assert if there is no current test runner.

Prototype

(test-assert [test-name] expression)

Parameters

test-nametest name.
expressionexpression to evaluate.

Returns

unspecified.

test-true

Run the test and check the result is not #f.

Prototype

(test-true [test-name] expression)

Parameters

test-nametest name.
expressionexpression to evaluate.

Returns

unspecified.

test-false

Run the test and check the result is #f.

Prototype

(test-false [test-name] expression)

Parameters

test-nametest name.
expressionexpression to evaluate.

Returns

unspecified.

test-eqv

This is equivalent to

(test-assert [test-name] (eqv? expected test-expr))

Run the test and check the result is #f.

Prototype

(test-eqv [test-name] expected test-expr)

Parameters

test-nametest name.
expectedexpected values
test-exprtest-expr to evaluate.

Returns

unspecified.

test-eq

This is equivalent to

(test-assert [test-name] (eq? expected test-expr))

Run the test and check the result is #f.

Prototype

(test-eq [test-name] expected test-expr)

Parameters

test-nametest name.
expectedexpected values
test-exprtest-expr to evaluate.

Returns

unspecified.

test-equal

This is equivalent to

(test-assert [test-name] (equal? expected test-expr))

Run the test and check the result is #f.

Prototype

(test-equal [test-name] expected test-expr)

Parameters

test-nametest name.
expectedexpected values
test-exprtest-expr to evaluate.

Returns

unspecified.

test-approximate

This is equivalent to (except that each argument is only evaluated once)

(test-assert [test-name]
  (and (>= test-expr (- expected error))
   (<= test-expr (+ expected error))))

Run the test and check the result is #f.

Prototype

(test-approximate [test-name] expected test-expr error)

Parameters

test-nametest name.
expectedexpected values
test-exprtest-expr to evaluate.
errorallowed error.

Returns

unspecified.

test-read-eval-string

This function parses string (using read) and evaluates the result.

The result of evaluation is returned from test-read-eval-string.  An error is signalled if there are unread characters after the read is done.

Prototype

(test-read-eval-string string)

Parameters

stringstring to evaluate.

Returns

evalated result.

test-error

Evaluating test-expr is expected to signal an error.The kind of error is indicated by error-type.

If the error-type is left out, or it is #t, it means “some kind of unspecified error should be signaled”.

Prototype

(test-error [[test-name] error-type] test-expr)

Parameters

test-nametest name.
error-typeerror-type
test-exprtest-expr to evaluate.

Returns

unspecified.

Close