ert.texi 33 KB

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