]> git.notmuchmail.org Git - notmuch/blob - test/README
test: allow user to choose which emacs to run tests with.
[notmuch] / test / README
1 Notmuch test suite
2 ==================
3 This directory contains the test suite for notmuch.
4
5 When fixing bugs or enhancing notmuch, you are strongly encouraged to
6 add tests in this directory to cover what you are trying to fix or
7 enhance.
8
9 Running Tests
10 -------------
11 The easiest way to run tests is to say "make test", (or simply run the
12 notmuch-test script). Either command will run all available tests.
13
14 Alternately, you can run a specific subset of tests by simply invoking
15 one of the executable scripts in this directory, (such as ./search,
16 ./reply, etc). Note that you will probably want "make test-binaries"
17 before running individual tests.
18
19 The following command-line options are available when running tests:
20
21 --debug::
22         This may help the person who is developing a new test.
23         It causes the command defined with test_debug to run.
24
25 --immediate::
26         This causes the test to immediately exit upon the first
27         failed test.
28
29 --valgrind::
30         Execute notmuch with valgrind and exit with status
31         126 on errors (just like regular tests, this will only stop
32         the test script when running under -i).  Valgrind errors
33         go to stderr, so you might want to pass the -v option, too.
34
35         Since it makes no sense to run the tests with --valgrind and
36         not see any output, this option implies --verbose.  For
37         convenience, it also implies --tee.
38
39 --tee::
40         In addition to printing the test output to the terminal,
41         write it to files named 't/test-results/$TEST_NAME.out'.
42         As the names depend on the tests' file names, it is safe to
43         run the tests with this option in parallel.
44
45 --root=<dir>::
46         This runs the testsuites specified under a seperate directory.
47         However, caution is advised, as not all tests are maintained
48         with this relocation in mind, so some tests may behave
49         differently.
50
51         Pointing this argument at a tmpfs filesystem can improve the
52         speed of the test suite for some users.
53
54 When invoking the test suite via "make test" any of the above options
55 can be specified as follows:
56
57         make test OPTIONS="--verbose"
58
59 You can choose an emacs binary to run the tests in one of the
60 following ways.
61
62         TEST_EMACS=my-special-emacs make test
63         TEST_EMACS=my-special-emacs ./emacs
64         make test TEST_EMACS=my-special-emacs
65
66 Skipping Tests
67 --------------
68 If, for any reason, you need to skip one or more tests, you can do so
69 by setting the NOTMUCH_SKIP_TESTS variable to the name of one or more
70 sections of tests.
71
72 For example:
73
74     $ NOTMUCH_SKIP_TESTS="search reply" make test
75
76 Even more fine-grained skipping is possible by appending a test number
77 (or glob pattern) after the section name. For example, the first
78 search test and the second reply test could be skipped with:
79
80     $ NOTMUCH_SKIP_TESTS="search.1 reply.2" make test
81
82 Note that some tests in the existing test suite rely on previous test
83 items, so you cannot arbitrarily skip any test and expect the
84 remaining tests to be unaffected.
85
86 Writing Tests
87 -------------
88 The test script is written as a shell script.  It should start with
89 the standard "#!/usr/bin/env bash" with copyright notices, and an
90 assignment to variable 'test_description', like this:
91
92         #!/usr/bin/env bash
93         #
94         # Copyright (c) 2005 Junio C Hamano
95         #
96
97         test_description='xxx test (option --frotz)
98
99         This test exercises the "notmuch xxx" command when
100         given the option --frotz.'
101
102 Source 'test-lib.sh'
103 --------------------
104 After assigning test_description, the test script should source
105 test-lib.sh like this:
106
107         . ./test-lib.sh
108
109 This test harness library does the following things:
110
111  - If the script is invoked with command line argument --help
112    (or -h), it shows the test_description and exits.
113
114  - Creates a temporary directory with default notmuch-config and a
115    mail store with a corpus of mail, (initially, 50 early messages
116    sent to the notmuch list). This directory is
117    test/tmp.<test-basename>. The path to notmuch-config is exported in
118    NOTMUCH_CONFIG environment variable and mail store path is stored
119    in MAIL_DIR variable.
120
121  - Defines standard test helper functions for your scripts to
122    use.  These functions are designed to make all scripts behave
123    consistently when command line arguments --verbose (or -v),
124    --debug (or -d), and --immediate (or -i) is given.
125
126 End with test_done
127 ------------------
128 Your script will be a sequence of tests, using helper functions
129 from the test harness library.  At the end of the script, call
130 'test_done'.
131
132 Test harness library
133 --------------------
134 There are a handful helper functions defined in the test harness
135 library for your script to use.
136
137  test_expect_success <message> <script>
138
139    This takes two strings as parameter, and evaluates the
140    <script>.  If it yields success, test is considered
141    successful.  <message> should state what it is testing.
142
143  test_begin_subtest <message>
144
145    Set the test description message for a subsequent test_expect_equal
146    invocation (see below).
147
148  test_subtest_known_broken
149
150    Mark the current test as broken.  Such tests are expected to fail.
151    Unlike the normal tests, which say "PASS" on success and "FAIL" on
152    failure, these will say "FIXED" on success and "BROKEN" on failure.
153    Failures from these tests won't cause -i (immediate) to stop.  A
154    test must call this before any test_expect_* function.
155
156  test_expect_equal <output> <expected>
157
158    This is an often-used convenience function built on top of
159    test_expect_success. It uses the message from the last
160    test_begin_subtest call, so call before calling
161    test_expect_equal. This function generates a successful test if
162    both the <output> and <expected> strings are identical. If not, it
163    will generate a failure and print the difference of the two
164    strings.
165
166  test_expect_equal_file <output> <expected>
167
168    Identical to test_exepect_equal, except that <output> and
169    <expected> are files instead of strings.  This is a much more
170    robust method to compare formatted textual information, since it
171    also notices whitespace and closing newline differences.
172
173  test_debug <script>
174
175    This takes a single argument, <script>, and evaluates it only
176    when the test script is started with --debug command line
177    argument.  This is primarily meant for use during the
178    development of a new test script.
179
180  test_emacs <emacs-lisp-expressions>
181
182    This function executes the provided emacs lisp script within
183    emacs. The script can be a sequence of emacs lisp expressions,
184    (that is, they will be evaluated within a progn form). Emacs
185    stdout and stderr is not available, the common way to get output
186    is to save it to a file. There are some auxiliary functions
187    useful in emacs tests provided in test-lib.el. Do not use `setq'
188    for setting variables in Emacs tests because it affects other
189    tests that may run in the same Emacs instance.  Use `let' instead
190    so the scope of the changed variables is limited to a single test.
191
192  test_done
193
194    Your test script must have test_done at the end.  Its purpose
195    is to summarize successes and failures in the test script and
196    exit with an appropriate error code.
197
198 There are also a number of notmuch-specific auxiliary functions and
199 variables which are useful in writing tests:
200
201   generate_message
202
203     Generates a message with an optional template. Most tests will
204     actually prefer to call add_message. See below.
205
206   add_message
207
208     Generate a message and add it to the database (by calling "notmuch
209     new"). It is sufficient to simply call add_message with no
210     arguments if you don't care about the content of the message. If
211     more control is needed, arguments can be provide to specify many
212     different header values for the new message. See the documentation
213     within test-lib.sh or refer to many example calls within existing
214     tests.
215
216   add_email_corpus
217
218     This function should be called at the beginning of a test file
219     when a test needs to operate on a non-empty body of messages. It
220     will initialize the mail database to a known state of 50 sample
221     messages, (culled from the early history of the notmuch mailing
222     list).
223
224   notmuch_counter_reset
225   $notmuch_counter_command
226   notmuch_counter_value
227
228     These allow to count how many times notmuch binary is called.
229     notmuch_counter_reset() function generates a script that counts
230     how many times it is called and resets the counter to zero.  The
231     function sets $notmuch_counter_command variable to the path to the
232     generated script that should be called instead of notmuch to do
233     the counting.  The notmuch_counter_value() function prints the
234     current counter value.