SRFI-78: Lightweight testing
The documentation on this page is composed of excerpts from the SRFI document.
Abstract
A simple mechanism is defined for testing Scheme programs.
As a most primitive example, the expression
(check (+ 1 1) => 3)evaluates the expression (+ 1 1) and compares the result with the expected result 3 provided after the syntactic keyword =>. Then the outcome of this comparison is reported in human-readable form by printing a message of the form
(+ 1 1) => 2 ; *** failed *** ; expected result: 3
Moreover, the outcome of any executed check is recorded in a global state counting the number of correct and failed checks and storing the first failed check. At the end of a file, or at any other point, the user can print a summary.
In addition to the simple test above, it is also possible to execute a parametric sequence of checks. Syntactically, this takes the form of an eager comprehension in the sense of SRFI 42. For example,
(check-ec (:range e 100)
(:let x (expt 2.0 e))
(= (+ x 1) x) => #f (e x))This statement runs the variable e through {0..99} and for each binding defines x as (expt 2.0 e). Then it is checked if (+ x 1) is equal to x, and it is expected that this is not the case (i.e. expected value is #f). The trailing (e x) tells the reporting mechanism to print the values of both e and x in case of a failed check. The output could look like this:
(let ((e 53) (x 9007199254740992.0)) (= (+ x 1) x)) => #t ; *** failed *** ; expected result: #f
The specification of bindings to report, (e x) in the example, is optional but very informative.
Other features of this SRFI are:
- A way to specify a different equality predicate (default is equal?).
- Controlling the amount of reporting being printed.
- Switching off the execution and reporting of checks entirely.
- Retrieving a boolean if all checks have been executed and passed.
Specification
check
- (check <expr> => <expected>)syntax
evaluates <expr> and compares the value to the value of <expected> using the predicate <equal>, which is equal? when omitted. Then a report is printed according to the current mode setting (see below) and the outcome is recorded in a global state to be used in check-report.
The precise order of evaluation is that first <equal> and <expected> are evaluated (in unspecified order) and then <expr> is evaluated. Example: (check (+ 1 1) => 2)
check-ec
- (check-ec <qualifier>^* <expr> => <expected>)syntax
an eager comprehension for executing a parametric set of checks.
Enumerates the sequence of bindings specified by <qualifier>^*. For each binding evaluates <equal> and <expected> in unspecified order. Then evaluates <expr> and compares the value obtained to the value of <expected> using the value of <equal> as predicate, which is equal? when omitted.
The comprehension stops after the first failed check, if there is any. Then a report is printed according to the current mode setting (see below) and the outcome is recorded in a global state to be used in check-report. The entire check-ec counts as a single check.
In case the check fails <argument>^* is used for constructing an informative message with the argument values. Use <argument>^* to list the relevant free variables of <expr> (see examples) that you want to have printed.
A <qualifier> is any qualifier of an eager comprehension as specified in SRFI 42.
Examples
(check-ec (: e 100) (positive? (expt 2 e)) => #t (e)) ; fails on fixnums (check-ec (: e 100) (:let x (expt 2.0 e)) (= (+ x 1) x) => #f (x)) ; fails (check-ec (: x 10) (: y 10) (: z 10) (* x (+ y z)) => (+ (* x y) (* x z)) (x y z)) ; passes with 10^3 cases checked
check-report
- check-reportprocedure
prints a summary and the first failed check, if there is any, depending on the current mode settings.
check-set-mode!
- check-set-mode! modeprocedure
sets the current mode to mode, which must be a symbol in '(off summary report-failed report), default is 'report.
The mode symbols have the following meaning:
- off
- do not execute any of the checks
- summary
- print only summary in (check-report) and nothing else
- report-failed
- report failed checks when they happen, and in summary
- report
- report every example executed
Note that you can change the mode at any time, and that check, check-ec and check-report use the current value.
check-reset!
- check-reset!procedure
resets the global state (counters of correct/failed examples) to the state immediately after loading the module for the first time, i.e. no checks have been executed.
check-passed?
- check-passed? expected-total-countprocedure
#t if there were no failed checks and expected-total-count correct checks, #f otherwise.
Rationale: This procedure can be used in automatized tests by terminating a test program with the statement (exit (if (check-passed? <n>) 0 1)).
About this egg
Dependencies
The srfi-42 egg is required.
Author
SRFI 78 was created by Sebastian Egner.
Maintainer
Wolfgang Corcoran-Mathe
Email: wcm at sigwinch dot xyzzy minus the zy.
Please report bugs!
Repository
https://github.com/Zipheir/srfi-78
Copyright
Copyright (C) Sebastian Egner (2005-2006). All Rights Reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.