3 (in-package :it.bese.FiveAM)
7 ;;;; Once the programmer has defined what the tests are these need to
8 ;;;; be run and the expected effects should be compared with the
9 ;;;; actual effects. FiveAM provides the function RUN for this
10 ;;;; purpose, RUN executes a number of tests and collects the results
11 ;;;; of each individual check into a list which is then
12 ;;;; returned. There are three types of test results: passed, failed
13 ;;;; and skipped, these are represented by TEST-RESULT objects.
15 ;;;; Generally running a test will return normally, but there are two
16 ;;;; exceptional situations which can occur:
18 ;;;; - An exception is signaled while running the test. If the
19 ;;;; variable *debug-on-error* is T than FiveAM will enter the
20 ;;;; debugger, otherwise a test failure (of type
21 ;;;; unexpected-test-failure) is returned. When entering the
22 ;;;; debugger two restarts are made available, one simply reruns the
23 ;;;; current test and another signals a test-failure and continues
24 ;;;; with the remaining tests.
26 ;;;; - A circular dependency is detected. An error is signaled and a
27 ;;;; restart is made available which signals a test-skipped and
28 ;;;; continues with the remaining tests. This restart also sets the
29 ;;;; dependency status of the test to nil, so any tests which depend
30 ;;;; on this one (even if the dependency is not circular) will be
33 ;;;; The functions RUN!, !, !! and !!! are convenient wrappers around
36 (defparameter *debug-on-error* nil
37 "T if we should drop into a debugger on error, NIL otherwise.")
39 (defparameter *debug-on-failure* nil
40 "T if we should drop into a debugger on a failing check, NIL otherwise.")
42 (defun import-testing-symbols (package-designator)
43 (import '(5am::is 5am::is-true 5am::is-false 5am::signals 5am::finishes)
46 (defparameter *run-queue* '()
47 "List of test waiting to be run.")
49 (define-condition circular-dependency (error)
50 ((test-case :initarg :test-case))
51 (:report (lambda (cd stream)
52 (format stream "A circular dependency wes detected in ~S." (slot-value cd 'test-case))))
53 (:documentation "Condition signaled when a circular dependency
54 between test-cases has been detected."))
56 (defgeneric run-resolving-dependencies (test)
57 (:documentation "Given a dependency spec determine if the spec
58 is satisfied or not, this will generally involve running other
59 tests. If the dependency spec can be satisfied the test is alos
62 (defmethod run-resolving-dependencies ((test test-case))
63 "Return true if this test, and its dependencies, are satisfied,
67 (setf (status test) :resolving)
68 (if (or (not (depends-on test))
69 (resolve-dependencies (depends-on test)))
71 (run-test-lambda test)
73 (with-run-state (result-list)
74 (unless (eql :circular (status test))
75 (push (make-instance 'test-skipped
77 :reason "Dependencies not satisfied")
79 (setf (status test) :depends-not-satisfied)))))
82 (error 'circular-dependency :test-case test)
85 (format s "Skip the test ~S and all its dependencies." (name test)))
86 (with-run-state (result-list)
87 (push (make-instance 'test-skipped :reason "Circular dependencies" :test-case test)
89 (setf (status test) :circular))))
92 (defmethod resolve-dependencies ((depends-on symbol))
93 "A test which depends on a symbol is interpreted as `(AND
95 (run-resolving-dependencies (get-test depends-on)))
97 (defmethod resolve-dependencies ((depends-on list))
98 "Return true if the dependency spec DEPENDS-ON is satisfied,
100 (if (null depends-on)
102 (flet ((satisfies-depends-p (test)
103 (funcall test (lambda (dep)
104 (eql t (resolve-dependencies dep)))
106 (ecase (car depends-on)
107 (and (satisfies-depends-p #'every))
108 (or (satisfies-depends-p #'some))
109 (not (satisfies-depends-p #'notany))))))
111 (defun results-status (result-list)
112 "Given a list of test results (generated while running a test)
113 return true if all of the results are of type TEST-PASSED,
116 (typep res 'test-passed))
119 (defun return-result-list (test-lambda)
120 "Run the test function TEST-LAMBDA and return a list of all
121 test results generated, does not modify the special environment
122 variable RESULT-LIST."
123 (bind-run-state ((result-list '()))
124 (funcall test-lambda)
127 (defmethod run-test-lambda ((test test-case))
128 (with-run-state (result-list)
129 (bind-run-state ((current-test test))
130 (labels ((abort-test (e)
131 (add-result 'unexpected-test-failure
134 :reason (format nil "Unexpected Error: ~S." e)
137 (let ((result-list '()))
138 (declare (special result-list))
139 (handler-bind ((check-failure (lambda (e)
141 (unless *debug-on-failure*
143 (find-restart 'ignore-failure)))))
145 (unless (or *debug-on-error*
146 (typep e 'check-failure))
148 (return-from run-it result-list)))))
150 (funcall (test-lambda test))
152 :report (lambda (stream)
153 (format stream "~@<Rerun the test ~S~@:>" test))
154 (return-from run-it (run-it)))
156 :report (lambda (stream)
157 (format stream "~@<Signal an exceptional test failure and abort the test ~S.~@:>" test))
158 (abort-test (make-instance 'test-failure :test-case test
159 :reason "Failure restart."))))
161 (let ((results (run-it)))
162 (setf (status test) (results-status results)
163 result-list (nconc result-list results)))))))
165 (defgeneric %run (test-spec)
166 (:documentation "Internal method for running a test. Does not
167 update the status of the tests nor the special vairables !,
170 (defmethod %run ((test test-case))
171 (run-resolving-dependencies test))
173 (defmethod %run ((suite test-suite))
174 (let ((suite-results '()))
175 (bind-run-state ((result-list '()))
176 (loop for test being the hash-values of (tests suite)
178 finally (setf suite-results result-list)))
179 (setf (status suite) (every (lambda (res)
180 (typep res 'test-passed))
182 (with-run-state (result-list)
183 (setf result-list (nconc result-list suite-results)))))
185 (defmethod %run ((test-name symbol))
186 (when-bind test (get-test test-name)
189 (defvar *initial-!* (lambda () (format t "Haven't run that many tests yet.~%")))
191 (defvar *!* *initial-!*)
192 (defvar *!!* *initial-!*)
193 (defvar *!!!* *initial-!*)
195 ;;;; ** Public entry points
197 (defun run! (&optional (test-spec *suite*))
198 "Equivalent to (explain (run TEST-SPEC))."
199 (explain! (run test-spec)))
201 (defun explain! (result-list)
202 "Explain the results of RESULT-LIST using a
203 detailed-text-explainer with output going to *test-dribble*"
204 (explain (make-instance 'detailed-text-explainer) result-list *test-dribble*))
206 (defun run (test-spec)
207 "Run the test specified by TEST-SPEC.
209 TEST-SPEC can be either a symbol naming a test or test suite, or
210 a testable-object object. This function changes the operations
211 performed by the !, !! and !!! functions."
212 (psetf *!* (lambda ()
213 (loop for test being the hash-keys of *test*
214 do (setf (status (get-test test)) :unknown))
215 (bind-run-state ((result-list '()))
223 "Rerun the most recently run test and explain the results."
224 (explain! (funcall *!*)))
227 "Rerun the second most recently run test and explain the results."
228 (explain! (funcall *!!*)))
231 "Rerun the third most recently run test and explain the results."
232 (explain! (funcall *!!!*)))
234 ;; Copyright (c) 2002-2003, Edward Marco Baringer
235 ;; All rights reserved.
237 ;; Redistribution and use in source and binary forms, with or without
238 ;; modification, are permitted provided that the following conditions are
241 ;; - Redistributions of source code must retain the above copyright
242 ;; notice, this list of conditions and the following disclaimer.
244 ;; - Redistributions in binary form must reproduce the above copyright
245 ;; notice, this list of conditions and the following disclaimer in the
246 ;; documentation and/or other materials provided with the distribution.
248 ;; - Neither the name of Edward Marco Baringer, nor BESE, nor the names
249 ;; of its contributors may be used to endorse or promote products
250 ;; derived from this software without specific prior written permission.
252 ;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
253 ;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
254 ;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
255 ;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
256 ;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
257 ;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
258 ;; LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
259 ;; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
260 ;; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
261 ;; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262 ;; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.