2011-01-12 16:08:24 +00:00
|
|
|
\input texinfo
|
|
|
|
@c %**start of header
|
2014-06-10 02:20:31 +00:00
|
|
|
@setfilename ../../info/ert.info
|
2011-01-12 16:08:24 +00:00
|
|
|
@settitle Emacs Lisp Regression Testing
|
Fix single-quoting style in PDF manuals
The PDF versions of the GNU manuals used curved single quotes to
represent grave accent and apostrophe, which made it a pain to cut
and paste code examples from them. Fix the PDF versions to use
grave accent and apostrophe for Lisp source code, keystrokes, etc.
This change does not affect the info files, nor does it affect
ordinary uses of curved single quotes in PDF.
* doc/emacs/docstyle.texi: New file, which specifies treatment for
grave accent and apostrophe, as well as the document encoding.
* doc/emacs/emacs-xtra.texi, doc/emacs/emacs.texi:
* doc/lispintro/emacs-lisp-intro.texi:
* doc/lispref/back.texi, doc/lispref/book-spine.texi:
* doc/lispref/elisp.texi, doc/lispref/lay-flat.texi:
* doc/misc/ada-mode.texi, doc/misc/auth.texi:
* doc/misc/autotype.texi, doc/misc/bovine.texi, doc/misc/calc.texi:
* doc/misc/cc-mode.texi, doc/misc/cl.texi, doc/misc/dbus.texi:
* doc/misc/dired-x.texi, doc/misc/ebrowse.texi, doc/misc/ede.texi:
* doc/misc/ediff.texi, doc/misc/edt.texi, doc/misc/efaq-w32.texi:
* doc/misc/efaq.texi, doc/misc/eieio.texi, doc/misc/emacs-gnutls.texi:
* doc/misc/emacs-mime.texi, doc/misc/epa.texi, doc/misc/erc.texi:
* doc/misc/ert.texi, doc/misc/eshell.texi, doc/misc/eudc.texi:
* doc/misc/eww.texi, doc/misc/flymake.texi, doc/misc/forms.texi:
* doc/misc/gnus-coding.texi, doc/misc/gnus-faq.texi:
* doc/misc/gnus.texi, doc/misc/htmlfontify.texi:
* doc/misc/idlwave.texi, doc/misc/ido.texi, doc/misc/info.texi:
* doc/misc/mairix-el.texi, doc/misc/message.texi, doc/misc/mh-e.texi:
* doc/misc/newsticker.texi, doc/misc/nxml-mode.texi:
* doc/misc/octave-mode.texi, doc/misc/org.texi, doc/misc/pcl-cvs.texi:
* doc/misc/pgg.texi, doc/misc/rcirc.texi, doc/misc/reftex.texi:
* doc/misc/remember.texi, doc/misc/sasl.texi, doc/misc/sc.texi:
* doc/misc/semantic.texi, doc/misc/ses.texi, doc/misc/sieve.texi:
* doc/misc/smtpmail.texi, doc/misc/speedbar.texi:
* doc/misc/srecode.texi, doc/misc/todo-mode.texi, doc/misc/tramp.texi:
* doc/misc/url.texi, doc/misc/vhdl-mode.texi, doc/misc/vip.texi:
* doc/misc/viper.texi, doc/misc/widget.texi, doc/misc/wisent.texi:
* doc/misc/woman.texi:
Use it instead of '@documentencoding UTF-8', to lessen the need for
global changes like this in the future.
* doc/emacs/Makefile.in (EMACS_XTRA):
* doc/lispintro/Makefile.in (srcs):
* doc/lispref/Makefile.in (srcs):
Add dependency on docstyle.texi.
* doc/misc/Makefile.in (style): New macro.
(${buildinfodir}/%.info, %.dvi, %.pdf, %.html)
(${buildinfodir}/ccmode.info, ${buildinfodir}/efaq%.info, gnus_deps):
Use it.
2015-05-01 19:05:53 +00:00
|
|
|
@include docstyle.texi
|
2011-01-12 16:08:24 +00:00
|
|
|
@c %**end of header
|
|
|
|
|
2012-05-02 01:22:26 +00:00
|
|
|
@dircategory Emacs misc features
|
2011-01-12 16:08:24 +00:00
|
|
|
@direntry
|
2013-12-12 03:37:38 +00:00
|
|
|
* ERT: (ert). Emacs Lisp regression testing tool.
|
2011-01-12 16:08:24 +00:00
|
|
|
@end direntry
|
|
|
|
|
|
|
|
@copying
|
2016-01-01 09:38:07 +00:00
|
|
|
Copyright @copyright{} 2008, 2010--2016 Free Software Foundation, Inc.
|
2011-01-12 16:08:24 +00:00
|
|
|
|
|
|
|
@quotation
|
|
|
|
Permission is granted to copy, distribute and/or modify this document
|
2011-01-18 08:16:08 +00:00
|
|
|
under the terms of the GNU Free Documentation License, Version 1.3 or
|
2011-01-12 16:08:24 +00:00
|
|
|
any later version published by the Free Software Foundation; with no
|
2014-05-07 17:34:53 +00:00
|
|
|
Invariant Sections, with the Front-Cover Texts being ``A GNU Manual,''
|
2011-01-18 08:16:08 +00:00
|
|
|
and with the Back-Cover Texts as in (a) below. A copy of the license
|
2012-12-21 19:01:24 +00:00
|
|
|
is included in the section entitled ``GNU Free Documentation License''.
|
2011-01-18 08:16:08 +00:00
|
|
|
|
|
|
|
(a) The FSF's Back-Cover Text is: ``You have the freedom to copy and
|
2012-12-21 18:36:41 +00:00
|
|
|
modify this GNU manual.''
|
2011-01-12 16:08:24 +00:00
|
|
|
@end quotation
|
|
|
|
@end copying
|
|
|
|
|
2014-01-03 03:00:39 +00:00
|
|
|
@titlepage
|
|
|
|
@title Emacs Lisp Regression Testing
|
|
|
|
@page
|
|
|
|
@vskip 0pt plus 1filll
|
|
|
|
@insertcopying
|
|
|
|
@end titlepage
|
|
|
|
|
|
|
|
@contents
|
|
|
|
|
|
|
|
@ifnottex
|
2012-12-22 19:49:54 +00:00
|
|
|
@node Top
|
2011-01-12 16:08:24 +00:00
|
|
|
@top ERT: Emacs Lisp Regression Testing
|
|
|
|
|
2014-01-03 03:00:39 +00:00
|
|
|
@insertcopying
|
|
|
|
|
2011-01-12 16:08:24 +00:00
|
|
|
ERT is a tool for automated testing in Emacs Lisp. Its main features
|
|
|
|
are facilities for defining tests, running them and reporting the
|
|
|
|
results, and for debugging test failures interactively.
|
|
|
|
|
|
|
|
ERT is similar to tools for other environments such as JUnit, but has
|
|
|
|
unique features that take advantage of the dynamic and interactive
|
|
|
|
nature of Emacs. Despite its name, it works well both for test-driven
|
|
|
|
development (see
|
|
|
|
@url{http://en.wikipedia.org/wiki/Test-driven_development}) and for
|
|
|
|
traditional software development methods.
|
|
|
|
|
|
|
|
@menu
|
|
|
|
* Introduction:: A simple example of an ERT test.
|
2011-10-12 04:37:25 +00:00
|
|
|
* How to Run Tests:: Run tests in Emacs or from the command line.
|
2011-01-12 16:08:24 +00:00
|
|
|
* How to Write Tests:: How to add tests to your Emacs Lisp code.
|
|
|
|
* How to Debug Tests:: What to do if a test fails.
|
|
|
|
* Extending ERT:: ERT is extensible in several ways.
|
|
|
|
* Other Testing Concepts:: Features not in ERT.
|
2012-12-21 19:01:24 +00:00
|
|
|
* GNU Free Documentation License:: The license for this documentation.
|
2011-01-12 16:08:24 +00:00
|
|
|
|
|
|
|
@detailmenu
|
|
|
|
--- The Detailed Node Listing ---
|
|
|
|
|
|
|
|
How to Run Tests
|
|
|
|
|
|
|
|
* Running Tests Interactively:: Run tests in your current Emacs.
|
|
|
|
* Running Tests in Batch Mode:: Run tests in emacs -Q.
|
2011-10-12 04:37:25 +00:00
|
|
|
* Test Selectors:: Choose which tests to run.
|
2011-01-12 16:08:24 +00:00
|
|
|
|
|
|
|
How to Write Tests
|
|
|
|
|
2011-10-12 04:37:25 +00:00
|
|
|
* The @code{should} Macro:: A powerful way to express assertions.
|
2011-01-12 16:08:24 +00:00
|
|
|
* Expected Failures:: Tests for known bugs.
|
2011-10-12 04:37:25 +00:00
|
|
|
* Tests and Their Environment:: Don't depend on customizations; no side effects.
|
2011-01-12 16:08:24 +00:00
|
|
|
* Useful Techniques:: Some examples.
|
|
|
|
|
|
|
|
How to Debug Tests
|
|
|
|
|
|
|
|
* Understanding Explanations:: How ERT gives details on why an assertion failed.
|
|
|
|
* Interactive Debugging:: Tools available in the ERT results buffer.
|
|
|
|
|
|
|
|
Extending ERT
|
|
|
|
|
|
|
|
* Defining Explanation Functions:: Teach ERT about more predicates.
|
|
|
|
* Low-Level Functions for Working with Tests:: Use ERT's data for your purposes.
|
|
|
|
|
|
|
|
Other Testing Concepts
|
|
|
|
|
2011-10-14 07:16:56 +00:00
|
|
|
* Mocks and Stubs:: Stubbing out code that is irrelevant to the test.
|
|
|
|
* Fixtures and Test Suites:: How ERT differs from tools for other languages.
|
2011-01-12 16:08:24 +00:00
|
|
|
|
2012-12-21 19:01:24 +00:00
|
|
|
Appendix
|
|
|
|
|
|
|
|
* GNU Free Documentation License:: The license for this documentation.
|
|
|
|
|
2011-01-12 16:08:24 +00:00
|
|
|
@end detailmenu
|
|
|
|
@end menu
|
2014-01-03 03:00:39 +00:00
|
|
|
@end ifnottex
|
2011-01-12 16:08:24 +00:00
|
|
|
|
2012-12-22 19:49:54 +00:00
|
|
|
@node Introduction
|
2011-01-12 16:08:24 +00:00
|
|
|
@chapter Introduction
|
|
|
|
|
|
|
|
ERT allows you to define @emph{tests} in addition to functions,
|
|
|
|
macros, variables, and the other usual Lisp constructs. Tests are
|
2012-12-22 16:25:40 +00:00
|
|
|
simply Lisp code: code that invokes other code and checks whether
|
2011-01-12 16:08:24 +00:00
|
|
|
it behaves as expected.
|
|
|
|
|
|
|
|
ERT keeps track of the tests that are defined and provides convenient
|
|
|
|
commands to run them to verify whether the definitions that are
|
|
|
|
currently loaded in Emacs pass the tests.
|
|
|
|
|
|
|
|
Some Lisp files have comments like the following (adapted from the
|
|
|
|
package @code{pp.el}):
|
|
|
|
|
|
|
|
@lisp
|
|
|
|
;; (pp-to-string '(quote quote)) ; expected: "'quote"
|
|
|
|
;; (pp-to-string '((quote a) (quote b))) ; expected: "('a 'b)\n"
|
|
|
|
;; (pp-to-string '('a 'b)) ; same as above
|
|
|
|
@end lisp
|
|
|
|
|
|
|
|
The code contained in these comments can be evaluated from time to
|
|
|
|
time to compare the output with the expected output. ERT formalizes
|
|
|
|
this and introduces a common convention, which simplifies Emacs
|
|
|
|
development, since programmers no longer have to manually find and
|
|
|
|
evaluate such comments.
|
|
|
|
|
|
|
|
An ERT test definition equivalent to the above comments is this:
|
|
|
|
|
|
|
|
@lisp
|
|
|
|
(ert-deftest pp-test-quote ()
|
|
|
|
"Tests the rendering of `quote' symbols in `pp-to-string'."
|
|
|
|
(should (equal (pp-to-string '(quote quote)) "'quote"))
|
|
|
|
(should (equal (pp-to-string '((quote a) (quote b))) "('a 'b)\n"))
|
|
|
|
(should (equal (pp-to-string '('a 'b)) "('a 'b)\n")))
|
|
|
|
@end lisp
|
|
|
|
|
|
|
|
If you know @code{defun}, the syntax of @code{ert-deftest} should look
|
|
|
|
familiar: This example defines a test named @code{pp-test-quote} that
|
2014-06-08 23:39:23 +00:00
|
|
|
will pass if the three calls to @code{equal} all return non-@code{nil}.
|
2011-01-12 16:08:24 +00:00
|
|
|
|
2012-10-31 21:02:51 +00:00
|
|
|
@code{should} is a macro with the same meaning as @code{cl-assert} but
|
2011-01-12 16:08:24 +00:00
|
|
|
better error reporting. @xref{The @code{should} Macro}.
|
|
|
|
|
2011-10-14 07:16:56 +00:00
|
|
|
Each test should have a name that describes what functionality it tests.
|
2012-12-22 16:25:40 +00:00
|
|
|
Test names can be chosen arbitrarily---they are in a
|
|
|
|
namespace separate from functions and variables---but should follow
|
2011-01-12 16:08:24 +00:00
|
|
|
the usual Emacs Lisp convention of having a prefix that indicates
|
|
|
|
which package they belong to. Test names are displayed by ERT when
|
|
|
|
reporting failures and can be used when selecting which tests to run.
|
|
|
|
|
|
|
|
The empty parentheses @code{()} in the first line don't currently have
|
|
|
|
any meaning and are reserved for future extension. They also make
|
2011-10-14 07:16:56 +00:00
|
|
|
the syntax of @code{ert-deftest} more similar to that of @code{defun}.
|
2011-01-12 16:08:24 +00:00
|
|
|
|
|
|
|
The docstring describes what feature this test tests. When running
|
|
|
|
tests interactively, the first line of the docstring is displayed for
|
|
|
|
tests that fail, so it is good if the first line makes sense on its
|
|
|
|
own.
|
|
|
|
|
|
|
|
The body of a test can be arbitrary Lisp code. It should have as few
|
|
|
|
side effects as possible; each test should be written to clean up
|
|
|
|
after itself, leaving Emacs in the same state as it was before the
|
|
|
|
test. Tests should clean up even if they fail. @xref{Tests and Their
|
|
|
|
Environment}.
|
|
|
|
|
|
|
|
|
2012-12-22 19:49:54 +00:00
|
|
|
@node How to Run Tests
|
2011-01-12 16:08:24 +00:00
|
|
|
@chapter How to Run Tests
|
|
|
|
|
|
|
|
You can run tests either in the Emacs you are working in, or on the
|
|
|
|
command line in a separate Emacs process in batch mode (i.e., with no
|
|
|
|
user interface). The former mode is convenient during interactive
|
|
|
|
development, the latter is useful to make sure that tests pass
|
2011-10-14 07:16:56 +00:00
|
|
|
independently of your customizations; and it allows you to invoke
|
|
|
|
tests from makefiles, and to write scripts that run tests in several
|
2011-01-12 16:08:24 +00:00
|
|
|
different Emacs versions.
|
|
|
|
|
|
|
|
@menu
|
|
|
|
* Running Tests Interactively:: Run tests in your current Emacs.
|
|
|
|
* Running Tests in Batch Mode:: Run tests in emacs -Q.
|
2011-10-14 07:16:56 +00:00
|
|
|
* Test Selectors:: Choose which tests to run.
|
2011-01-12 16:08:24 +00:00
|
|
|
@end menu
|
|
|
|
|
|
|
|
|
2012-12-22 19:49:54 +00:00
|
|
|
@node Running Tests Interactively
|
2011-01-12 16:08:24 +00:00
|
|
|
@section Running Tests Interactively
|
|
|
|
|
|
|
|
You can run the tests that are currently defined in your Emacs with
|
2011-10-14 07:16:56 +00:00
|
|
|
the command @kbd{@kbd{M-x} ert @kbd{RET} t @kbd{RET}}. (For an
|
|
|
|
explanation of the @code{t} argument, @pxref{Test Selectors}.) ERT will pop
|
2011-01-12 16:08:24 +00:00
|
|
|
up a new buffer, the ERT results buffer, showing the results of the
|
|
|
|
tests run. It looks like this:
|
|
|
|
|
|
|
|
@example
|
|
|
|
Selector: t
|
2013-10-24 07:40:05 +00:00
|
|
|
Passed: 31
|
|
|
|
Skipped: 0
|
|
|
|
Failed: 2 (2 unexpected)
|
|
|
|
Total: 33/33
|
2011-01-12 16:08:24 +00:00
|
|
|
|
|
|
|
Started at: 2008-09-11 08:39:25-0700
|
|
|
|
Finished.
|
|
|
|
Finished at: 2008-09-11 08:39:27-0700
|
|
|
|
|
|
|
|
FF...............................
|
|
|
|
|
|
|
|
F addition-test
|
|
|
|
(ert-test-failed
|
|
|
|
((should
|
|
|
|
(=
|
|
|
|
(+ 1 2)
|
|
|
|
4))
|
|
|
|
:form
|
|
|
|
(= 3 4)
|
|
|
|
:value nil))
|
|
|
|
|
|
|
|
F list-test
|
|
|
|
(ert-test-failed
|
|
|
|
((should
|
|
|
|
(equal
|
|
|
|
(list 'a 'b 'c)
|
|
|
|
'(a b d)))
|
|
|
|
:form
|
|
|
|
(equal
|
|
|
|
(a b c)
|
|
|
|
(a b d))
|
|
|
|
:value nil :explanation
|
|
|
|
(list-elt 2
|
|
|
|
(different-atoms c d))))
|
|
|
|
@end example
|
|
|
|
|
2011-10-14 07:16:56 +00:00
|
|
|
At the top, there is a summary of the results: we ran all tests defined
|
|
|
|
in the current Emacs (@code{Selector: t}), 31 of them passed, and 2
|
|
|
|
failed unexpectedly. @xref{Expected Failures}, for an explanation of
|
|
|
|
the term @emph{unexpected} in this context.
|
2011-01-12 16:08:24 +00:00
|
|
|
|
|
|
|
The line of dots and @code{F}s is a progress bar where each character
|
|
|
|
represents one test; it fills while the tests are running. A dot
|
|
|
|
means that the test passed, an @code{F} means that it failed. Below
|
|
|
|
the progress bar, ERT shows details about each test that had an
|
|
|
|
unexpected result. In the example above, there are two failures, both
|
|
|
|
due to failed @code{should} forms. @xref{Understanding Explanations},
|
|
|
|
for more details.
|
|
|
|
|
|
|
|
In the ERT results buffer, @kbd{TAB} and @kbd{S-TAB} cycle between
|
|
|
|
buttons. Each name of a function or macro in this buffer is a button;
|
|
|
|
moving point to it and typing @kbd{RET} jumps to its definition.
|
|
|
|
|
|
|
|
Pressing @kbd{r} re-runs the test near point on its own. Pressing
|
|
|
|
@kbd{d} re-runs it with the debugger enabled. @kbd{.} jumps to the
|
|
|
|
definition of the test near point (@kbd{RET} has the same effect if
|
|
|
|
point is on the name of the test). On a failed test, @kbd{b} shows
|
|
|
|
the backtrace of the failure.
|
|
|
|
|
|
|
|
@kbd{l} shows the list of @code{should} forms executed in the test.
|
|
|
|
If any messages were generated (with the Lisp function @code{message})
|
|
|
|
in a test or any of the code that it invoked, @kbd{m} will show them.
|
|
|
|
|
|
|
|
By default, long expressions in the failure details are abbreviated
|
|
|
|
using @code{print-length} and @code{print-level}. Pressing @kbd{L}
|
|
|
|
while point is on a test failure will increase the limits to show more
|
|
|
|
of the expression.
|
|
|
|
|
|
|
|
|
2012-12-22 19:49:54 +00:00
|
|
|
@node Running Tests in Batch Mode
|
2011-01-12 16:08:24 +00:00
|
|
|
@section Running Tests in Batch Mode
|
|
|
|
|
|
|
|
ERT supports automated invocations from the command line or from
|
|
|
|
scripts or makefiles. There are two functions for this purpose,
|
|
|
|
@code{ert-run-tests-batch} and @code{ert-run-tests-batch-and-exit}.
|
|
|
|
They can be used like this:
|
|
|
|
|
|
|
|
@example
|
2011-10-14 07:16:56 +00:00
|
|
|
emacs -batch -l ert -l my-tests.el -f ert-run-tests-batch-and-exit
|
2011-01-12 16:08:24 +00:00
|
|
|
@end example
|
|
|
|
|
|
|
|
This command will start up Emacs in batch mode, load ERT, load
|
|
|
|
@code{my-tests.el}, and run all tests defined in it. It will exit
|
|
|
|
with a zero exit status if all tests passed, or nonzero if any tests
|
|
|
|
failed or if anything else went wrong. It will also print progress
|
|
|
|
messages and error diagnostics to standard output.
|
|
|
|
|
2015-12-26 11:05:06 +00:00
|
|
|
You can also redirect the above output to a log file, say
|
|
|
|
@file{output.log}, and use the
|
|
|
|
@code{ert-summarize-tests-batch-and-exit} function to produce a neat
|
|
|
|
summary as shown below:
|
|
|
|
|
|
|
|
@example
|
|
|
|
emacs -batch -l ert -f ert-summarize-tests-batch-and-exit output.log
|
|
|
|
@end example
|
|
|
|
|
2011-10-14 07:16:56 +00:00
|
|
|
If ERT is not part of your Emacs distribution, you may need to use
|
|
|
|
@code{-L /path/to/ert/} so that Emacs can find it. You may need
|
|
|
|
additional @code{-L} flags to ensure that @code{my-tests.el} and all the
|
|
|
|
files that it requires are on your @code{load-path}.
|
2011-01-12 16:08:24 +00:00
|
|
|
|
|
|
|
|
2012-12-22 19:49:54 +00:00
|
|
|
@node Test Selectors
|
2011-01-12 16:08:24 +00:00
|
|
|
@section Test Selectors
|
|
|
|
|
|
|
|
Functions like @code{ert} accept a @emph{test selector}, a Lisp
|
|
|
|
expression specifying a set of tests. Test selector syntax is similar
|
|
|
|
to Common Lisp's type specifier syntax:
|
|
|
|
|
|
|
|
@itemize
|
|
|
|
@item @code{nil} selects no tests.
|
|
|
|
@item @code{t} selects all tests.
|
|
|
|
@item @code{:new} selects all tests that have not been run yet.
|
|
|
|
@item @code{:failed} and @code{:passed} select tests according to their most recent result.
|
|
|
|
@item @code{:expected}, @code{:unexpected} select tests according to their most recent result.
|
2011-10-14 07:16:56 +00:00
|
|
|
@item A string is a regular expression that selects all tests with matching names.
|
|
|
|
@item A test (i.e., an object of @code{ert-test} data type) selects that test.
|
2011-01-12 16:08:24 +00:00
|
|
|
@item A symbol selects the test that the symbol names.
|
2011-10-14 07:16:56 +00:00
|
|
|
@item @code{(member TESTS...)} selects the elements of TESTS, a list of
|
|
|
|
tests or symbols naming tests.
|
2011-01-12 16:08:24 +00:00
|
|
|
@item @code{(eql TEST)} selects TEST, a test or a symbol naming a test.
|
|
|
|
@item @code{(and SELECTORS...)} selects the tests that match all SELECTORS.
|
|
|
|
@item @code{(or SELECTORS...)} selects the tests that match any SELECTOR.
|
|
|
|
@item @code{(not SELECTOR)} selects all tests that do not match SELECTOR.
|
|
|
|
@item @code{(tag TAG)} selects all tests that have TAG on their tags list.
|
2011-10-14 07:16:56 +00:00
|
|
|
(Tags are optional labels you can apply to tests when you define them.)
|
|
|
|
@item @code{(satisfies PREDICATE)} selects all tests that satisfy PREDICATE,
|
2014-06-08 23:39:23 +00:00
|
|
|
a function that takes a test as argument and returns non-@code{nil} if
|
|
|
|
it is selected.
|
2011-01-12 16:08:24 +00:00
|
|
|
@end itemize
|
|
|
|
|
|
|
|
Selectors that are frequently useful when selecting tests to run
|
|
|
|
include @code{t} to run all tests that are currently defined in Emacs,
|
2011-10-14 07:16:56 +00:00
|
|
|
@code{"^foo-"} to run all tests in package @code{foo} (this assumes
|
|
|
|
that package @code{foo} uses the prefix @code{foo-} for its test names),
|
|
|
|
result-based selectors such as @code{(or :new :unexpected)} to
|
2011-01-12 16:08:24 +00:00
|
|
|
run all tests that have either not run yet or that had an unexpected
|
|
|
|
result in the last run, and tag-based selectors such as @code{(not
|
|
|
|
(tag :causes-redisplay))} to run all tests that are not tagged
|
|
|
|
@code{:causes-redisplay}.
|
|
|
|
|
|
|
|
|
2012-12-22 19:49:54 +00:00
|
|
|
@node How to Write Tests
|
2011-01-12 16:08:24 +00:00
|
|
|
@chapter How to Write Tests
|
|
|
|
|
|
|
|
ERT lets you define tests in the same way you define functions. You
|
|
|
|
can type @code{ert-deftest} forms in a buffer and evaluate them there
|
|
|
|
with @code{eval-defun} or @code{compile-defun}, or you can save the
|
|
|
|
file and load it, optionally byte-compiling it first.
|
|
|
|
|
|
|
|
Just like @code{find-function} is only able to find where a function
|
|
|
|
was defined if the function was loaded from a file, ERT is only able
|
|
|
|
to find where a test was defined if the test was loaded from a file.
|
|
|
|
|
|
|
|
|
|
|
|
@menu
|
2011-10-14 07:16:56 +00:00
|
|
|
* The @code{should} Macro:: A powerful way to express assertions.
|
2011-01-12 16:08:24 +00:00
|
|
|
* Expected Failures:: Tests for known bugs.
|
2011-10-14 07:16:56 +00:00
|
|
|
* Tests and Their Environment:: Don't depend on customizations; no side effects.
|
2011-01-12 16:08:24 +00:00
|
|
|
* Useful Techniques:: Some examples.
|
|
|
|
@end menu
|
|
|
|
|
2012-12-22 19:49:54 +00:00
|
|
|
@node The @code{should} Macro
|
2011-01-12 16:08:24 +00:00
|
|
|
@section The @code{should} Macro
|
|
|
|
|
|
|
|
Test bodies can include arbitrary code; but to be useful, they need to
|
2011-10-14 07:16:56 +00:00
|
|
|
check whether the code being tested (or @emph{code under test})
|
2011-01-12 16:08:24 +00:00
|
|
|
does what it is supposed to do. The macro @code{should} is similar to
|
2012-10-31 21:02:51 +00:00
|
|
|
@code{cl-assert} from the cl package
|
2011-10-14 07:16:56 +00:00
|
|
|
(@pxref{Assertions,,, cl, Common Lisp Extensions}),
|
|
|
|
but analyzes its argument form and records information that ERT can
|
|
|
|
display to help debugging.
|
2011-01-12 16:08:24 +00:00
|
|
|
|
|
|
|
This test definition
|
|
|
|
|
|
|
|
@lisp
|
|
|
|
(ert-deftest addition-test ()
|
|
|
|
(should (= (+ 1 2) 4)))
|
|
|
|
@end lisp
|
|
|
|
|
|
|
|
will produce this output when run via @kbd{M-x ert}:
|
|
|
|
|
|
|
|
@example
|
|
|
|
F addition-test
|
|
|
|
(ert-test-failed
|
|
|
|
((should
|
|
|
|
(=
|
|
|
|
(+ 1 2)
|
|
|
|
4))
|
|
|
|
:form
|
|
|
|
(= 3 4)
|
|
|
|
:value nil))
|
|
|
|
@end example
|
|
|
|
|
|
|
|
In this example, @code{should} recorded the fact that (= (+ 1 2) 4)
|
2014-06-08 23:39:23 +00:00
|
|
|
reduced to (= 3 4) before it reduced to @code{nil}. When debugging why the
|
2011-01-12 16:08:24 +00:00
|
|
|
test failed, it helps to know that the function @code{+} returned 3
|
|
|
|
here. ERT records the return value for any predicate called directly
|
|
|
|
within @code{should}.
|
|
|
|
|
|
|
|
In addition to @code{should}, ERT provides @code{should-not}, which
|
2014-06-08 23:39:23 +00:00
|
|
|
checks that the predicate returns @code{nil}, and @code{should-error}, which
|
2011-01-12 16:08:24 +00:00
|
|
|
checks that the form called within it signals an error. An example
|
|
|
|
use of @code{should-error}:
|
|
|
|
|
|
|
|
@lisp
|
|
|
|
(ert-deftest test-divide-by-zero ()
|
|
|
|
(should-error (/ 1 0)
|
|
|
|
:type 'arith-error))
|
|
|
|
@end lisp
|
|
|
|
|
|
|
|
This checks that dividing one by zero signals an error of type
|
|
|
|
@code{arith-error}. The @code{:type} argument to @code{should-error}
|
|
|
|
is optional; if absent, any type of error is accepted.
|
|
|
|
@code{should-error} returns an error description of the error that was
|
2011-11-14 20:23:26 +00:00
|
|
|
signaled, to allow additional checks to be made. The error
|
2011-01-12 16:08:24 +00:00
|
|
|
description has the format @code{(ERROR-SYMBOL . DATA)}.
|
|
|
|
|
|
|
|
There is no @code{should-not-error} macro since tests that signal an
|
|
|
|
error fail anyway, so @code{should-not-error} is effectively the
|
|
|
|
default.
|
|
|
|
|
|
|
|
@xref{Understanding Explanations}, for more details on what
|
|
|
|
@code{should} reports.
|
|
|
|
|
|
|
|
|
2012-12-22 19:49:54 +00:00
|
|
|
@node Expected Failures
|
2011-01-12 16:08:24 +00:00
|
|
|
@section Expected Failures
|
|
|
|
|
2011-10-14 07:16:56 +00:00
|
|
|
Some bugs are complicated to fix, or not very important, and are left as
|
2011-01-12 16:08:24 +00:00
|
|
|
@emph{known bugs}. If there is a test case that triggers the bug and
|
|
|
|
fails, ERT will alert you of this failure every time you run all
|
|
|
|
tests. For known bugs, this alert is a distraction. The way to
|
|
|
|
suppress it is to add @code{:expected-result :failed} to the test
|
|
|
|
definition:
|
|
|
|
|
|
|
|
@lisp
|
|
|
|
(ert-deftest future-bug ()
|
|
|
|
"Test `time-forward' with negative arguments.
|
2011-10-14 07:16:56 +00:00
|
|
|
Since this functionality isn't implemented, the test is known to fail."
|
2011-01-12 16:08:24 +00:00
|
|
|
:expected-result :failed
|
|
|
|
(time-forward -1))
|
|
|
|
@end lisp
|
|
|
|
|
|
|
|
ERT will still display a small @code{f} in the progress bar as a
|
|
|
|
reminder that there is a known bug, and will count the test as failed,
|
|
|
|
but it will be quiet about it otherwise.
|
|
|
|
|
|
|
|
An alternative to marking the test as a known failure this way is to
|
|
|
|
delete the test. This is a good idea if there is no intent to fix it,
|
|
|
|
i.e., if the behavior that was formerly considered a bug has become an
|
|
|
|
accepted feature.
|
|
|
|
|
|
|
|
In general, however, it can be useful to keep tests that are known to
|
|
|
|
fail. If someone wants to fix the bug, they will have a very good
|
|
|
|
starting point: an automated test case that reproduces the bug. This
|
|
|
|
makes it much easier to fix the bug, demonstrate that it is fixed, and
|
|
|
|
prevent future regressions.
|
|
|
|
|
|
|
|
ERT displays the same kind of alerts for tests that pass unexpectedly
|
2011-10-14 07:16:56 +00:00
|
|
|
as it displays for unexpected failures. This way, if you make code
|
2011-01-12 16:08:24 +00:00
|
|
|
changes that happen to fix a bug that you weren't aware of, you will
|
|
|
|
know to remove the @code{:expected-result} clause of that test and
|
|
|
|
close the corresponding bug report, if any.
|
|
|
|
|
|
|
|
Since @code{:expected-result} evaluates its argument when the test is
|
|
|
|
loaded, tests can be marked as known failures only on certain Emacs
|
|
|
|
versions, specific architectures, etc.:
|
|
|
|
|
|
|
|
@lisp
|
|
|
|
(ert-deftest foo ()
|
|
|
|
"A test that is expected to fail on Emacs 23 but succeed elsewhere."
|
|
|
|
:expected-result (if (string-match "GNU Emacs 23[.]" (emacs-version))
|
|
|
|
:failed
|
|
|
|
:passed)
|
|
|
|
...)
|
|
|
|
@end lisp
|
|
|
|
|
|
|
|
|
2012-12-22 19:49:54 +00:00
|
|
|
@node Tests and Their Environment
|
2011-01-12 16:08:24 +00:00
|
|
|
@section Tests and Their Environment
|
|
|
|
|
2013-10-24 07:40:05 +00:00
|
|
|
Sometimes, it doesn't make sense to run a test due to missing
|
|
|
|
preconditions. A required Emacs feature might not be compiled in, the
|
|
|
|
function to be tested could call an external binary which might not be
|
|
|
|
available on the test machine, you name it. In this case, the macro
|
|
|
|
@code{skip-unless} could be used to skip the test:
|
|
|
|
|
|
|
|
@lisp
|
|
|
|
(ert-deftest test-dbus ()
|
|
|
|
"A test that checks D-BUS functionality."
|
|
|
|
(skip-unless (featurep 'dbusbind))
|
|
|
|
...)
|
|
|
|
@end lisp
|
|
|
|
|
2011-01-12 16:08:24 +00:00
|
|
|
The outcome of running a test should not depend on the current state
|
|
|
|
of the environment, and each test should leave its environment in the
|
|
|
|
same state it found it in. In particular, a test should not depend on
|
|
|
|
any Emacs customization variables or hooks, and if it has to make any
|
2011-10-14 07:16:56 +00:00
|
|
|
changes to Emacs's state or state external to Emacs (such as the file
|
|
|
|
system), it should undo these changes before it returns, regardless of
|
2011-01-12 16:08:24 +00:00
|
|
|
whether it passed or failed.
|
|
|
|
|
|
|
|
Tests should not depend on the environment because any such
|
|
|
|
dependencies can make the test brittle or lead to failures that occur
|
|
|
|
only under certain circumstances and are hard to reproduce. Of
|
|
|
|
course, the code under test may have settings that affect its
|
|
|
|
behavior. In that case, it is best to make the test @code{let}-bind
|
2011-10-14 07:16:56 +00:00
|
|
|
all such setting variables to set up a specific configuration for the
|
2011-01-12 16:08:24 +00:00
|
|
|
duration of the test. The test can also set up a number of different
|
|
|
|
configurations and run the code under test with each.
|
|
|
|
|
|
|
|
Tests that have side effects on their environment should restore it to
|
|
|
|
its original state because any side effects that persist after the
|
|
|
|
test can disrupt the workflow of the programmer running the tests. If
|
2011-10-14 07:16:56 +00:00
|
|
|
the code under test has side effects on Emacs's current state, such as
|
2011-01-12 16:08:24 +00:00
|
|
|
on the current buffer or window configuration, the test should create
|
|
|
|
a temporary buffer for the code to manipulate (using
|
|
|
|
@code{with-temp-buffer}), or save and restore the window configuration
|
|
|
|
(using @code{save-window-excursion}), respectively. For aspects of
|
|
|
|
the state that can not be preserved with such macros, cleanup should
|
|
|
|
be performed with @code{unwind-protect}, to ensure that the cleanup
|
|
|
|
occurs even if the test fails.
|
|
|
|
|
|
|
|
An exception to this are messages that the code under test prints with
|
|
|
|
@code{message} and similar logging; tests should not bother restoring
|
Use @file for buffers, per the Texinfo manual
* doc/misc/ada-mode.texi, doc/misc/auth.texi, doc/misc/calc.texi:
* doc/misc/ebrowse.texi, doc/misc/efaq.texi, doc/misc/emacs-gnutls.texi:
* doc/misc/epa.texi, doc/misc/ert.texi, doc/misc/eshell.texi:
* doc/misc/eww.texi, doc/misc/flymake.texi, doc/misc/gnus.texi:
* doc/misc/info.texi, doc/misc/message.texi, doc/misc/mh-e.texi:
* doc/misc/newsticker.texi, doc/misc/pcl-cvs.texi, doc/misc/rcirc.texi:
* doc/misc/sem-user.texi, doc/misc/smtpmail.texi, doc/misc/url.texi:
* doc/misc/viper.texi, doc/misc/wisent.texi, doc/misc/woman.texi:
Use @file for buffers, per the Texinfo manual.
2014-03-12 20:52:34 +00:00
|
|
|
the @file{*Message*} buffer to its original state.
|
2011-01-12 16:08:24 +00:00
|
|
|
|
|
|
|
The above guidelines imply that tests should avoid calling highly
|
|
|
|
customizable commands such as @code{find-file}, except, of course, if
|
|
|
|
such commands are what they want to test. The exact behavior of
|
|
|
|
@code{find-file} depends on many settings such as
|
|
|
|
@code{find-file-wildcards}, @code{enable-local-variables}, and
|
|
|
|
@code{auto-mode-alist}. It is difficult to write a meaningful test if
|
|
|
|
its behavior can be affected by so many external factors. Also,
|
|
|
|
@code{find-file} has side effects that are hard to predict and thus
|
2011-10-14 07:16:56 +00:00
|
|
|
hard to undo: It may create a new buffer or reuse an existing
|
2011-01-12 16:08:24 +00:00
|
|
|
buffer if one is already visiting the requested file; and it runs
|
|
|
|
@code{find-file-hook}, which can have arbitrary side effects.
|
|
|
|
|
|
|
|
Instead, it is better to use lower-level mechanisms with simple and
|
|
|
|
predictable semantics like @code{with-temp-buffer}, @code{insert} or
|
2011-10-14 07:16:56 +00:00
|
|
|
@code{insert-file-contents-literally}, and to activate any desired mode
|
2012-12-22 16:25:40 +00:00
|
|
|
by calling the corresponding function directly, after binding the
|
2014-06-08 23:39:23 +00:00
|
|
|
hook variables to @code{nil}. This avoids the above problems.
|
2011-01-12 16:08:24 +00:00
|
|
|
|
|
|
|
|
2012-12-22 19:49:54 +00:00
|
|
|
@node Useful Techniques
|
2011-01-12 16:08:24 +00:00
|
|
|
@section Useful Techniques when Writing Tests
|
|
|
|
|
|
|
|
Testing simple functions that have no side effects and no dependencies
|
|
|
|
on their environment is easy. Such tests often look like this:
|
|
|
|
|
|
|
|
@lisp
|
|
|
|
(ert-deftest ert-test-mismatch ()
|
|
|
|
(should (eql (ert--mismatch "" "") nil))
|
|
|
|
(should (eql (ert--mismatch "" "a") 0))
|
|
|
|
(should (eql (ert--mismatch "a" "a") nil))
|
|
|
|
(should (eql (ert--mismatch "ab" "a") 1))
|
|
|
|
(should (eql (ert--mismatch "Aa" "aA") 0))
|
|
|
|
(should (eql (ert--mismatch '(a b c) '(a b d)) 2)))
|
|
|
|
@end lisp
|
|
|
|
|
|
|
|
This test calls the function @code{ert--mismatch} several times with
|
|
|
|
various combinations of arguments and compares the return value to the
|
|
|
|
expected return value. (Some programmers prefer @code{(should (eql
|
|
|
|
EXPECTED ACTUAL))} over the @code{(should (eql ACTUAL EXPECTED))}
|
|
|
|
shown here. ERT works either way.)
|
|
|
|
|
|
|
|
Here's a more complicated test:
|
|
|
|
|
|
|
|
@lisp
|
|
|
|
(ert-deftest ert-test-record-backtrace ()
|
|
|
|
(let ((test (make-ert-test :body (lambda () (ert-fail "foo")))))
|
|
|
|
(let ((result (ert-run-test test)))
|
|
|
|
(should (ert-test-failed-p result))
|
|
|
|
(with-temp-buffer
|
|
|
|
(ert--print-backtrace (ert-test-failed-backtrace result))
|
|
|
|
(goto-char (point-min))
|
|
|
|
(end-of-line)
|
2011-10-14 07:16:56 +00:00
|
|
|
(let ((first-line (buffer-substring-no-properties
|
|
|
|
(point-min) (point))))
|
|
|
|
(should (equal first-line
|
|
|
|
" signal(ert-test-failed (\"foo\"))")))))))
|
2011-01-12 16:08:24 +00:00
|
|
|
@end lisp
|
|
|
|
|
|
|
|
This test creates a test object using @code{make-ert-test} whose body
|
|
|
|
will immediately signal failure. It then runs that test and asserts
|
|
|
|
that it fails. Then, it creates a temporary buffer and invokes
|
|
|
|
@code{ert--print-backtrace} to print the backtrace of the failed test
|
|
|
|
to the current buffer. Finally, it extracts the first line from the
|
|
|
|
buffer and asserts that it matches what we expect. It uses
|
|
|
|
@code{buffer-substring-no-properties} and @code{equal} to ignore text
|
|
|
|
properties; for a test that takes properties into account,
|
|
|
|
@code{buffer-substring} and @code{ert-equal-including-properties}
|
|
|
|
could be used instead.
|
|
|
|
|
|
|
|
The reason why this test only checks the first line of the backtrace
|
|
|
|
is that the remainder of the backtrace is dependent on ERT's internals
|
|
|
|
as well as whether the code is running interpreted or compiled. By
|
2012-12-22 16:25:40 +00:00
|
|
|
looking only at the first line, the test checks a useful property---that
|
|
|
|
the backtrace correctly captures the call to @code{signal} that
|
|
|
|
results from the call to @code{ert-fail}---without being brittle.
|
2011-01-12 16:08:24 +00:00
|
|
|
|
|
|
|
This example also shows that writing tests is much easier if the code
|
|
|
|
under test was structured with testing in mind.
|
|
|
|
|
|
|
|
For example, if @code{ert-run-test} accepted only symbols that name
|
|
|
|
tests rather than test objects, the test would need a name for the
|
|
|
|
failing test, which would have to be a temporary symbol generated with
|
2011-10-14 07:16:56 +00:00
|
|
|
@code{make-symbol}, to avoid side effects on Emacs's state. Choosing
|
2011-01-12 16:08:24 +00:00
|
|
|
the right interface for @code{ert-run-tests} allows the test to be
|
|
|
|
simpler.
|
|
|
|
|
|
|
|
Similarly, if @code{ert--print-backtrace} printed the backtrace to a
|
|
|
|
buffer with a fixed name rather than the current buffer, it would be
|
|
|
|
much harder for the test to undo the side effect. Of course, some
|
|
|
|
code somewhere needs to pick the buffer name. But that logic is
|
|
|
|
independent of the logic that prints backtraces, and keeping them in
|
|
|
|
separate functions allows us to test them independently.
|
|
|
|
|
|
|
|
A lot of code that you will encounter in Emacs was not written with
|
|
|
|
testing in mind. Sometimes, the easiest way to write tests for such
|
|
|
|
code is to restructure the code slightly to provide better interfaces
|
|
|
|
for testing. Usually, this makes the interfaces easier to use as
|
|
|
|
well.
|
|
|
|
|
|
|
|
|
2012-12-22 19:49:54 +00:00
|
|
|
@node How to Debug Tests
|
2011-01-12 16:08:24 +00:00
|
|
|
@chapter How to Debug Tests
|
|
|
|
|
|
|
|
This section describes how to use ERT's features to understand why
|
|
|
|
a test failed.
|
|
|
|
|
|
|
|
|
|
|
|
@menu
|
|
|
|
* Understanding Explanations:: How ERT gives details on why an assertion failed.
|
|
|
|
* Interactive Debugging:: Tools available in the ERT results buffer.
|
|
|
|
@end menu
|
|
|
|
|
|
|
|
|
2012-12-22 19:49:54 +00:00
|
|
|
@node Understanding Explanations
|
2011-01-12 16:08:24 +00:00
|
|
|
@section Understanding Explanations
|
|
|
|
|
|
|
|
Failed @code{should} forms are reported like this:
|
|
|
|
|
|
|
|
@example
|
|
|
|
F addition-test
|
|
|
|
(ert-test-failed
|
|
|
|
((should
|
|
|
|
(=
|
|
|
|
(+ 1 2)
|
|
|
|
4))
|
|
|
|
:form
|
|
|
|
(= 3 4)
|
|
|
|
:value nil))
|
|
|
|
@end example
|
|
|
|
|
|
|
|
ERT shows what the @code{should} expression looked like and what
|
|
|
|
values its subexpressions had: The source code of the assertion was
|
|
|
|
@code{(should (= (+ 1 2) 4))}, which applied the function @code{=} to
|
|
|
|
the arguments @code{3} and @code{4}, resulting in the value
|
|
|
|
@code{nil}. In this case, the test is wrong; it should expect 3
|
|
|
|
rather than 4.
|
|
|
|
|
|
|
|
If a predicate like @code{equal} is used with @code{should}, ERT
|
|
|
|
provides a so-called @emph{explanation}:
|
|
|
|
|
|
|
|
@example
|
|
|
|
F list-test
|
|
|
|
(ert-test-failed
|
|
|
|
((should
|
|
|
|
(equal
|
|
|
|
(list 'a 'b 'c)
|
|
|
|
'(a b d)))
|
|
|
|
:form
|
|
|
|
(equal
|
|
|
|
(a b c)
|
|
|
|
(a b d))
|
|
|
|
:value nil :explanation
|
|
|
|
(list-elt 2
|
|
|
|
(different-atoms c d))))
|
|
|
|
@end example
|
|
|
|
|
|
|
|
In this case, the function @code{equal} was applied to the arguments
|
|
|
|
@code{(a b c)} and @code{(a b d)}. ERT's explanation shows that
|
|
|
|
the item at index 2 differs between the two lists; in one list, it is
|
|
|
|
the atom c, in the other, it is the atom d.
|
|
|
|
|
|
|
|
In simple examples like the above, the explanation is unnecessary.
|
|
|
|
But in cases where the difference is not immediately apparent, it can
|
|
|
|
save time:
|
|
|
|
|
|
|
|
@example
|
|
|
|
F test1
|
|
|
|
(ert-test-failed
|
|
|
|
((should
|
|
|
|
(equal x y))
|
|
|
|
:form
|
|
|
|
(equal a a)
|
|
|
|
:value nil :explanation
|
|
|
|
(different-symbols-with-the-same-name a a)))
|
|
|
|
@end example
|
|
|
|
|
|
|
|
ERT only provides explanations for predicates that have an explanation
|
|
|
|
function registered. @xref{Defining Explanation Functions}.
|
|
|
|
|
|
|
|
|
2012-12-22 19:49:54 +00:00
|
|
|
@node Interactive Debugging
|
2011-01-12 16:08:24 +00:00
|
|
|
@section Interactive Debugging
|
|
|
|
|
2011-10-14 07:16:56 +00:00
|
|
|
Debugging failed tests essentially works the same way as debugging any
|
2011-01-12 16:08:24 +00:00
|
|
|
other problems with Lisp code. Here are a few tricks specific to
|
|
|
|
tests:
|
|
|
|
|
|
|
|
@itemize
|
|
|
|
@item Re-run the failed test a few times to see if it fails in the same way
|
|
|
|
each time. It's good to find out whether the behavior is
|
|
|
|
deterministic before spending any time looking for a cause. In the
|
|
|
|
ERT results buffer, @kbd{r} re-runs the selected test.
|
|
|
|
|
2011-10-14 07:16:56 +00:00
|
|
|
@item Use @kbd{.} to jump to the source code of the test to find out exactly
|
|
|
|
what it does. Perhaps the test is broken rather than the code
|
2011-01-12 16:08:24 +00:00
|
|
|
under test.
|
|
|
|
|
|
|
|
@item If the test contains a series of @code{should} forms and you can't
|
|
|
|
tell which one failed, use @kbd{l}, which shows you the list of all
|
|
|
|
@code{should} forms executed during the test before it failed.
|
|
|
|
|
|
|
|
@item Use @kbd{b} to view the backtrace. You can also use @kbd{d} to re-run
|
|
|
|
the test with debugging enabled, this will enter the debugger and show
|
|
|
|
the backtrace as well; but the top few frames shown there will not be
|
|
|
|
relevant to you since they are ERT's own debugger hook. @kbd{b}
|
|
|
|
strips them out, so it is more convenient.
|
|
|
|
|
|
|
|
@item If the test or the code under testing prints messages using
|
|
|
|
@code{message}, use @kbd{m} to see what messages it printed before it
|
|
|
|
failed. This can be useful to figure out how far it got.
|
|
|
|
|
|
|
|
@item You can instrument tests for debugging the same way you instrument
|
2012-12-22 16:25:40 +00:00
|
|
|
@code{defun}s for debugging: go to the source code of the test and
|
2011-01-12 16:08:24 +00:00
|
|
|
type @kbd{@kbd{C-u} @kbd{C-M-x}}. Then, go back to the ERT buffer and
|
|
|
|
re-run the test with @kbd{r} or @kbd{d}.
|
|
|
|
|
|
|
|
@item If you have been editing and rearranging tests, it is possible that
|
2012-12-22 16:25:40 +00:00
|
|
|
ERT remembers an old test that you have since renamed or removed:
|
2011-01-12 16:08:24 +00:00
|
|
|
renamings or removals of definitions in the source code leave around a
|
2011-10-14 07:16:56 +00:00
|
|
|
stray definition under the old name in the running process (this is a
|
|
|
|
common problem in Lisp). In such a situation, hit @kbd{D} to let ERT
|
2011-01-12 16:08:24 +00:00
|
|
|
forget about the obsolete test.
|
|
|
|
@end itemize
|
|
|
|
|
|
|
|
|
2012-12-22 19:49:54 +00:00
|
|
|
@node Extending ERT
|
2011-01-12 16:08:24 +00:00
|
|
|
@chapter Extending ERT
|
|
|
|
|
|
|
|
There are several ways to add functionality to ERT.
|
|
|
|
|
|
|
|
@menu
|
|
|
|
* Defining Explanation Functions:: Teach ERT about more predicates.
|
|
|
|
* Low-Level Functions for Working with Tests:: Use ERT's data for your purposes.
|
|
|
|
@end menu
|
|
|
|
|
|
|
|
|
2012-12-22 19:49:54 +00:00
|
|
|
@node Defining Explanation Functions
|
2011-01-12 16:08:24 +00:00
|
|
|
@section Defining Explanation Functions
|
|
|
|
|
|
|
|
The explanation function for a predicate is a function that takes the
|
|
|
|
same arguments as the predicate and returns an @emph{explanation}.
|
|
|
|
The explanation should explain why the predicate, when invoked with
|
|
|
|
the arguments given to the explanation function, returns the value
|
|
|
|
that it returns. The explanation can be any object but should have a
|
|
|
|
comprehensible printed representation. If the return value of the
|
|
|
|
predicate needs no explanation for a given list of arguments, the
|
2014-06-08 23:39:23 +00:00
|
|
|
explanation function should return @code{nil}.
|
2011-01-12 16:08:24 +00:00
|
|
|
|
|
|
|
To associate an explanation function with a predicate, add the
|
|
|
|
property @code{ert-explainer} to the symbol that names the predicate.
|
|
|
|
The value of the property should be the symbol that names the
|
|
|
|
explanation function.
|
|
|
|
|
|
|
|
|
2012-12-22 19:49:54 +00:00
|
|
|
@node Low-Level Functions for Working with Tests
|
2011-01-12 16:08:24 +00:00
|
|
|
@section Low-Level Functions for Working with Tests
|
|
|
|
|
|
|
|
Both @code{ert-run-tests-interactively} and @code{ert-run-tests-batch}
|
|
|
|
are implemented on top of the lower-level test handling code in the
|
2011-11-14 06:27:12 +00:00
|
|
|
sections of @file{ert.el} labeled ``Facilities for running a single test'',
|
2011-10-14 07:16:56 +00:00
|
|
|
``Test selectors'', and ``Facilities for running a whole set of tests''.
|
2011-01-12 16:08:24 +00:00
|
|
|
|
|
|
|
If you want to write code that works with ERT tests, you should take a
|
|
|
|
look at this lower-level code. Symbols that start with @code{ert--}
|
2011-10-14 07:16:56 +00:00
|
|
|
are internal to ERT, whereas those that start with @code{ert-} are
|
|
|
|
meant to be usable by other code. But there is no mature API yet.
|
2011-01-12 16:08:24 +00:00
|
|
|
|
|
|
|
Contributions to ERT are welcome.
|
|
|
|
|
|
|
|
|
2012-12-22 19:49:54 +00:00
|
|
|
@node Other Testing Concepts
|
2011-01-12 16:08:24 +00:00
|
|
|
@chapter Other Testing Concepts
|
|
|
|
|
|
|
|
For information on mocks, stubs, fixtures, or test suites, see below.
|
|
|
|
|
|
|
|
|
|
|
|
@menu
|
2011-10-14 07:16:56 +00:00
|
|
|
* Mocks and Stubs:: Stubbing out code that is irrelevant to the test.
|
|
|
|
* Fixtures and Test Suites:: How ERT differs from tools for other languages.
|
2011-01-12 16:08:24 +00:00
|
|
|
@end menu
|
|
|
|
|
2012-12-22 19:49:54 +00:00
|
|
|
@node Mocks and Stubs
|
2011-01-12 16:08:24 +00:00
|
|
|
@section Other Tools for Emacs Lisp
|
|
|
|
|
|
|
|
Stubbing out functions or using so-called @emph{mocks} can make it
|
|
|
|
easier to write tests. See
|
|
|
|
@url{http://en.wikipedia.org/wiki/Mock_object} for an explanation of
|
|
|
|
the corresponding concepts in object-oriented languages.
|
|
|
|
|
|
|
|
ERT does not have built-in support for mocks or stubs. The package
|
|
|
|
@code{el-mock} (see @url{http://www.emacswiki.org/emacs/el-mock.el})
|
|
|
|
offers mocks for Emacs Lisp and can be used in conjunction with ERT.
|
|
|
|
|
|
|
|
|
2012-12-22 19:49:54 +00:00
|
|
|
@node Fixtures and Test Suites
|
2011-01-12 16:08:24 +00:00
|
|
|
@section Fixtures and Test Suites
|
|
|
|
|
|
|
|
In many ways, ERT is similar to frameworks for other languages like
|
|
|
|
SUnit or JUnit. However, two features commonly found in such
|
|
|
|
frameworks are notably absent from ERT: fixtures and test suites.
|
|
|
|
|
2011-10-14 07:16:56 +00:00
|
|
|
Fixtures are mainly used (e.g., in SUnit or JUnit) to provide an
|
|
|
|
environment for a set of tests, and consist of set-up and tear-down
|
2011-01-12 16:08:24 +00:00
|
|
|
functions.
|
|
|
|
|
|
|
|
While fixtures are a useful syntactic simplification in other
|
|
|
|
languages, this does not apply to Lisp, where higher-order functions
|
2015-04-11 14:47:25 +00:00
|
|
|
and @code{unwind-protect} are available. One way to implement and use a
|
2011-01-12 16:08:24 +00:00
|
|
|
fixture in ERT is
|
|
|
|
|
|
|
|
@lisp
|
|
|
|
(defun my-fixture (body)
|
|
|
|
(unwind-protect
|
|
|
|
(progn [set up]
|
|
|
|
(funcall body))
|
|
|
|
[tear down]))
|
|
|
|
|
|
|
|
(ert-deftest my-test ()
|
|
|
|
(my-fixture
|
|
|
|
(lambda ()
|
|
|
|
[test code])))
|
|
|
|
@end lisp
|
|
|
|
|
|
|
|
(Another way would be a @code{with-my-fixture} macro.) This solves
|
|
|
|
the set-up and tear-down part, and additionally allows any test
|
|
|
|
to use any combination of fixtures, so it is more flexible than what
|
|
|
|
other tools typically allow.
|
|
|
|
|
|
|
|
If the test needs access to the environment the fixture sets up, the
|
|
|
|
fixture can be modified to pass arguments to the body.
|
|
|
|
|
|
|
|
These are well-known Lisp techniques. Special syntax for them could
|
|
|
|
be added but would provide only a minor simplification.
|
|
|
|
|
|
|
|
(If you are interested in such syntax, note that splitting set-up and
|
|
|
|
tear-down into separate functions, like *Unit tools usually do, makes
|
2015-04-11 14:47:25 +00:00
|
|
|
it impossible to establish dynamic @code{let} bindings as part of the
|
2011-01-12 16:08:24 +00:00
|
|
|
fixture. So, blindly imitating the way fixtures are implemented in
|
|
|
|
other languages would be counter-productive in Lisp.)
|
|
|
|
|
|
|
|
The purpose of test suites is to group related tests together.
|
|
|
|
|
|
|
|
The most common use of this is to run just the tests for one
|
|
|
|
particular module. Since symbol prefixes are the usual way of
|
|
|
|
separating module namespaces in Emacs Lisp, test selectors already
|
|
|
|
solve this by allowing regexp matching on test names; e.g., the
|
2015-09-04 20:31:41 +00:00
|
|
|
selector @code{"^ert-"} selects ERT's self-tests.
|
2011-01-12 16:08:24 +00:00
|
|
|
|
2011-10-14 07:16:56 +00:00
|
|
|
Other uses include grouping tests by their expected execution time,
|
2012-12-05 22:27:56 +00:00
|
|
|
e.g., to run quick tests during interactive development and slow tests less
|
2011-10-14 07:16:56 +00:00
|
|
|
often. This can be achieved with the @code{:tag} argument to
|
2011-01-12 16:08:24 +00:00
|
|
|
@code{ert-deftest} and @code{tag} test selectors.
|
|
|
|
|
2012-12-22 19:49:54 +00:00
|
|
|
@node GNU Free Documentation License
|
2012-12-21 19:01:24 +00:00
|
|
|
@appendix GNU Free Documentation License
|
|
|
|
@include doclicense.texi
|
|
|
|
|
2011-01-12 16:08:24 +00:00
|
|
|
@bye
|
|
|
|
|
2011-10-14 07:16:56 +00:00
|
|
|
@c LocalWords: ERT JUnit namespace docstring ERT's
|
2011-01-12 16:08:24 +00:00
|
|
|
@c LocalWords: backtrace makefiles workflow backtraces API SUnit
|
|
|
|
@c LocalWords: subexpressions
|