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