Removed mis-quotes.
[ETest.git] / etest.texinfo
blob8e9b2a7d60fd44bdc4fe02a50fda68c43205d51c
1 \input texinfo   @c -*-texinfo-*-
3 @setfilename etest.info
4 @settitle Emacs Testing Framework
6 @dircategory Emacs
7 @direntry
8 * ETest: (etest).           The Emacs Testing Framework
9 @end direntry
11 @copying
12 Copyright @copyright{} 2008 Philip Jackson.
13 @quotation
14 Permission is granted to copy, distribute and/or modify this document
15 under the terms of the GNU Free Documentation License, Version 1.1 or
16 any later version published by the Free Software Foundation; with no
17 Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A
18 copy of the license is included in the section entitled "GNU Free
19 Documentation License".
20 @end quotation
21 @end copying
23 @titlepage
24 @title ETest - The Emacs Testing Framework
26 @page
27 @vskip 0pt plus 1filll
28 @insertcopying
29 @end titlepage
31 @ifhtml
32 @contents
33 @end ifhtml
35 @ifnottex
36 @node Top, Introduction, (dir), (dir)
37 @top ETest Manual 0.1
39 @menu
40 * Introduction::                Introduction to ETest.
41 * Fetching ETest::              How to download ETest.
42 * Installation::                How to get emacs to wear ETest.
43 * Usage::                       How to use ETest.
44 * The results buffer::          Display of test results.
45 * The Tests::                   Functions available for testing.
46 @end menu
47 @end ifnottex
49 @node Introduction, Fetching ETest, Top, Top
50 @chapter Introduction
52 ETest (or etest, if you like) is the Emacs Testing Framework. It is a
53 small modular system for writing unit tests in Emacs Lisp.
55 At time of writing ETest consists of two files. @file{etest.el}
56 provides the core test functionality. It defines the function
57 (actually, macro) @code{etest} which is usually one's entrance into a
58 test run. The file @file{etest-result-mode.el} adds functions that
59 allow the visualisation of a run with syntax highlighting, folding and
60 comment toggling.
62 @node Fetching ETest, Installation, Introduction, Top
63 @chapter Fetching ETest
65 At the time of writing you must download ETest using git:
67 @code{git clone http://repo.or.cz/r/ETest.git etest}
69 Though it will always be bleeding edge @code{master} should always be
70 in a @emph{working} state.
72 @node Installation, Usage, Fetching ETest, Top
73 @chapter Installation
75 To install ETest use one of the following methods:
77 @section Compiling ETest
79 Once you've fetched ETest change into the new directory and run
80 @code{make} if all goes well run @code{make install} (you may need
81 escalated privileges for this step).
83 @section Manual Installation
85 If you don't want to byte-compile for some reason then you can just
86 copy the @code{.el} files into a directory in your @var{load-path}.
88 If you would rather add the unpacked library @emph{to} your load path
89 then the following will work assuming the etest directory is in your
90 home directory:
92 @lisp
93 (add-to-list 'load-path "~/etest")
94 @end lisp
96 If you want to run tests from a lisp buffer add the following to your
97 initialistation file:
99 @lisp
100 ;; The only keybinding we make... hopefully
101 (add-hook 'emacs-lisp-mode-hook
102           (lambda ()
103             (local-set-key (kbd "C-c t") 'etest-execute)))
104 @end lisp
106 And to have @code{.etest} files load @code{emacs-lisp-mode} put the
107 following into your initialistation file:
109 @lisp
110 ;; Load lisp mode when editing an etest file
111 (add-to-list 'auto-mode-alist '("\\.etest$" . emacs-lisp-mode))
112 @end lisp
114 @node Usage, The results buffer, Installation, Top
115 @chapter Usage
117 First you must evaluate @code{(require 'etest)} then you can start
118 using the @code{etest} macro.
120 Using ETest is very simple. Once you have installed the modules then
121 you can simply run your first test like this:
123 @lisp
124 (etest (ok 1))
125 @end lisp
126 @noindent
128 This should pop up a results buffer showing you the outcome of the
129 run. In this case all should be ok because, well, 1 is a pass
130 according to the @code{ok} test.
132 @menu
133 * Example working practice::    How one might use etest.
134 @end menu
136 @node Example working practice,  , Usage, Usage
137 @section Example working practice
139 Generally (at least the way I work) is to have a @code{.etest} file
140 which matches all but the extension of the filename of the file I am
141 to test. So if I were testing a file called @code{find-cmd.el} I would
142 write my tests in a file called @code{find-cmd.etest} (in the same
143 directory as @code{find-cmd.el}) and then when I hit @kbd{C-c t} (in
144 @code{emacs-lisp-mode}) the run would happen and the results pop up in
145 their normal fashion.
147 Thanks to @file{etest-execute.el} there are several options for the
148 location of an etest file (one at a time). In order of execution they
149 are:
151 1. The buffer local variable @code{etest-file} is set to the filename
152 of the etest file.
154 2. A matching etest file is in the directories listed in
155 @code{etest-load-path}.
157 3. A matching etest file is in the current working directory
158 (@code{default-directory}).
160 4. A matching etest file is in one of the directories mentioned in
161 @code{load-path}.
163 At the moment it's impossible to have two etest structures defined in
164 one file @emph{and} see the results in one results buffer (at least in
165 the result mode that is bundled with ETest) as an @code{erase-buffer}
166 is executed (effectively) for each invocation of the @code{etest}
167 macro.
169 @node The results buffer, The Tests, Usage, Top
170 @chapter The results buffer
172 The results buffer is where you can see (and manipulate) the results
173 of a test run in a human friendly format. It will always popup when
174 etest is run and let you know how things went.
176 @menu
177 * Example output::              What the output looks like.
178 * Bindings::                    What commands you have at your disposal.
179 @end menu
181 @node Example output, Bindings, The results buffer, The results buffer
182 @section Example output
184 Given the hypothetical tests:
186 @lisp
187 (etest
188  ("Numeric comparisons"
189   ("Integers"
190    (ok (> 1 0) "one is more than 0")
191    (ok (< 1 0) "one is less than 0"))
192   ("Floats"
193    (ok (> 10 9.99))
194    (ok (< 1 -5.2) "one is less than 5.2"))))
195 @end lisp
196 @noindent
198 Once run give us the following in the results buffer:
200 @example
201 * Numeric comparisons
202 ** Integers
203    ok ................ one is more than 0
204                        # got: 't'
205    not ok ............ one is less than 0
206                        # got: 'nil'
207 ** Floats
208    ok ................ (ok (> 10 9.99))
209                        # got: 't'
210    not ok ............ one is less than 5.2
211                        # got: 'nil'
212 @end example
213 @noindent
215 All headings are foldable as are comments.
217 @node Bindings,  , Example output, The results buffer
218 @section Bindings
220 @table @kbd
221 @item q
222 @findex bury-buffer
223 Bury this results buffer.
224 @item #
225 @findex etest-rm-etest-rm-cycle-comments
226 Shift the values in @code{etest-rm-comment-visibility-types} and use
227 the @code{car} of that list to determine the visibility of comments.
228 @item <tab>
229 @findex etest-rm-toggle-headline
230 Toggle the visibility of a heading or test comment.
231 @end table
233 @node The Tests,  , The results buffer, Top
234 @chapter The Tests
236 Tests are always run within the @code{etest} form and usually always
237 evaluate their arguments. Tests will always have a defined number of
238 required arguments (for example @code{ok} requires one argument. Each
239 test also allows for one optional argument which is a custom
240 documentation string. If this argument is omitted then ETest will
241 generate one in its place. So, for example, if you used
242 @code{(etest (ok 1))} the doc string would be @code{"(ok 1)"} if you used
243 @code{(etest (ok 1 "Foo"))} the doc string would be @code{"Foo"}.
245 @menu
246 * Test Structure::              Basic structure of tests.
247 * Builtin Simple Tests::        Boolean checks.
248 * Builtin Equality Tests::      Are two things similar?
249 * Builtin Error Tests::         Test exception handling.
250 * Builtin String Tests::        Check a string matches an RE.
251 * Defining your own tests::     Extend ETest.
252 * Leaving tests until later::   Using todo and skip.
253 @end menu
255 @node Test Structure, Builtin Simple Tests, The Tests, The Tests
256 @section Test Structure
258 Tests can be grouped within headings by simply using a string as the
259 first element of a form like this:
261 @lisp
262 (etest
263  ("Simple tests"
264   (ok 1)))
265 @end lisp
266 @noindent
268 You can nest headings to your hearts content.
270 This will produce a set of results that follow the same hierarchical
271 pattern as the tests themselves. For example the above produces the
272 following structure:
274 @lisp
275 (("Simple tests"
276   (:result t :comments "got: '1'" :doc "(ok 1)")))
277 @end lisp
278 @noindent
280 The output in the results buffer is:
282 @example
283 * Simple tests
284   ok ................. (ok 1)
285                        # got: '1'
286 @end example
288 @node Builtin Simple Tests, Builtin Equality Tests, Test Structure, The Tests
289 @section Builtin Simple Tests
291 These basic tests allow you, basically, to check if a value is either
292 non-nil or nil.
294 @subsection ok
296 @code{ok} will only pass if its argument produces a non-nil result.
298 @lisp
299 (etest (ok (+ 1 1)))
300 @end lisp
301 @noindent
303 @subsection null
305 @code{null} will only pass if its argument produces a nil result.
307 @lisp
308 (etest (null nil))
309 @end lisp
310 @noindent
312 @node Builtin Equality Tests, Builtin Error Tests, Builtin Simple Tests, The Tests
313 @section Builtin Equality Tests
315 The following functions map to their lisp counterparts and so don't
316 really require much explanation. Evaluate each of the examples to
317 watch them pass.
319 Each take two forms which, post evaluation, are the objects to
320 compare.
322 @subsection eq
324 @lisp
325 (etest (eq 1 1))
326 @end lisp
327 @noindent
329 @subsection eql
331 @lisp
332 (etest (eql 1.1 1.1))
333 @end lisp
334 @noindent
336 @subsection equal
338 @lisp
339 (etest (equal '(1 2) '(1 2)))
340 @end lisp
341 @noindent
343 @node Builtin Error Tests, Builtin String Tests, Builtin Equality Tests, The Tests
344 @section Builtin Error Tests
346 These two functions each take one form.
348 @subsection error
350 This test will pass if an exception is raised. For example, cause a
351 divide by zero error (@code{(arith-error)}):
353 @lisp
354 (etest (error (/ 1 0)))
355 @end lisp
356 @noindent
358 @subsection noerror
360 This test will pass if no exception is raised. For example, a valid
361 division will not raise an error:
363 @lisp
364 (etest (noerror (/ 0 1)))
365 @end lisp
366 @noindent
368 @node Builtin String Tests, Defining your own tests, Builtin Error Tests, The Tests
369 @section Builtin String Tests
371 @subsection like
373 @code{like} takes two arguments a string and a regexp to test it against:
375 @lisp
376 (etest (like "Hello" "^H\\(e\\)"))
377 @end lisp
378 @noindent
380 Produces this in the results buffer:
382 @example
383  ok .................. (like "Hello" "^H\\(e\\)")
384                        # searching: 'Hello'
385                        # match   1: 'e'
386 @end example
387 @noindent
389 The grouping within the regular expression only affects the comments.
391 @node Defining your own tests, Leaving tests until later, Builtin String Tests, The Tests
392 @section Defining your own tests
394 Defining your own tests is fairly trivial and where ETest becomes
395 really useful.
397 Each test must return a plist that has @code{:result} and, optionally,
398 @code{:comments} in it.
400 @code{:result} represents whether the test passed or failed non-nil
401 for a pass and nil for a fail.
403 The optional @code{:comments} are newline separated strings that might
404 help the user in their diagnosis of a problem. Comments should follow
405 the conventions set by the 'builtin' tests using keywords such as
406 'got:'.
408 To have ETest recognise the test as valid the @code{deftest} function
409 should be used. For example if I wanted to create a function that
410 took two arguments and tested the first was numerically greater than
411 the other I might do this:
413 @lisp
414 (defun etest-greater-than (one two)
415   (let* ((res (> one two)))
416     (list :result res
417           :comments (unless res
418                       (format "one: '%d'\ntwo: '%d'" one two)))))
419 @end lisp
420 @noindent
422 We let emacs take care of type errors (and any other type of error)
423 which is what all of the builtins do. Also it's worth noting that if
424 you want your arguments evaling you have to do it yourself.
426 Now we let ETest know this new function exists:
428 @lisp
429 (deftest '(> 2) 'etest-greater-than)
430 @end lisp
431 @noindent
433 So, the new function will be called @code{>}, it will take two arguments
434 and it calls maps to @code{etest-greater-than}.
436 @lisp
437 (deftest '(> 2) 'etest-greater-than)
438 @end lisp
439 @noindent
441 Now you can mix @code{>} with other tests:
443 @lisp
444 (etest
445  (ok "something")
446  (> 1 2 "one is more than two"))
447 @end lisp
448 @noindent
450 Which in the results buffer produces:
452 @example
453  ok .................. (ok "something")
454                        # got: '"something"'
455  not ok .............. one is more than two
456                        # one: '1'
457                        # two: '2'
458 @end example
459 @noindent
461 @node Leaving tests until later,  , Defining your own tests, The Tests
462 @section Leaving tests until later
464 @subsection Todo tests
466 If you wrap a test in a todo keyword:
468 @lisp
469 (todo (ok nil))
470 @end lisp
472 Then the test will appear to pass but with an extra keyword in the
473 result (@code{:todo}) with the value @code{t}.
475 The result mode takes account of this by highlighting todoed tests and
476 showing you what happened when ETest actually ran the test (catching
477 any exceptions so as not to stop a run).
479 @bye