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