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