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