remove softlink
[CommonLispStat.git] / external / lift.darcs / website / source / user-guide.md
blob43b2d298dcb935d3a9b285b333211ea21e411fcc
1 {include header.md}
2 {set-property title "LIFT User's Guide"}
3 {set-property style-sheet user-guide}
4 {set-property docs-package lift}
6 # LIFT User's Guide
8 # Table of Contents 
10 {table-of-contents :start 2 :depth 3}
12 ## Introduction
14 The LIsp Framework for Testing (LIFT) is a unit and system test tool for LISP. 
15 Though inspired by [SUnit][] and [JUnit][], it's built with Lisp in mind. 
16 In LIFT, [testcases][] are organized into hierarchical [testsuites][] each of 
17 which can have its own [fixture][]. When run, a testcase can succeed, fail, 
18 or error. LIFT supports randomized testing, benchmarking, profiling, and reporting.
20  [testcases]> glossary A test-case is the smallest unit of testing.
21  [testsuites]> glossary A testsuite is a group of tests plus their environment plus local variables and configuration settings. 
22  [fixture]> glossary The environment in which a test-case runs. This includes code for both setup and teardown.
24 ## Overview : our first testsuite
26 LIFT supports interactive testing so imagine that we type each of the following forms into a file and evaluate them as we go. 
28     (in-package #:common-lisp-user)
29     (use-package :lift)
31 First, we define an empty testsuite. [Deftestsuite][] is like defclass
32 so here we define a testsuite with no super-testsuites and
33 no slots.
34     
35     > (deftestsuite lift-examples-1 () ())
36     ==> #<lift-examples-1: no tests defined>
38 Add a test-case to our new suite. Since we don't a test name, 
39 LIFT will add this to the most recently defined testsuite 
40 and name it for us.
42     > (addtest (lift-examples-1)
43         (ensure-same (+ 1 1) 2))
44     ==> #<Test passed>
46 Add another test using ensure-error
47 Here we specify the testsuite and the name.
49     > (addtest (lift-examples-1)
50          div-by-zero
51        (ensure-error (let ((x 0)) (/ x))))
52     ==> #<Test passed>
54 Though it works, [ensure-error][] is a bit heavy-handed in this case. We can use
55 [ensure-condition][] to check that we get exactly the right _kind_ of error.
57     > (addtest (lift-examples-1)
58         div-by-zero
59        (ensure-condition division-by-zero (let ((x 0)) (/ x))))
61 Now, we can run all the defined tests.
62 Unless you tell it otherwise, LIFT runs all the test-cases
63 of the most recently touched testsuite. In this case, thats
64 lift-example-1.
66     > (run-tests)
67     ==> #<Results for lift-examples-1 [2 Successful tests]>
69 As you saw above, if you don't supply a test-case name, LIFT will give it one. This works for quick interactive testing but makes it hard to find a problem when running regression tests. It's a much better practice to give every test-case a name -- it also makes the testsuite self documenting. 
71 Here is a test-case that fails because floating point math isn't exact.
73     > (addtest (lift-examples-1)
74        floating-point-math
75        (ensure-same (+ 1.23 1.456) 2.686))
76     ==> #<Test failed>
78 Hmmm, what happened? Lift returns a [test-result][] object so we can look at it to understand what went wrong.
80     > (describe *)
81     Test Report for lift-examples-1: 1 test run, 1 Failure.
82     
83     Failure: lift-examples-1 : floating-point-math
84       Condition: Ensure-same: 2.6859999 is not equal to 2.686
85       Code     : ((ensure-same (+ 1.23 1.456) 2.686))
87 We try again using the function `almost=` for the test of [ensure-same][]
89     > (addtest (lift-examples-1)
90         floating-point-math
91         (ensure-same (+ 1.23 1.456) 2.686 :test 'almost=))
92     ==> #<Error during testing>
94 Whoopts, we forgot to write `almost=`! Here's a simple (though not
95 very efficient) version
97     > (defun almost= (a b)
98        (< (abs (- a b)) 0.000001))
99     ==> almost=
101 Like run-tests, run-test runs the most recently touched test-case.
103     > (run-test)
104     ==> #<lift-examples-1.lift-examples-1 passed>
106 The examples above cover most of LIFT's basics: 
108 * Use [deftestsuite][] and [addtest][] to define testsuites and test-cases.
109 * In a testcase, use members of the ensure family of macros (like [ensure][], [ensure-same][], and [ensure-condition][]) to specify what is supposed to happen
110 * Run tests interactively by evaluating them or by calling [run-test][] or [run-tests][]
112 In what follows, we'll explore LIFT in more depth by looking at test hierarchies and fixtures, randomized testing, and using LIFT for benchmarking and profiling.
114 ## Defining testsuites and adding testcases.
116 The [deftestsuite][] macro defines or redefines a testsuite. Testsuites are CLOS classes and deftestsuite looks a lot like defclass.
118     (deftestsuite name (supersuite*)
119         (slotspec*)
120         options*)
122 The list of supersuites lets you organize tests into a hierarchy. This can be useful both to share fixtures (i.e., setup and tearcode code) and to organize your testing: different parts of the hierarchy can test different parts of your software. The slotspecs are similar to slotspecs in defclass but with a twist: deftestsuite automatically adds an initarg and accessor for each spec{footnote Though they once did, the slotspecs don't really define slots for the class internally anymore. LIFT keeps track of slot values through a different (slower but more flexible) mechanism.}. You can specify an initial value using a pair rather than needing to specify an initform and these value can use the values of previously defined slots (as if they were being bound in a let* form). Finally, you'll also see below that slot values are immediately available with the body of a test method. These two features make writing tests very simple.
124     > (deftestsuite test-slots ()
125         ((a 1) (b 2) (c (+ a b)))
126         (:test ((ensure-same (+ a b) c))))
127     Start: test-slots
128     #<Results for test-slots [1 Successful test]>
130 The example above also shows that you can define tests directly in the deftestsuite form.  This is really handy for unit testing where you don't want the boilerplate to get in the way of the tests! Here is another, more complex example:
132     > (deftestsuite test-leap-year-p ()
133        ()
134        ;; Use :tests to define a list of tests
135        (:tests
136         ((ensure (leap-year-p 1904)))
137         ;; we give this one a name
138         (div-by-four (ensure (leap-year-p 2000)))
139         ((ensure (leap-year-p 1996))))
140        ;; use :test to define one test at a time
141        (:test ((ensure-null (leap-year-p 1900))))
142        (:test ((ensure-null (leap-year-p 1997)))))
144     ;; let's see what we've done
145     > (print-tests :start-at 'test-leap-year-p)
146     test-leap-year-p (5)
147       TEST-1
148       div-by-four
149       TEST-3
150       TEST-4
151       TEST-5
153 So far, our tests have not required any setup or teardown. Let's next look at at a few tests that do. The first example is from the [ASDF-Install][] testsuite. It uses its fixtures setup to make sure that the working directory is empty (so that it is ensured of installing into a clean system).{footnote We'll talk about the :dynamic-variables clause in more detail below}
155     (deftestsuite test-asdf-install-basic-installation (test-asdf-install) 
156       ()
157       (:dynamic-variables 
158        (*verify-gpg-signatures* t))
159       (:setup 
160        (delete-directory-and-files *working-directory* 
161            :if-does-not-exist :ignore)))
163 This next testsuite is from [Log5][log5]. 
164 Though the details aren't important, you can be assured that LIFT will run the setup before every test-case and the teardown after every test-case (even if there is an error).  
166     (deftestsuite test-stream-sender-with-stream (test-stream-sender)
167      (sender-name
168       string-stream
169       (sender nil))
170      (:setup
171       (setf sender-name (gensym)
172          string-stream (make-string-output-stream)))   
173      (:teardown (stop-sender-fn sender-name :warn-if-not-found-p nil))
174      :equality-test #'string-equal)
177 #### Deftestsuite options and arguments
179 We've already seen two other clauses that deftestsuite supports (:dynamic-variables and :equality-test). Here is the complete list:
181 * dynamic-variables - specifies how to initialize dynamic-variables within a testsuite
182 * documentation - used, of all things, for documentation
183 * equality-test - specifies the default equality-test used by ensure-same and ensure-different. See [\*lift-equality-test\*][]
184 * export-p - if true, the testsuite name will be exported
185 * export-slots - if true, all of the testsuite slots will be exported. It can also be a list of slot names to export
186 * function - defines a local test function (think of flet or labels)
187 * random-instance - tells LIFT how to make random examples of things for this suite
188 * run-setup - tells LIFT when to run setup.
189 * setup - code for test setup
190 * teardown - code for testsuite teardown
191 * test - defines a single test
192 * tests - defines several tests
193 * timeout - how long can each test take
195 Many of these are self-explanatory. We'll discuss  :dynamic-variables, :equality-test, :function, :run-setup and :timeout here and look at :random-instance below when we talk about random-testing. 
197 ##### Dynamic-variables
199 It is often the case that you'll want some dynamic variable bound around the body of all of your tests. This is hard to do because LIFT doesn't expose it's mechanism for easy access.{footnote At least, it doesn't expose them yet... One long range plan is to do a better job of building a sort of test metaobject protocol that would make it easier to extend LIFT in new and exciting ways.}. The :dynamic-variables clause lets you specify a list of variables and bindings that LIFT will setup for each testcase. XXX exactly when?
201 ##### Equality-test
203 This is used to specify the default equality-test used by [ensure-same][] for test-cases in this suite and any suites that inherit from it. Though you can use the special variable [*lift-equality-test*][] to set test, it usually better to exercise control at the testsuite level. This is especially handy when, for example, you are testing numeric functions and want to avoid having to specify the test for every `ensure-same`.
205 ##### Function
207 Let the Common Lisp forms `flet`, `labels`, and `macrolet`, [deftestsuite][]'s `function` clause lets you define functions that are local to a particular testsuite (and its descendants). There are two good reasons to use `:function`: it provides good internal documentation and structure _and_ you can use the testsuite's local variables without without any fuss or bother. Here is an example:
209     (deftestsuite test-size (api-tests)
210         (last-count db)
211      (:function
212         (check-size (expected)
213             (ensure (>= (size) last-count))
214             (setf last-count (size))
215             (ensure-same (size) (count-slowly db))
216             (ensure-same (size) expected)))
217      (:setup
218         (setf db (open-data "bar" :if-exists :supersede)))
220 The `check-size` function will not conflict with any other check-size functions (from other tests or any of Lisp's other namespaces). Secondly, the references to `last-count` and `db` will automatically refer to the testsuite's variables.
222 ##### Run-setup
224 LIFT's usual behavior is to run a testsuite's `setup` and `teardown` code around every single test-case. This provides the best isolation and makes it easy to think about a test-case by itself. If test setup takes a _long_ time or if you want to break a complex test into a number of stages, then LIFT's usual behavior will just get in the way. The `run-setup` clause lets you control when `setup` (and `teardown`) occur. It can take on one of the following values:
226 * :once-per-test-case or t (the default) - run `setup` and `teardown` around every testcase
227 * :once-per-suite - run `setup` for the first test-case of a testsuite and run `teardown` after the last test-case.
228 * :never or nil
230 ##### Timeout
232 Things go wrong (that is, after all, part of why we write tests!). The `timeout` clause lets you tell LIFT that if test-case hasn't completed within a certain number of seconds, then you want LIFT to complete the test with an error. 
234 ### LIFT and Random testing
236 To be written.
238 ### Benchmarking with LIFT
240 To be written.
242 ### Reporting
244 To be written.
246 <div class='reference'>
247     
248 ## Reference
250 ### Defining Tests
252 {docs deftestsuite macro}
253 {docs addtest macro}
255 ### How to test for something
257 The following macros can be used outside of LIFT where they will function very much like `assert`. When used in the body of an `addtest` or `deftestsuite` form, however, they will record test failures instead of signaling one themselves.{footnote Random testing adds a few additional `ensure` variants like [ensure-random-cases][]¯¯.}
259 {docs ensure macro}
260 {docs ensure-null macro}
261 {docs ensure-same macro}
262 {docs ensure-different macro}
263 {docs ensure-condition macro}
264 {docs ensure-warning macro}
265 {docs ensure-error macro}
266 {docs ensure-cases macro}
268 ### Running tests 
270 {docs run-test function}
271 {docs run-tests function}
272 {docs lift-result}
273 {docs lift-property}
274 {docs *test-result* variable}
276 ### Configuring LIFT
278 {docs *test-ignore-warnings?* variable}
279 {docs *test-break-on-errors?* variable}
280 {docs *test-print-length* variable}
281 {docs *test-print-level* variable}
282 {docs *test-print-when-defined?* variable}
283 {docs *test-evaluate-when-defined?* variable}
284 {docs *test-describe-if-not-successful?* variable}
285 {docs *test-maximum-time* variable}
286 {docs *test-print-suite-names* variable}
287 {docs *test-print-test-case-names* variable}
288 {docs *lift-equality-test* variable}
289 {docs *lift-debug-output* variable}
291 ### Introspection
293 {docs print-tests}
294 {docs map-testsuites}
295 {docs testsuites}
296 {docs testsuite-tests}
297 {docs find-testsuite}
298 {docs last-test-status}
299 {docs suite-tested-p}
300 {docs testsuite-p}
301 {docs failures}
302 {docs errors}
304 ### Random testing
306 {docs ensure-random-cases}
307 {docs ensure-random-cases+}
308 {docs random-instance-for-suite}
310 {docs ensure-random-cases-failure}
312 {docs defrandom-instance}
313 {docs random-element}
314 {docs random-number}
315 {docs an-integer}
316 {docs a-double-float}
317 {docs a-single-float}
318 {docs a-symbol}
320 ### Benchmarking and Profiling
322 {docs measure-time}
323 {docs measure-space}
325 ### Miscellaneous 
327 {docs test-mixin}
328 {docs *current-test*}
330 </div>
332 ## Indices
334 ### Index of Functions
336 {docs-index function}
338 ### Index of variables
340 {docs-index variable}
342 ### Index of Macros
344 {docs-index macro}
346 ### Full symbol index
348 {docs-index :all}
350 <hr>
352 #### Glossary
354 {glossary}
357 #### Footnotes
359 {footnotes}
361 {include footer.md}