]> code.delx.au - gnu-emacs/blob - doc/misc/ert.texi
Merge from emacs-23 branch.
[gnu-emacs] / doc / misc / ert.texi
1 \input texinfo
2 @c %**start of header
3 @setfilename ../../info/ert
4 @settitle Emacs Lisp Regression Testing
5 @c %**end of header
6
7 @dircategory Emacs
8 @direntry
9 * ERT: (ert). Emacs Lisp Regression Testing.
10 @end direntry
11
12 @copying
13 Copyright @copyright{} 2008, 2010, 2011 Free Software Foundation, Inc.
14
15 @quotation
16 Permission is granted to copy, distribute and/or modify this document
17 under the terms of the GNU Free Documentation License, Version 1.2 or
18 any later version published by the Free Software Foundation; with no
19 Invariant Sections, with no Front-Cover Texts, and with no Back-Cover
20 Texts.
21 @end quotation
22 @end copying
23
24 @node Top, Introduction, (dir), (dir)
25 @top ERT: Emacs Lisp Regression Testing
26
27 ERT is a tool for automated testing in Emacs Lisp. Its main features
28 are facilities for defining tests, running them and reporting the
29 results, and for debugging test failures interactively.
30
31 ERT is similar to tools for other environments such as JUnit, but has
32 unique features that take advantage of the dynamic and interactive
33 nature of Emacs. Despite its name, it works well both for test-driven
34 development (see
35 @url{http://en.wikipedia.org/wiki/Test-driven_development}) and for
36 traditional software development methods.
37
38 @menu
39 * Introduction:: A simple example of an ERT test.
40 * How to Run Tests:: Run tests in your Emacs or from the command line.
41 * How to Write Tests:: How to add tests to your Emacs Lisp code.
42 * How to Debug Tests:: What to do if a test fails.
43 * Extending ERT:: ERT is extensible in several ways.
44 * Other Testing Concepts:: Features not in ERT.
45
46 @detailmenu
47 --- The Detailed Node Listing ---
48
49 How to Run Tests
50
51 * Running Tests Interactively:: Run tests in your current Emacs.
52 * Running Tests in Batch Mode:: Run tests in emacs -Q.
53 * Test Selectors:: Choose which tests to run.
54
55 How to Write Tests
56
57 * The @code{should} Macro:: A powerful way to express assertions.
58 * Expected Failures:: Tests for known bugs.
59 * Tests and Their Environment:: Don't depend on customizations; no side effects.
60 * Useful Techniques:: Some examples.
61
62 How to Debug Tests
63
64 * Understanding Explanations:: How ERT gives details on why an assertion failed.
65 * Interactive Debugging:: Tools available in the ERT results buffer.
66
67 Extending ERT
68
69 * Defining Explanation Functions:: Teach ERT about more predicates.
70 * Low-Level Functions for Working with Tests:: Use ERT's data for your purposes.
71
72 Other Testing Concepts
73
74 * Mocks and Stubs:: Stubbing out code that is irrelevant to the test.
75 * Fixtures and Test Suites:: How ERT differs from tools for other languages.
76
77 @end detailmenu
78 @end menu
79
80 @node Introduction, How to Run Tests, Top, Top
81 @chapter Introduction
82
83 ERT allows you to define @emph{tests} in addition to functions,
84 macros, variables, and the other usual Lisp constructs. Tests are
85 simply Lisp code --- code that invokes other code and checks whether
86 it behaves as expected.
87
88 ERT keeps track of the tests that are defined and provides convenient
89 commands to run them to verify whether the definitions that are
90 currently loaded in Emacs pass the tests.
91
92 Some Lisp files have comments like the following (adapted from the
93 package @code{pp.el}):
94
95 @lisp
96 ;; (pp-to-string '(quote quote)) ; expected: "'quote"
97 ;; (pp-to-string '((quote a) (quote b))) ; expected: "('a 'b)\n"
98 ;; (pp-to-string '('a 'b)) ; same as above
99 @end lisp
100
101 The code contained in these comments can be evaluated from time to
102 time to compare the output with the expected output. ERT formalizes
103 this and introduces a common convention, which simplifies Emacs
104 development, since programmers no longer have to manually find and
105 evaluate such comments.
106
107 An ERT test definition equivalent to the above comments is this:
108
109 @lisp
110 (ert-deftest pp-test-quote ()
111 "Tests the rendering of `quote' symbols in `pp-to-string'."
112 (should (equal (pp-to-string '(quote quote)) "'quote"))
113 (should (equal (pp-to-string '((quote a) (quote b))) "('a 'b)\n"))
114 (should (equal (pp-to-string '('a 'b)) "('a 'b)\n")))
115 @end lisp
116
117 If you know @code{defun}, the syntax of @code{ert-deftest} should look
118 familiar: This example defines a test named @code{pp-test-quote} that
119 will pass if the three calls to @code{equal} all return true
120 (non-nil).
121
122 @code{should} is a macro with the same meaning as @code{assert} but
123 better error reporting. @xref{The @code{should} Macro}.
124
125 Each test should have a name that describes what functionality the
126 test tests. Test names can be chosen arbitrarily --- they are in a
127 namespace separate from functions and variables --- but should follow
128 the usual Emacs Lisp convention of having a prefix that indicates
129 which package they belong to. Test names are displayed by ERT when
130 reporting failures and can be used when selecting which tests to run.
131
132 The empty parentheses @code{()} in the first line don't currently have
133 any meaning and are reserved for future extension. They also make
134 @code{ert-deftest}'s syntax more similar to @code{defun}.
135
136 The docstring describes what feature this test tests. When running
137 tests interactively, the first line of the docstring is displayed for
138 tests that fail, so it is good if the first line makes sense on its
139 own.
140
141 The body of a test can be arbitrary Lisp code. It should have as few
142 side effects as possible; each test should be written to clean up
143 after itself, leaving Emacs in the same state as it was before the
144 test. Tests should clean up even if they fail. @xref{Tests and Their
145 Environment}.
146
147
148 @node How to Run Tests, How to Write Tests, Introduction, Top
149 @chapter How to Run Tests
150
151 You can run tests either in the Emacs you are working in, or on the
152 command line in a separate Emacs process in batch mode (i.e., with no
153 user interface). The former mode is convenient during interactive
154 development, the latter is useful to make sure that tests pass
155 independently of your customizations, allows tests to be invoked from
156 makefiles and scripts to be written that run tests in several
157 different Emacs versions.
158
159 @menu
160 * Running Tests Interactively:: Run tests in your current Emacs.
161 * Running Tests in Batch Mode:: Run tests in emacs -Q.
162 * Test Selectors:: Choose which tests to run.
163 @end menu
164
165
166 @node Running Tests Interactively, Running Tests in Batch Mode, How to Run Tests, How to Run Tests
167 @section Running Tests Interactively
168
169 You can run the tests that are currently defined in your Emacs with
170 the command @kbd{@kbd{M-x} ert @kbd{RET} t @kbd{RET}}. ERT will pop
171 up a new buffer, the ERT results buffer, showing the results of the
172 tests run. It looks like this:
173
174 @example
175 Selector: t
176 Passed: 31
177 Failed: 2 (2 unexpected)
178 Total: 33/33
179
180 Started at: 2008-09-11 08:39:25-0700
181 Finished.
182 Finished at: 2008-09-11 08:39:27-0700
183
184 FF...............................
185
186 F addition-test
187 (ert-test-failed
188 ((should
189 (=
190 (+ 1 2)
191 4))
192 :form
193 (= 3 4)
194 :value nil))
195
196 F list-test
197 (ert-test-failed
198 ((should
199 (equal
200 (list 'a 'b 'c)
201 '(a b d)))
202 :form
203 (equal
204 (a b c)
205 (a b d))
206 :value nil :explanation
207 (list-elt 2
208 (different-atoms c d))))
209 @end example
210
211 At the top, there is a summary of the results: We ran all tests in the
212 current Emacs (@code{Selector: t}), 31 of them passed, and 2 failed
213 unexpectedly. @xref{Expected Failures}, for an explanation of the
214 term @emph{unexpected} in this context.
215
216 The line of dots and @code{F}s is a progress bar where each character
217 represents one test; it fills while the tests are running. A dot
218 means that the test passed, an @code{F} means that it failed. Below
219 the progress bar, ERT shows details about each test that had an
220 unexpected result. In the example above, there are two failures, both
221 due to failed @code{should} forms. @xref{Understanding Explanations},
222 for more details.
223
224 In the ERT results buffer, @kbd{TAB} and @kbd{S-TAB} cycle between
225 buttons. Each name of a function or macro in this buffer is a button;
226 moving point to it and typing @kbd{RET} jumps to its definition.
227
228 Pressing @kbd{r} re-runs the test near point on its own. Pressing
229 @kbd{d} re-runs it with the debugger enabled. @kbd{.} jumps to the
230 definition of the test near point (@kbd{RET} has the same effect if
231 point is on the name of the test). On a failed test, @kbd{b} shows
232 the backtrace of the failure.
233
234 @kbd{l} shows the list of @code{should} forms executed in the test.
235 If any messages were generated (with the Lisp function @code{message})
236 in a test or any of the code that it invoked, @kbd{m} will show them.
237
238 By default, long expressions in the failure details are abbreviated
239 using @code{print-length} and @code{print-level}. Pressing @kbd{L}
240 while point is on a test failure will increase the limits to show more
241 of the expression.
242
243
244 @node Running Tests in Batch Mode, Test Selectors, Running Tests Interactively, How to Run Tests
245 @section Running Tests in Batch Mode
246
247 ERT supports automated invocations from the command line or from
248 scripts or makefiles. There are two functions for this purpose,
249 @code{ert-run-tests-batch} and @code{ert-run-tests-batch-and-exit}.
250 They can be used like this:
251
252 @example
253 emacs -batch -L /path/to/ert -l ert.el -l my-tests.el -f ert-run-tests-batch-and-exit
254 @end example
255
256 This command will start up Emacs in batch mode, load ERT, load
257 @code{my-tests.el}, and run all tests defined in it. It will exit
258 with a zero exit status if all tests passed, or nonzero if any tests
259 failed or if anything else went wrong. It will also print progress
260 messages and error diagnostics to standard output.
261
262 You may need additional @code{-L} flags to ensure that
263 @code{my-tests.el} and all the files that it requires are on your
264 @code{load-path}.
265
266
267 @node Test Selectors, , Running Tests in Batch Mode, How to Run Tests
268 @section Test Selectors
269
270 Functions like @code{ert} accept a @emph{test selector}, a Lisp
271 expression specifying a set of tests. Test selector syntax is similar
272 to Common Lisp's type specifier syntax:
273
274 @itemize
275 @item @code{nil} selects no tests.
276 @item @code{t} selects all tests.
277 @item @code{:new} selects all tests that have not been run yet.
278 @item @code{:failed} and @code{:passed} select tests according to their most recent result.
279 @item @code{:expected}, @code{:unexpected} select tests according to their most recent result.
280 @item A string selects all tests that have a name that matches the string, a regexp.
281 @item A test selects that test.
282 @item A symbol selects the test that the symbol names.
283 @item @code{(member TESTS...)} selects TESTS, a list of tests or symbols naming tests.
284 @item @code{(eql TEST)} selects TEST, a test or a symbol naming a test.
285 @item @code{(and SELECTORS...)} selects the tests that match all SELECTORS.
286 @item @code{(or SELECTORS...)} selects the tests that match any SELECTOR.
287 @item @code{(not SELECTOR)} selects all tests that do not match SELECTOR.
288 @item @code{(tag TAG)} selects all tests that have TAG on their tags list.
289 @item @code{(satisfies PREDICATE)} Selects all tests that satisfy PREDICATE.
290 @end itemize
291
292 Selectors that are frequently useful when selecting tests to run
293 include @code{t} to run all tests that are currently defined in Emacs,
294 @code{"^foo-"} to run all tests in package @code{foo} --- this assumes
295 that package @code{foo} uses the prefix @code{foo-} for its test names
296 ---, result-based selectors such as @code{(or :new :unexpected)} to
297 run all tests that have either not run yet or that had an unexpected
298 result in the last run, and tag-based selectors such as @code{(not
299 (tag :causes-redisplay))} to run all tests that are not tagged
300 @code{:causes-redisplay}.
301
302
303 @node How to Write Tests, How to Debug Tests, How to Run Tests, Top
304 @chapter How to Write Tests
305
306 ERT lets you define tests in the same way you define functions. You
307 can type @code{ert-deftest} forms in a buffer and evaluate them there
308 with @code{eval-defun} or @code{compile-defun}, or you can save the
309 file and load it, optionally byte-compiling it first.
310
311 Just like @code{find-function} is only able to find where a function
312 was defined if the function was loaded from a file, ERT is only able
313 to find where a test was defined if the test was loaded from a file.
314
315
316 @menu
317 * The @code{should} Macro:: A powerful way to express assertions.
318 * Expected Failures:: Tests for known bugs.
319 * Tests and Their Environment:: Don't depend on customizations; no side effects.
320 * Useful Techniques:: Some examples.
321 @end menu
322
323 @node The @code{should} Macro, Expected Failures, How to Write Tests, How to Write Tests
324 @section The @code{should} Macro
325
326 Test bodies can include arbitrary code; but to be useful, they need to
327 have checks whether the code being tested (or @emph{code under test})
328 does what it is supposed to do. The macro @code{should} is similar to
329 @code{assert} from the cl package, but analyzes its argument form and
330 records information that ERT can display to help debugging.
331
332 This test definition
333
334 @lisp
335 (ert-deftest addition-test ()
336 (should (= (+ 1 2) 4)))
337 @end lisp
338
339 will produce this output when run via @kbd{M-x ert}:
340
341 @example
342 F addition-test
343 (ert-test-failed
344 ((should
345 (=
346 (+ 1 2)
347 4))
348 :form
349 (= 3 4)
350 :value nil))
351 @end example
352
353 In this example, @code{should} recorded the fact that (= (+ 1 2) 4)
354 reduced to (= 3 4) before it reduced to nil. When debugging why the
355 test failed, it helps to know that the function @code{+} returned 3
356 here. ERT records the return value for any predicate called directly
357 within @code{should}.
358
359 In addition to @code{should}, ERT provides @code{should-not}, which
360 checks that the predicate returns nil, and @code{should-error}, which
361 checks that the form called within it signals an error. An example
362 use of @code{should-error}:
363
364 @lisp
365 (ert-deftest test-divide-by-zero ()
366 (should-error (/ 1 0)
367 :type 'arith-error))
368 @end lisp
369
370 This checks that dividing one by zero signals an error of type
371 @code{arith-error}. The @code{:type} argument to @code{should-error}
372 is optional; if absent, any type of error is accepted.
373 @code{should-error} returns an error description of the error that was
374 signalled, to allow additional checks to be made. The error
375 description has the format @code{(ERROR-SYMBOL . DATA)}.
376
377 There is no @code{should-not-error} macro since tests that signal an
378 error fail anyway, so @code{should-not-error} is effectively the
379 default.
380
381 @xref{Understanding Explanations}, for more details on what
382 @code{should} reports.
383
384
385 @node Expected Failures, Tests and Their Environment, The @code{should} Macro, How to Write Tests
386 @section Expected Failures
387
388 Some bugs are complicated to fix or not very important and are left as
389 @emph{known bugs}. If there is a test case that triggers the bug and
390 fails, ERT will alert you of this failure every time you run all
391 tests. For known bugs, this alert is a distraction. The way to
392 suppress it is to add @code{:expected-result :failed} to the test
393 definition:
394
395 @lisp
396 (ert-deftest future-bug ()
397 "Test `time-forward' with negative arguments.
398 Since this functionality isn't implemented yet, the test is known to fail."
399 :expected-result :failed
400 (time-forward -1))
401 @end lisp
402
403 ERT will still display a small @code{f} in the progress bar as a
404 reminder that there is a known bug, and will count the test as failed,
405 but it will be quiet about it otherwise.
406
407 An alternative to marking the test as a known failure this way is to
408 delete the test. This is a good idea if there is no intent to fix it,
409 i.e., if the behavior that was formerly considered a bug has become an
410 accepted feature.
411
412 In general, however, it can be useful to keep tests that are known to
413 fail. If someone wants to fix the bug, they will have a very good
414 starting point: an automated test case that reproduces the bug. This
415 makes it much easier to fix the bug, demonstrate that it is fixed, and
416 prevent future regressions.
417
418 ERT displays the same kind of alerts for tests that pass unexpectedly
419 that it displays for unexpected failures. This way, if you make code
420 changes that happen to fix a bug that you weren't aware of, you will
421 know to remove the @code{:expected-result} clause of that test and
422 close the corresponding bug report, if any.
423
424 Since @code{:expected-result} evaluates its argument when the test is
425 loaded, tests can be marked as known failures only on certain Emacs
426 versions, specific architectures, etc.:
427
428 @lisp
429 (ert-deftest foo ()
430 "A test that is expected to fail on Emacs 23 but succeed elsewhere."
431 :expected-result (if (string-match "GNU Emacs 23[.]" (emacs-version))
432 :failed
433 :passed)
434 ...)
435 @end lisp
436
437
438 @node Tests and Their Environment, Useful Techniques, Expected Failures, How to Write Tests
439 @section Tests and Their Environment
440
441 The outcome of running a test should not depend on the current state
442 of the environment, and each test should leave its environment in the
443 same state it found it in. In particular, a test should not depend on
444 any Emacs customization variables or hooks, and if it has to make any
445 changes to Emacs' state or state external to Emacs such as the file
446 system, it should undo these changes before it returns, regardless of
447 whether it passed or failed.
448
449 Tests should not depend on the environment because any such
450 dependencies can make the test brittle or lead to failures that occur
451 only under certain circumstances and are hard to reproduce. Of
452 course, the code under test may have settings that affect its
453 behavior. In that case, it is best to make the test @code{let}-bind
454 all such settings variables to set up a specific configuration for the
455 duration of the test. The test can also set up a number of different
456 configurations and run the code under test with each.
457
458 Tests that have side effects on their environment should restore it to
459 its original state because any side effects that persist after the
460 test can disrupt the workflow of the programmer running the tests. If
461 the code under test has side effects on Emacs' current state, such as
462 on the current buffer or window configuration, the test should create
463 a temporary buffer for the code to manipulate (using
464 @code{with-temp-buffer}), or save and restore the window configuration
465 (using @code{save-window-excursion}), respectively. For aspects of
466 the state that can not be preserved with such macros, cleanup should
467 be performed with @code{unwind-protect}, to ensure that the cleanup
468 occurs even if the test fails.
469
470 An exception to this are messages that the code under test prints with
471 @code{message} and similar logging; tests should not bother restoring
472 the @code{*Message*} buffer to its original state.
473
474 The above guidelines imply that tests should avoid calling highly
475 customizable commands such as @code{find-file}, except, of course, if
476 such commands are what they want to test. The exact behavior of
477 @code{find-file} depends on many settings such as
478 @code{find-file-wildcards}, @code{enable-local-variables}, and
479 @code{auto-mode-alist}. It is difficult to write a meaningful test if
480 its behavior can be affected by so many external factors. Also,
481 @code{find-file} has side effects that are hard to predict and thus
482 hard to undo: It may create a new buffer or may reuse an existing
483 buffer if one is already visiting the requested file; and it runs
484 @code{find-file-hook}, which can have arbitrary side effects.
485
486 Instead, it is better to use lower-level mechanisms with simple and
487 predictable semantics like @code{with-temp-buffer}, @code{insert} or
488 @code{insert-file-contents-literally}, and activating the desired mode
489 by calling the corresponding function directly --- after binding the
490 hook variables to nil. This avoids the above problems.
491
492
493 @node Useful Techniques, , Tests and Their Environment, How to Write Tests
494 @section Useful Techniques when Writing Tests
495
496 Testing simple functions that have no side effects and no dependencies
497 on their environment is easy. Such tests often look like this:
498
499 @lisp
500 (ert-deftest ert-test-mismatch ()
501 (should (eql (ert--mismatch "" "") nil))
502 (should (eql (ert--mismatch "" "a") 0))
503 (should (eql (ert--mismatch "a" "a") nil))
504 (should (eql (ert--mismatch "ab" "a") 1))
505 (should (eql (ert--mismatch "Aa" "aA") 0))
506 (should (eql (ert--mismatch '(a b c) '(a b d)) 2)))
507 @end lisp
508
509 This test calls the function @code{ert--mismatch} several times with
510 various combinations of arguments and compares the return value to the
511 expected return value. (Some programmers prefer @code{(should (eql
512 EXPECTED ACTUAL))} over the @code{(should (eql ACTUAL EXPECTED))}
513 shown here. ERT works either way.)
514
515 Here's a more complicated test:
516
517 @lisp
518 (ert-deftest ert-test-record-backtrace ()
519 (let ((test (make-ert-test :body (lambda () (ert-fail "foo")))))
520 (let ((result (ert-run-test test)))
521 (should (ert-test-failed-p result))
522 (with-temp-buffer
523 (ert--print-backtrace (ert-test-failed-backtrace result))
524 (goto-char (point-min))
525 (end-of-line)
526 (let ((first-line (buffer-substring-no-properties (point-min) (point))))
527 (should (equal first-line " signal(ert-test-failed (\"foo\"))")))))))
528 @end lisp
529
530 This test creates a test object using @code{make-ert-test} whose body
531 will immediately signal failure. It then runs that test and asserts
532 that it fails. Then, it creates a temporary buffer and invokes
533 @code{ert--print-backtrace} to print the backtrace of the failed test
534 to the current buffer. Finally, it extracts the first line from the
535 buffer and asserts that it matches what we expect. It uses
536 @code{buffer-substring-no-properties} and @code{equal} to ignore text
537 properties; for a test that takes properties into account,
538 @code{buffer-substring} and @code{ert-equal-including-properties}
539 could be used instead.
540
541 The reason why this test only checks the first line of the backtrace
542 is that the remainder of the backtrace is dependent on ERT's internals
543 as well as whether the code is running interpreted or compiled. By
544 looking only at the first line, the test checks a useful property
545 --- that the backtrace correctly captures the call to @code{signal} that
546 results from the call to @code{ert-fail} --- without being brittle.
547
548 This example also shows that writing tests is much easier if the code
549 under test was structured with testing in mind.
550
551 For example, if @code{ert-run-test} accepted only symbols that name
552 tests rather than test objects, the test would need a name for the
553 failing test, which would have to be a temporary symbol generated with
554 @code{make-symbol}, to avoid side effects on Emacs' state. Choosing
555 the right interface for @code{ert-run-tests} allows the test to be
556 simpler.
557
558 Similarly, if @code{ert--print-backtrace} printed the backtrace to a
559 buffer with a fixed name rather than the current buffer, it would be
560 much harder for the test to undo the side effect. Of course, some
561 code somewhere needs to pick the buffer name. But that logic is
562 independent of the logic that prints backtraces, and keeping them in
563 separate functions allows us to test them independently.
564
565 A lot of code that you will encounter in Emacs was not written with
566 testing in mind. Sometimes, the easiest way to write tests for such
567 code is to restructure the code slightly to provide better interfaces
568 for testing. Usually, this makes the interfaces easier to use as
569 well.
570
571
572 @node How to Debug Tests, Extending ERT, How to Write Tests, Top
573 @chapter How to Debug Tests
574
575 This section describes how to use ERT's features to understand why
576 a test failed.
577
578
579 @menu
580 * Understanding Explanations:: How ERT gives details on why an assertion failed.
581 * Interactive Debugging:: Tools available in the ERT results buffer.
582 @end menu
583
584
585 @node Understanding Explanations, Interactive Debugging, How to Debug Tests, How to Debug Tests
586 @section Understanding Explanations
587
588 Failed @code{should} forms are reported like this:
589
590 @example
591 F addition-test
592 (ert-test-failed
593 ((should
594 (=
595 (+ 1 2)
596 4))
597 :form
598 (= 3 4)
599 :value nil))
600 @end example
601
602 ERT shows what the @code{should} expression looked like and what
603 values its subexpressions had: The source code of the assertion was
604 @code{(should (= (+ 1 2) 4))}, which applied the function @code{=} to
605 the arguments @code{3} and @code{4}, resulting in the value
606 @code{nil}. In this case, the test is wrong; it should expect 3
607 rather than 4.
608
609 If a predicate like @code{equal} is used with @code{should}, ERT
610 provides a so-called @emph{explanation}:
611
612 @example
613 F list-test
614 (ert-test-failed
615 ((should
616 (equal
617 (list 'a 'b 'c)
618 '(a b d)))
619 :form
620 (equal
621 (a b c)
622 (a b d))
623 :value nil :explanation
624 (list-elt 2
625 (different-atoms c d))))
626 @end example
627
628 In this case, the function @code{equal} was applied to the arguments
629 @code{(a b c)} and @code{(a b d)}. ERT's explanation shows that
630 the item at index 2 differs between the two lists; in one list, it is
631 the atom c, in the other, it is the atom d.
632
633 In simple examples like the above, the explanation is unnecessary.
634 But in cases where the difference is not immediately apparent, it can
635 save time:
636
637 @example
638 F test1
639 (ert-test-failed
640 ((should
641 (equal x y))
642 :form
643 (equal a a)
644 :value nil :explanation
645 (different-symbols-with-the-same-name a a)))
646 @end example
647
648 ERT only provides explanations for predicates that have an explanation
649 function registered. @xref{Defining Explanation Functions}.
650
651
652 @node Interactive Debugging, , Understanding Explanations, How to Debug Tests
653 @section Interactive Debugging
654
655 Debugging failed tests works essentially the same way as debugging any
656 other problems with Lisp code. Here are a few tricks specific to
657 tests:
658
659 @itemize
660 @item Re-run the failed test a few times to see if it fails in the same way
661 each time. It's good to find out whether the behavior is
662 deterministic before spending any time looking for a cause. In the
663 ERT results buffer, @kbd{r} re-runs the selected test.
664
665 @item Use @kbd{.} to jump to the source code of the test to find out what
666 exactly it does. Perhaps the test is broken rather than the code
667 under test.
668
669 @item If the test contains a series of @code{should} forms and you can't
670 tell which one failed, use @kbd{l}, which shows you the list of all
671 @code{should} forms executed during the test before it failed.
672
673 @item Use @kbd{b} to view the backtrace. You can also use @kbd{d} to re-run
674 the test with debugging enabled, this will enter the debugger and show
675 the backtrace as well; but the top few frames shown there will not be
676 relevant to you since they are ERT's own debugger hook. @kbd{b}
677 strips them out, so it is more convenient.
678
679 @item If the test or the code under testing prints messages using
680 @code{message}, use @kbd{m} to see what messages it printed before it
681 failed. This can be useful to figure out how far it got.
682
683 @item You can instrument tests for debugging the same way you instrument
684 @code{defun}s for debugging --- go to the source code of the test and
685 type @kbd{@kbd{C-u} @kbd{C-M-x}}. Then, go back to the ERT buffer and
686 re-run the test with @kbd{r} or @kbd{d}.
687
688 @item If you have been editing and rearranging tests, it is possible that
689 ERT remembers an old test that you have since renamed or removed ---
690 renamings or removals of definitions in the source code leave around a
691 stray definition under the old name in the running process, this is a
692 common problem in Lisp. In such a situation, hit @kbd{D} to let ERT
693 forget about the obsolete test.
694 @end itemize
695
696
697 @node Extending ERT, Other Testing Concepts, How to Debug Tests, Top
698 @chapter Extending ERT
699
700 There are several ways to add functionality to ERT.
701
702 @menu
703 * Defining Explanation Functions:: Teach ERT about more predicates.
704 * Low-Level Functions for Working with Tests:: Use ERT's data for your purposes.
705 @end menu
706
707
708 @node Defining Explanation Functions, Low-Level Functions for Working with Tests, Extending ERT, Extending ERT
709 @section Defining Explanation Functions
710
711 The explanation function for a predicate is a function that takes the
712 same arguments as the predicate and returns an @emph{explanation}.
713 The explanation should explain why the predicate, when invoked with
714 the arguments given to the explanation function, returns the value
715 that it returns. The explanation can be any object but should have a
716 comprehensible printed representation. If the return value of the
717 predicate needs no explanation for a given list of arguments, the
718 explanation function should return nil.
719
720 To associate an explanation function with a predicate, add the
721 property @code{ert-explainer} to the symbol that names the predicate.
722 The value of the property should be the symbol that names the
723 explanation function.
724
725
726 @node Low-Level Functions for Working with Tests, , Defining Explanation Functions, Extending ERT
727 @section Low-Level Functions for Working with Tests
728
729 Both @code{ert-run-tests-interactively} and @code{ert-run-tests-batch}
730 are implemented on top of the lower-level test handling code in the
731 sections named ``Facilities for running a single test'', ``Test
732 selectors'', and ``Facilities for running a whole set of tests''.
733
734 If you want to write code that works with ERT tests, you should take a
735 look at this lower-level code. Symbols that start with @code{ert--}
736 are internal to ERT, those that start with @code{ert-} but not
737 @code{ert--} are meant to be usable by other code. But there is no
738 mature API yet.
739
740 Contributions to ERT are welcome.
741
742
743 @node Other Testing Concepts, , Extending ERT, Top
744 @chapter Other Testing Concepts
745
746 For information on mocks, stubs, fixtures, or test suites, see below.
747
748
749 @menu
750 * Mocks and Stubs:: Stubbing out code that is irrelevant to the test.
751 * Fixtures and Test Suites:: How ERT differs from tools for other languages.
752 @end menu
753
754 @node Mocks and Stubs, Fixtures and Test Suites, Other Testing Concepts, Other Testing Concepts
755 @section Other Tools for Emacs Lisp
756
757 Stubbing out functions or using so-called @emph{mocks} can make it
758 easier to write tests. See
759 @url{http://en.wikipedia.org/wiki/Mock_object} for an explanation of
760 the corresponding concepts in object-oriented languages.
761
762 ERT does not have built-in support for mocks or stubs. The package
763 @code{el-mock} (see @url{http://www.emacswiki.org/emacs/el-mock.el})
764 offers mocks for Emacs Lisp and can be used in conjunction with ERT.
765
766
767 @node Fixtures and Test Suites, , Mocks and Stubs, Other Testing Concepts
768 @section Fixtures and Test Suites
769
770 In many ways, ERT is similar to frameworks for other languages like
771 SUnit or JUnit. However, two features commonly found in such
772 frameworks are notably absent from ERT: fixtures and test suites.
773
774 Fixtures, as used e.g. in SUnit or JUnit, are mainly used to provide
775 an environment for a set of tests, and consist of set-up and tear-down
776 functions.
777
778 While fixtures are a useful syntactic simplification in other
779 languages, this does not apply to Lisp, where higher-order functions
780 and `unwind-protect' are available. One way to implement and use a
781 fixture in ERT is
782
783 @lisp
784 (defun my-fixture (body)
785 (unwind-protect
786 (progn [set up]
787 (funcall body))
788 [tear down]))
789
790 (ert-deftest my-test ()
791 (my-fixture
792 (lambda ()
793 [test code])))
794 @end lisp
795
796 (Another way would be a @code{with-my-fixture} macro.) This solves
797 the set-up and tear-down part, and additionally allows any test
798 to use any combination of fixtures, so it is more flexible than what
799 other tools typically allow.
800
801 If the test needs access to the environment the fixture sets up, the
802 fixture can be modified to pass arguments to the body.
803
804 These are well-known Lisp techniques. Special syntax for them could
805 be added but would provide only a minor simplification.
806
807 (If you are interested in such syntax, note that splitting set-up and
808 tear-down into separate functions, like *Unit tools usually do, makes
809 it impossible to establish dynamic `let' bindings as part of the
810 fixture. So, blindly imitating the way fixtures are implemented in
811 other languages would be counter-productive in Lisp.)
812
813 The purpose of test suites is to group related tests together.
814
815 The most common use of this is to run just the tests for one
816 particular module. Since symbol prefixes are the usual way of
817 separating module namespaces in Emacs Lisp, test selectors already
818 solve this by allowing regexp matching on test names; e.g., the
819 selector "^ert-" selects ERT's self-tests.
820
821 Other uses include grouping tests by their expected execution time to
822 run quick tests during interactive development and slow tests less
823 frequently. This can be achieved with the @code{:tag} argument to
824 @code{ert-deftest} and @code{tag} test selectors.
825
826 @bye
827
828 @c LocalWords: ERT Hagelberg Ohler JUnit namespace docstring ERT's
829 @c LocalWords: backtrace makefiles workflow backtraces API SUnit
830 @c LocalWords: subexpressions