aboutsummaryrefslogtreecommitdiffstats
path: root/doc
diff options
context:
space:
mode:
authorChristian Ohler2011-01-13 03:08:24 +1100
committerChristian Ohler2011-01-13 03:08:24 +1100
commitd221e7808c01fdc9234734f95ecf49e902085ddd (patch)
tree09d270adbfdeada366ecd2328b2660a75358dd0c /doc
parent03d32f1b2263270b75a0b3324c52c39965345665 (diff)
downloademacs-d221e7808c01fdc9234734f95ecf49e902085ddd.tar.gz
emacs-d221e7808c01fdc9234734f95ecf49e902085ddd.zip
Add ERT, a tool for automated testing in Emacs Lisp.
* Makefile.in, configure.in, doc/misc/Makefile.in, doc/misc/makefile.w32-in: Add ERT. Make "make check" run tests in test/automated. * doc/misc/ert.texi, lisp/emacs-lisp/ert.el, lisp/emacs-lisp/ert-x.el: New files. * test/automated: New directory.
Diffstat (limited to 'doc')
-rw-r--r--doc/misc/ChangeLog7
-rw-r--r--doc/misc/Makefile.in11
-rw-r--r--doc/misc/ert.texi830
-rw-r--r--doc/misc/makefile.w32-in13
4 files changed, 858 insertions, 3 deletions
diff --git a/doc/misc/ChangeLog b/doc/misc/ChangeLog
index ca43b7e06df..7fc944e523c 100644
--- a/doc/misc/ChangeLog
+++ b/doc/misc/ChangeLog
@@ -1,3 +1,10 @@
12011-01-13 Christian Ohler <ohler@gnu.org>
2
3 * ert.texi: New file.
4
5 * Makefile.in:
6 * makefile.w32-in: Add ert.texi.
7
12011-01-10 Jan Moringen <jan.moringen@uni-bielefeld.de> 82011-01-10 Jan Moringen <jan.moringen@uni-bielefeld.de>
2 9
3 * dbus.texi (Receiving Method Calls): New function 10 * dbus.texi (Receiving Method Calls): New function
diff --git a/doc/misc/Makefile.in b/doc/misc/Makefile.in
index deeafa2c1b2..7953d0cf596 100644
--- a/doc/misc/Makefile.in
+++ b/doc/misc/Makefile.in
@@ -62,6 +62,7 @@ INFO_TARGETS = \
62 $(infodir)/emacs-mime \ 62 $(infodir)/emacs-mime \
63 $(infodir)/epa \ 63 $(infodir)/epa \
64 $(infodir)/erc \ 64 $(infodir)/erc \
65 $(infodir)/ert \
65 $(infodir)/eshell \ 66 $(infodir)/eshell \
66 $(infodir)/eudc \ 67 $(infodir)/eudc \
67 $(infodir)/efaq \ 68 $(infodir)/efaq \
@@ -112,6 +113,7 @@ DVI_TARGETS = \
112 emacs-mime.dvi \ 113 emacs-mime.dvi \
113 epa.dvi \ 114 epa.dvi \
114 erc.dvi \ 115 erc.dvi \
116 ert.dvi \
115 eshell.dvi \ 117 eshell.dvi \
116 eudc.dvi \ 118 eudc.dvi \
117 faq.dvi \ 119 faq.dvi \
@@ -162,6 +164,7 @@ PDF_TARGETS = \
162 emacs-mime.pdf \ 164 emacs-mime.pdf \
163 epa.pdf \ 165 epa.pdf \
164 erc.pdf \ 166 erc.pdf \
167 ert.pdf \
165 eshell.pdf \ 168 eshell.pdf \
166 eudc.pdf \ 169 eudc.pdf \
167 faq.pdf \ 170 faq.pdf \
@@ -360,6 +363,14 @@ erc.dvi: ${srcdir}/erc.texi
360erc.pdf: ${srcdir}/erc.texi 363erc.pdf: ${srcdir}/erc.texi
361 $(ENVADD) $(TEXI2PDF) $< 364 $(ENVADD) $(TEXI2PDF) $<
362 365
366ert : $(infodir)/ert
367$(infodir)/ert: ert.texi $(infodir)
368 cd $(srcdir); $(MAKEINFO) ert.texi
369ert.dvi: ert.texi
370 $(ENVADD) $(TEXI2DVI) ${srcdir}/ert.texi
371ert.pdf: ert.texi
372 $(ENVADD) $(TEXI2PDF) ${srcdir}/ert.texi
373
363eshell : $(infodir)/eshell 374eshell : $(infodir)/eshell
364$(infodir)/eshell: eshell.texi 375$(infodir)/eshell: eshell.texi
365 $(mkinfodir) 376 $(mkinfodir)
diff --git a/doc/misc/ert.texi b/doc/misc/ert.texi
new file mode 100644
index 00000000000..e1a8cdc319c
--- /dev/null
+++ b/doc/misc/ert.texi
@@ -0,0 +1,830 @@
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
13Copyright @copyright{} 2008, 2010, 2011 Free Software Foundation, Inc.
14
15@quotation
16Permission is granted to copy, distribute and/or modify this document
17under the terms of the GNU Free Documentation License, Version 1.2 or
18any later version published by the Free Software Foundation; with no
19Invariant Sections, with no Front-Cover Texts, and with no Back-Cover
20Texts.
21@end quotation
22@end copying
23
24@node Top, Introduction, (dir), (dir)
25@top ERT: Emacs Lisp Regression Testing
26
27ERT is a tool for automated testing in Emacs Lisp. Its main features
28are facilities for defining tests, running them and reporting the
29results, and for debugging test failures interactively.
30
31ERT is similar to tools for other environments such as JUnit, but has
32unique features that take advantage of the dynamic and interactive
33nature of Emacs. Despite its name, it works well both for test-driven
34development (see
35@url{http://en.wikipedia.org/wiki/Test-driven_development}) and for
36traditional 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
49How 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
55How 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
62How 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
67Extending 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
72Other 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
83ERT allows you to define @emph{tests} in addition to functions,
84macros, variables, and the other usual Lisp constructs. Tests are
85simply Lisp code --- code that invokes other code and checks whether
86it behaves as expected.
87
88ERT keeps track of the tests that are defined and provides convenient
89commands to run them to verify whether the definitions that are
90currently loaded in Emacs pass the tests.
91
92Some Lisp files have comments like the following (adapted from the
93package @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
101The code contained in these comments can be evaluated from time to
102time to compare the output with the expected output. ERT formalizes
103this and introduces a common convention, which simplifies Emacs
104development, since programmers no longer have to manually find and
105evaluate such comments.
106
107An 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
117If you know @code{defun}, the syntax of @code{ert-deftest} should look
118familiar: This example defines a test named @code{pp-test-quote} that
119will 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
123better error reporting. @xref{The @code{should} Macro}.
124
125Each test should have a name that describes what functionality the
126test tests. Test names can be chosen arbitrarily --- they are in a
127namespace separate from functions and variables --- but should follow
128the usual Emacs Lisp convention of having a prefix that indicates
129which package they belong to. Test names are displayed by ERT when
130reporting failures and can be used when selecting which tests to run.
131
132The empty parentheses @code{()} in the first line don't currently have
133any meaning and are reserved for future extension. They also make
134@code{ert-deftest}'s syntax more similar to @code{defun}.
135
136The docstring describes what feature this test tests. When running
137tests interactively, the first line of the docstring is displayed for
138tests that fail, so it is good if the first line makes sense on its
139own.
140
141The body of a test can be arbitrary Lisp code. It should have as few
142side effects as possible; each test should be written to clean up
143after itself, leaving Emacs in the same state as it was before the
144test. Tests should clean up even if they fail. @xref{Tests and Their
145Environment}.
146
147
148@node How to Run Tests, How to Write Tests, Introduction, Top
149@chapter How to Run Tests
150
151You can run tests either in the Emacs you are working in, or on the
152command line in a separate Emacs process in batch mode (i.e., with no
153user interface). The former mode is convenient during interactive
154development, the latter is useful to make sure that tests pass
155independently of your customizations, allows tests to be invoked from
156makefiles and scripts to be written that run tests in several
157different 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
169You can run the tests that are currently defined in your Emacs with
170the command @kbd{@kbd{M-x} ert @kbd{RET} t @kbd{RET}}. ERT will pop
171up a new buffer, the ERT results buffer, showing the results of the
172tests run. It looks like this:
173
174@example
175Selector: t
176Passed: 31
177Failed: 2 (2 unexpected)
178Total: 33/33
179
180Started at: 2008-09-11 08:39:25-0700
181Finished.
182Finished at: 2008-09-11 08:39:27-0700
183
184FF...............................
185
186F addition-test
187 (ert-test-failed
188 ((should
189 (=
190 (+ 1 2)
191 4))
192 :form
193 (= 3 4)
194 :value nil))
195
196F 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
211At the top, there is a summary of the results: We ran all tests in the
212current Emacs (@code{Selector: t}), 31 of them passed, and 2 failed
213unexpectedly. @xref{Expected Failures}, for an explanation of the
214term @emph{unexpected} in this context.
215
216The line of dots and @code{F}s is a progress bar where each character
217represents one test; it fills while the tests are running. A dot
218means that the test passed, an @code{F} means that it failed. Below
219the progress bar, ERT shows details about each test that had an
220unexpected result. In the example above, there are two failures, both
221due to failed @code{should} forms. @xref{Understanding Explanations},
222for more details.
223
224In the ERT results buffer, @kbd{TAB} and @kbd{S-TAB} cycle between
225buttons. Each name of a function or macro in this buffer is a button;
226moving point to it and typing @kbd{RET} jumps to its definition.
227
228Pressing @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
230definition of the test near point (@kbd{RET} has the same effect if
231point is on the name of the test). On a failed test, @kbd{b} shows
232the backtrace of the failure.
233
234@kbd{l} shows the list of @code{should} forms executed in the test.
235If any messages were generated (with the Lisp function @code{message})
236in a test or any of the code that it invoked, @kbd{m} will show them.
237
238By default, long expressions in the failure details are abbreviated
239using @code{print-length} and @code{print-level}. Pressing @kbd{L}
240while point is on a test failure will increase the limits to show more
241of 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
247ERT supports automated invocations from the command line or from
248scripts or makefiles. There are two functions for this purpose,
249@code{ert-run-tests-batch} and @code{ert-run-tests-batch-and-exit}.
250They can be used like this:
251
252@example
253emacs -batch -L /path/to/ert -l ert.el -l my-tests.el -f ert-run-tests-batch-and-exit
254@end example
255
256This 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
258with a zero exit status if all tests passed, or nonzero if any tests
259failed or if anything else went wrong. It will also print progress
260messages and error diagnostics to standard output.
261
262You 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
270Functions like @code{ert} accept a @emph{test selector}, a Lisp
271expression specifying a set of tests. Test selector syntax is similar
272to 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
292Selectors that are frequently useful when selecting tests to run
293include @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
295that package @code{foo} uses the prefix @code{foo-} for its test names
296---, result-based selectors such as @code{(or :new :unexpected)} to
297run all tests that have either not run yet or that had an unexpected
298result 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
306ERT lets you define tests in the same way you define functions. You
307can type @code{ert-deftest} forms in a buffer and evaluate them there
308with @code{eval-defun} or @code{compile-defun}, or you can save the
309file and load it, optionally byte-compiling it first.
310
311Just like @code{find-function} is only able to find where a function
312was defined if the function was loaded from a file, ERT is only able
313to 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
326Test bodies can include arbitrary code; but to be useful, they need to
327have checks whether the code being tested (or @emph{code under test})
328does 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
330records information that ERT can display to help debugging.
331
332This test definition
333
334@lisp
335(ert-deftest addition-test ()
336 (should (= (+ 1 2) 4)))
337@end lisp
338
339will produce this output when run via @kbd{M-x ert}:
340
341@example
342F 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
353In this example, @code{should} recorded the fact that (= (+ 1 2) 4)
354reduced to (= 3 4) before it reduced to nil. When debugging why the
355test failed, it helps to know that the function @code{+} returned 3
356here. ERT records the return value for any predicate called directly
357within @code{should}.
358
359In addition to @code{should}, ERT provides @code{should-not}, which
360checks that the predicate returns nil, and @code{should-error}, which
361checks that the form called within it signals an error. An example
362use 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
370This checks that dividing one by zero signals an error of type
371@code{arith-error}. The @code{:type} argument to @code{should-error}
372is optional; if absent, any type of error is accepted.
373@code{should-error} returns an error description of the error that was
374signalled, to allow additional checks to be made. The error
375description has the format @code{(ERROR-SYMBOL . DATA)}.
376
377There is no @code{should-not-error} macro since tests that signal an
378error fail anyway, so @code{should-not-error} is effectively the
379default.
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
388Some 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
390fails, ERT will alert you of this failure every time you run all
391tests. For known bugs, this alert is a distraction. The way to
392suppress it is to add @code{:expected-result :failed} to the test
393definition:
394
395@lisp
396(ert-deftest future-bug ()
397 "Test `time-forward' with negative arguments.
398Since this functionality isn't implemented yet, the test is known to fail."
399 :expected-result :failed
400 (time-forward -1))
401@end lisp
402
403ERT will still display a small @code{f} in the progress bar as a
404reminder that there is a known bug, and will count the test as failed,
405but it will be quiet about it otherwise.
406
407An alternative to marking the test as a known failure this way is to
408delete the test. This is a good idea if there is no intent to fix it,
409i.e., if the behavior that was formerly considered a bug has become an
410accepted feature.
411
412In general, however, it can be useful to keep tests that are known to
413fail. If someone wants to fix the bug, they will have a very good
414starting point: an automated test case that reproduces the bug. This
415makes it much easier to fix the bug, demonstrate that it is fixed, and
416prevent future regressions.
417
418ERT displays the same kind of alerts for tests that pass unexpectedly
419that it displays for unexpected failures. This way, if you make code
420changes that happen to fix a bug that you weren't aware of, you will
421know to remove the @code{:expected-result} clause of that test and
422close the corresponding bug report, if any.
423
424Since @code{:expected-result} evaluates its argument when the test is
425loaded, tests can be marked as known failures only on certain Emacs
426versions, 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
441The outcome of running a test should not depend on the current state
442of the environment, and each test should leave its environment in the
443same state it found it in. In particular, a test should not depend on
444any Emacs customization variables or hooks, and if it has to make any
445changes to Emacs' state or state external to Emacs such as the file
446system, it should undo these changes before it returns, regardless of
447whether it passed or failed.
448
449Tests should not depend on the environment because any such
450dependencies can make the test brittle or lead to failures that occur
451only under certain circumstances and are hard to reproduce. Of
452course, the code under test may have settings that affect its
453behavior. In that case, it is best to make the test @code{let}-bind
454all such settings variables to set up a specific configuration for the
455duration of the test. The test can also set up a number of different
456configurations and run the code under test with each.
457
458Tests that have side effects on their environment should restore it to
459its original state because any side effects that persist after the
460test can disrupt the workflow of the programmer running the tests. If
461the code under test has side effects on Emacs' current state, such as
462on the current buffer or window configuration, the test should create
463a 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
466the state that can not be preserved with such macros, cleanup should
467be performed with @code{unwind-protect}, to ensure that the cleanup
468occurs even if the test fails.
469
470An exception to this are messages that the code under test prints with
471@code{message} and similar logging; tests should not bother restoring
472the @code{*Message*} buffer to its original state.
473
474The above guidelines imply that tests should avoid calling highly
475customizable commands such as @code{find-file}, except, of course, if
476such 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
480its behavior can be affected by so many external factors. Also,
481@code{find-file} has side effects that are hard to predict and thus
482hard to undo: It may create a new buffer or may reuse an existing
483buffer if one is already visiting the requested file; and it runs
484@code{find-file-hook}, which can have arbitrary side effects.
485
486Instead, it is better to use lower-level mechanisms with simple and
487predictable semantics like @code{with-temp-buffer}, @code{insert} or
488@code{insert-file-contents-literally}, and activating the desired mode
489by calling the corresponding function directly --- after binding the
490hook 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
496Testing simple functions that have no side effects and no dependencies
497on 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
509This test calls the function @code{ert--mismatch} several times with
510various combinations of arguments and compares the return value to the
511expected return value. (Some programmers prefer @code{(should (eql
512EXPECTED ACTUAL))} over the @code{(should (eql ACTUAL EXPECTED))}
513shown here. ERT works either way.)
514
515Here'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
530This test creates a test object using @code{make-ert-test} whose body
531will immediately signal failure. It then runs that test and asserts
532that it fails. Then, it creates a temporary buffer and invokes
533@code{ert--print-backtrace} to print the backtrace of the failed test
534to the current buffer. Finally, it extracts the first line from the
535buffer and asserts that it matches what we expect. It uses
536@code{buffer-substring-no-properties} and @code{equal} to ignore text
537properties; for a test that takes properties into account,
538@code{buffer-substring} and @code{ert-equal-including-properties}
539could be used instead.
540
541The reason why this test only checks the first line of the backtrace
542is that the remainder of the backtrace is dependent on ERT's internals
543as well as whether the code is running interpreted or compiled. By
544looking only at the first line, the test checks a useful property
545--- that the backtrace correctly captures the call to @code{signal} that
546results from the call to @code{ert-fail} --- without being brittle.
547
548This example also shows that writing tests is much easier if the code
549under test was structured with testing in mind.
550
551For example, if @code{ert-run-test} accepted only symbols that name
552tests rather than test objects, the test would need a name for the
553failing test, which would have to be a temporary symbol generated with
554@code{make-symbol}, to avoid side effects on Emacs' state. Choosing
555the right interface for @code{ert-run-tests} allows the test to be
556simpler.
557
558Similarly, if @code{ert--print-backtrace} printed the backtrace to a
559buffer with a fixed name rather than the current buffer, it would be
560much harder for the test to undo the side effect. Of course, some
561code somewhere needs to pick the buffer name. But that logic is
562independent of the logic that prints backtraces, and keeping them in
563separate functions allows us to test them independently.
564
565A lot of code that you will encounter in Emacs was not written with
566testing in mind. Sometimes, the easiest way to write tests for such
567code is to restructure the code slightly to provide better interfaces
568for testing. Usually, this makes the interfaces easier to use as
569well.
570
571
572@node How to Debug Tests, Extending ERT, How to Write Tests, Top
573@chapter How to Debug Tests
574
575This section describes how to use ERT's features to understand why
576a 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
588Failed @code{should} forms are reported like this:
589
590@example
591F 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
602ERT shows what the @code{should} expression looked like and what
603values its subexpressions had: The source code of the assertion was
604@code{(should (= (+ 1 2) 4))}, which applied the function @code{=} to
605the arguments @code{3} and @code{4}, resulting in the value
606@code{nil}. In this case, the test is wrong; it should expect 3
607rather than 4.
608
609If a predicate like @code{equal} is used with @code{should}, ERT
610provides a so-called @emph{explanation}:
611
612@example
613F 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
628In 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
630the item at index 2 differs between the two lists; in one list, it is
631the atom c, in the other, it is the atom d.
632
633In simple examples like the above, the explanation is unnecessary.
634But in cases where the difference is not immediately apparent, it can
635save time:
636
637@example
638F 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
648ERT only provides explanations for predicates that have an explanation
649function registered. @xref{Defining Explanation Functions}.
650
651
652@node Interactive Debugging, , Understanding Explanations, How to Debug Tests
653@section Interactive Debugging
654
655Debugging failed tests works essentially the same way as debugging any
656other problems with Lisp code. Here are a few tricks specific to
657tests:
658
659@itemize
660@item Re-run the failed test a few times to see if it fails in the same way
661each time. It's good to find out whether the behavior is
662deterministic before spending any time looking for a cause. In the
663ERT 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
666exactly it does. Perhaps the test is broken rather than the code
667under test.
668
669@item If the test contains a series of @code{should} forms and you can't
670tell 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
674the test with debugging enabled, this will enter the debugger and show
675the backtrace as well; but the top few frames shown there will not be
676relevant to you since they are ERT's own debugger hook. @kbd{b}
677strips 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
681failed. 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
685type @kbd{@kbd{C-u} @kbd{C-M-x}}. Then, go back to the ERT buffer and
686re-run the test with @kbd{r} or @kbd{d}.
687
688@item If you have been editing and rearranging tests, it is possible that
689ERT remembers an old test that you have since renamed or removed ---
690renamings or removals of definitions in the source code leave around a
691stray definition under the old name in the running process, this is a
692common problem in Lisp. In such a situation, hit @kbd{D} to let ERT
693forget 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
700There 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
711The explanation function for a predicate is a function that takes the
712same arguments as the predicate and returns an @emph{explanation}.
713The explanation should explain why the predicate, when invoked with
714the arguments given to the explanation function, returns the value
715that it returns. The explanation can be any object but should have a
716comprehensible printed representation. If the return value of the
717predicate needs no explanation for a given list of arguments, the
718explanation function should return nil.
719
720To associate an explanation function with a predicate, add the
721property @code{ert-explainer} to the symbol that names the predicate.
722The value of the property should be the symbol that names the
723explanation 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
729Both @code{ert-run-tests-interactively} and @code{ert-run-tests-batch}
730are implemented on top of the lower-level test handling code in the
731sections named ``Facilities for running a single test'', ``Test
732selectors'', and ``Facilities for running a whole set of tests''.
733
734If you want to write code that works with ERT tests, you should take a
735look at this lower-level code. Symbols that start with @code{ert--}
736are 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
738mature API yet.
739
740Contributions to ERT are welcome.
741
742
743@node Other Testing Concepts, , Extending ERT, Top
744@chapter Other Testing Concepts
745
746For 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
757Stubbing out functions or using so-called @emph{mocks} can make it
758easier to write tests. See
759@url{http://en.wikipedia.org/wiki/Mock_object} for an explanation of
760the corresponding concepts in object-oriented languages.
761
762ERT 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})
764offers 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
770In many ways, ERT is similar to frameworks for other languages like
771SUnit or JUnit. However, two features commonly found in such
772frameworks are notably absent from ERT: fixtures and test suites.
773
774Fixtures, as used e.g. in SUnit or JUnit, are mainly used to provide
775an environment for a set of tests, and consist of set-up and tear-down
776functions.
777
778While fixtures are a useful syntactic simplification in other
779languages, this does not apply to Lisp, where higher-order functions
780and `unwind-protect' are available. One way to implement and use a
781fixture 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
797the set-up and tear-down part, and additionally allows any test
798to use any combination of fixtures, so it is more flexible than what
799other tools typically allow.
800
801If the test needs access to the environment the fixture sets up, the
802fixture can be modified to pass arguments to the body.
803
804These are well-known Lisp techniques. Special syntax for them could
805be added but would provide only a minor simplification.
806
807(If you are interested in such syntax, note that splitting set-up and
808tear-down into separate functions, like *Unit tools usually do, makes
809it impossible to establish dynamic `let' bindings as part of the
810fixture. So, blindly imitating the way fixtures are implemented in
811other languages would be counter-productive in Lisp.)
812
813The purpose of test suites is to group related tests together.
814
815The most common use of this is to run just the tests for one
816particular module. Since symbol prefixes are the usual way of
817separating module namespaces in Emacs Lisp, test selectors already
818solve this by allowing regexp matching on test names; e.g., the
819selector "^ert-" selects ERT's self-tests.
820
821Other uses include grouping tests by their expected execution time to
822run quick tests during interactive development and slow tests less
823frequently. 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
diff --git a/doc/misc/makefile.w32-in b/doc/misc/makefile.w32-in
index fd3b1476b55..e5150e3b122 100644
--- a/doc/misc/makefile.w32-in
+++ b/doc/misc/makefile.w32-in
@@ -47,7 +47,8 @@ INFO_TARGETS = $(infodir)/ccmode \
47 $(infodir)/org $(infodir)/url $(infodir)/speedbar \ 47 $(infodir)/org $(infodir)/url $(infodir)/speedbar \
48 $(infodir)/tramp $(infodir)/ses $(infodir)/smtpmail \ 48 $(infodir)/tramp $(infodir)/ses $(infodir)/smtpmail \
49 $(infodir)/flymake $(infodir)/newsticker $(infodir)/rcirc \ 49 $(infodir)/flymake $(infodir)/newsticker $(infodir)/rcirc \
50 $(infodir)/erc $(infodir)/remember $(infodir)/nxml-mode \ 50 $(infodir)/erc $(infodir)/ert \
51 $(infodir)/remember $(infodir)/nxml-mode \
51 $(infodir)/epa $(infodir)/mairix-el $(infodir)/sasl \ 52 $(infodir)/epa $(infodir)/mairix-el $(infodir)/sasl \
52 $(infodir)/auth $(infodir)/eieio $(infodir)/ede \ 53 $(infodir)/auth $(infodir)/eieio $(infodir)/ede \
53 $(infodir)/semantic $(infodir)/edt 54 $(infodir)/semantic $(infodir)/edt
@@ -58,7 +59,8 @@ DVI_TARGETS = calc.dvi cc-mode.dvi cl.dvi dbus.dvi dired-x.dvi \
58 ada-mode.dvi autotype.dvi idlwave.dvi eudc.dvi ebrowse.dvi \ 59 ada-mode.dvi autotype.dvi idlwave.dvi eudc.dvi ebrowse.dvi \
59 pcl-cvs.dvi woman.dvi eshell.dvi org.dvi url.dvi \ 60 pcl-cvs.dvi woman.dvi eshell.dvi org.dvi url.dvi \
60 speedbar.dvi tramp.dvi ses.dvi smtpmail.dvi flymake.dvi \ 61 speedbar.dvi tramp.dvi ses.dvi smtpmail.dvi flymake.dvi \
61 newsticker.dvi rcirc.dvi erc.dvi remember.dvi nxml-mode.dvi \ 62 newsticker.dvi rcirc.dvi erc.dvi ert.dvi \
63 remember.dvi nxml-mode.dvi \
62 epa.dvi mairix-el.dvi sasl.dvi auth.dvi eieio.dvi ede.dvi \ 64 epa.dvi mairix-el.dvi sasl.dvi auth.dvi eieio.dvi ede.dvi \
63 semantic.dvi edt.dvi 65 semantic.dvi edt.dvi
64INFOSOURCES = info.texi 66INFOSOURCES = info.texi
@@ -305,6 +307,11 @@ $(infodir)/erc: erc.texi
305erc.dvi: erc.texi 307erc.dvi: erc.texi
306 $(ENVADD) $(TEXI2DVI) $(srcdir)/erc.texi 308 $(ENVADD) $(TEXI2DVI) $(srcdir)/erc.texi
307 309
310$(infodir)/ert: ert.texi
311 $(MAKEINFO) ert.texi
312ert.dvi: ert.texi
313 $(ENVADD) $(TEXI2DVI) $(srcdir)/ert.texi
314
308$(infodir)/epa: epa.texi 315$(infodir)/epa: epa.texi
309 $(MAKEINFO) epa.texi 316 $(MAKEINFO) epa.texi
310epa.dvi: epa.texi 317epa.dvi: epa.texi
@@ -362,7 +369,7 @@ clean: mostlyclean
362 $(infodir)/url* $(infodir)/org* \ 369 $(infodir)/url* $(infodir)/org* \
363 $(infodir)/flymake* $(infodir)/newsticker* \ 370 $(infodir)/flymake* $(infodir)/newsticker* \
364 $(infodir)/sieve* $(infodir)/pgg* \ 371 $(infodir)/sieve* $(infodir)/pgg* \
365 $(infodir)/erc* $(infodir)/rcirc* \ 372 $(infodir)/erc* $(infodir)/ert* $(infodir)/rcirc* \
366 $(infodir)/remember* $(infodir)/nxml-mode* \ 373 $(infodir)/remember* $(infodir)/nxml-mode* \
367 $(infodir)/epa* $(infodir)/sasl* \ 374 $(infodir)/epa* $(infodir)/sasl* \
368 $(infodir)/mairix-el* $(infodir)/auth* \ 375 $(infodir)/mairix-el* $(infodir)/auth* \