For Gauche 0.9.10

Next: , Previous: , Up: Library modules - SRFIs   [Contents][Index]

11.18 srfi-78 - Lightweight testing

Module: srfi-78

This srfi defines check and check-ec macro, along with a few helper procedures, to write tests. Especially, check-ec uses the same comprehension style as srfi-42 to run test expressions with various combinations of input easily.

We implemented this module to work with gauche.test (see Unit testing). Specifically, if the check macros are invoked while gauche.test is active (that is, between test-start and test-end), the check macros are simply a wrapper of Gauche’s test macro; the results are tracked and reported by gauche.test.

If this module is used without gauche.test, however, it works as specified by the srfi, including reporting. So, a tests using this srfi can be run in both ways—if it is loaded by itself, it runs as vanilla srfi-78, and if it is included in a test file that uses gacuhe.test, it runs as a part of gauche.test.

Macro: check expr => expected
Macro: check expr (=> equal) expected

[SRFI-78] {srfi-78} Evaluate expected and equal, then evaluate expr and compare the results of expected and expr using equal. In the first form, equal? is used as equal.

Macro: check-ec qualifier … expr => expected (argument …)
Macro: check-ec qualifier … expr (=> equal) expected (argument …)
Macro: check-ec qualifier … expr => expected
Macro: check-ec qualifier … expr (=> equal) expected

[SRFI-78] {srfi-78} Evaluates expr, equal and expected repeatedly in the environment where the qualifiers bind variables, and each time the results of expr and equal are compared with equal. If the results don’t agree, the failure is recorded with the given argument …, which can be useful to diagnose which combination of bindings the test failed on. Single failure stops the iteration. If the results of expr and expected agrees on all the iterations, the entire check-ec is regarded as success.

The way qualifiers work is the same as srfi-42. See Eager comprehensions, for the details.

The following example tests Fermat numbers (2^2^n + 1) are primes. It fails since F_5 is a composite.

(check-ec (: n 6)
          (:let fn (+ (expt 2 (expt 2 n)) 1))
          (bpsw-prime? fn)
          => #t (n))
 prints Checking (bpsw-prime? fn), expecting #t => ERROR: got #f, with n: 5

The entire check-ec form is treated as one check for the sake of reporting.

Function: check-report

[SRFI-78] {srfi-78} If this module is used stand-alone, prints the summary of test results to the current output port.

If this module is running inside gauche.test, this does nothing. Reporting is done by gauche.test.

Function: check-set-mode! mode

[SRFI-78] {srfi-78} Sets how the test progress and result will be reported. This only has an effect when check is run without gauche.test.

Valid mode is one of the following symbols:


Do not report the result at all, even from (check-report).


Report the summary of the results when (chec-report) is called.


In addition to summary, report any failed checks as they occur.


In addition to summary, report each result of check as they occur.

The default is report.

Function: check-reset!

[SRFI-78] {srfi-78} Clears the internal state to keep track of number of performed checks and failed checks, etc. This doesn’t affect gauche.test bookkeeping.

Function: check-passed? expected-total-count

[SRFI-78] {srfi-78} This can be used to programatically query whether the expected number of tests are passed since the last check-reset! or the beginning. The expected-total-count must be a number, and it returns #t iff the internal count of passed test matches it and there is no failed checks.

This works even if checks are run with gauche.test. However, only the tests using check and check-ec are counted.

Next: , Previous: , Up: Library modules - SRFIs   [Contents][Index]