README.md
1Tests
2=====
3
4Tests are broadly divided into *unit tests* ([test/unit](https://github.com/neovim/neovim/tree/master/test/unit/)),
5*functional tests* ([test/functional](https://github.com/neovim/neovim/tree/master/test/functional/)),
6and *old tests* ([src/nvim/testdir/](https://github.com/neovim/neovim/tree/master/src/nvim/testdir/)).
7
8- _Unit_ testing is achieved by compiling the tests as a shared library which is
9 loaded and called by [LuaJit FFI](http://luajit.org/ext_ffi.html).
10- _Functional_ tests are driven by RPC, so they do not require LuaJit (as
11 opposed to Lua).
12
13You can learn the [key concepts of Lua in 15 minutes](http://learnxinyminutes.com/docs/lua/).
14Use any existing test as a template to start writing new tests.
15
16Tests are run by `/cmake/RunTests.cmake` file, using `busted` (a Lua test-runner).
17For some failures, `.nvimlog` (or `$NVIM_LOG_FILE`) may provide insight.
18
19Depending on the presence of binaries (e.g., `xclip`) some tests will be
20ignored. You must compile with libintl to prevent `E319: The command is not
21available in this version` errors.
22
23
24---
25
26- [Running tests](#running-tests)
27- [Writing tests](#writing-tests)
28- [Lint](#lint)
29- [Configuration](#configuration)
30
31---
32
33
34Layout
35======
36
37- `/test/benchmark` : benchmarks
38- `/test/functional` : functional tests
39- `/test/unit` : unit tests
40- `/test/config` : contains `*.in` files which are transformed into `*.lua`
41 files using `configure_file` CMake command: this is for acessing CMake
42 variables in lua tests.
43- `/test/includes` : include-files for use by luajit `ffi.cdef` C definitions
44 parser: normally used to make macros not accessible via this mechanism
45 accessible the other way.
46- `/test/*/preload.lua` : modules preloaded by busted `--helper` option
47- `/test/**/helpers.lua` : common utility functions for test code
48- `/test/*/**/*_spec.lua` : actual tests. Files that do not end with
49 `_spec.lua` are libraries like `/test/**/helpers.lua`, except that they have
50 some common topic.
51- `/src/nvim/testdir` : old tests (from Vim)
52
53
54Running tests
55=============
56
57Executing Tests
58---------------
59
60To run all tests (except "old" tests):
61
62 make test
63
64To run only _unit_ tests:
65
66 make unittest
67
68To run only _functional_ tests:
69
70 make functionaltest
71
72
73Legacy tests
74------------
75
76To run all legacy Vim tests:
77
78 make oldtest
79
80To run a *single* legacy test file you can use either:
81
82 make oldtest TEST_FILE=test_syntax.vim
83
84or:
85
86 make src/nvim/testdir/test_syntax.vim
87
88- Specify only the test file name, not the full path.
89
90
91Debugging tests
92---------------
93
94- You can set `$GDB` to [run tests under gdbserver](https://github.com/neovim/neovim/pull/1527).
95 And if `$VALGRIND` is set it will pass `--vgdb=yes` to valgrind instead of
96 starting gdbserver directly.
97- Hanging tests often happen due to unexpected `:h press-enter` prompts. The
98 default screen width is 50 columns. Commands that try to print lines longer
99 than 50 columns in the command-line, e.g. `:edit very...long...path`, will
100 trigger the prompt. In this case, a shorter path or `:silent edit` should be
101 used.
102- If you can't figure out what is going on, try to visualize the screen. Put
103 this at the beginning of your test:
104
105 ```lua
106 local Screen = require('test.functional.ui.screen')
107 local screen = Screen.new()
108 screen:attach()
109 ```
110
111 Afterwards, put `screen:snapshot_util()` at any position in your test. See the
112 comment at the top of `test/functional/ui/screen.lua` for more.
113
114Filtering Tests
115---------------
116
117### Filter by name
118
119Another filter method is by setting a pattern of test name to `TEST_FILTER` or `TEST_FILTER_OUT`.
120
121``` lua
122it('foo api',function()
123 ...
124end)
125it('bar api',function()
126 ...
127end)
128```
129
130To run only test with filter name:
131
132 TEST_FILTER='foo.*api' make functionaltest
133
134To run all tests except ones matching a filter:
135
136 TEST_FILTER_OUT='foo.*api' make functionaltest
137
138### Filter by file
139
140To run a *specific* unit test:
141
142 TEST_FILE=test/unit/foo.lua make unittest
143
144To run a *specific* functional test:
145
146 TEST_FILE=test/functional/foo.lua make functionaltest
147
148To *repeat* a test:
149
150 BUSTED_ARGS="--repeat=100 --no-keep-going" TEST_FILE=test/functional/foo_spec.lua make functionaltest
151
152### Filter by tag
153
154Tests can be "tagged" by adding `#` before a token in the test description.
155
156``` lua
157it('#foo bar baz', function()
158 ...
159end)
160it('#foo another test', function()
161 ...
162end)
163```
164
165To run only the tagged tests:
166
167 TEST_TAG=foo make functionaltest
168
169**NOTE:**
170
171* `TEST_FILE` is not a pattern string like `TEST_TAG` or `TEST_FILTER`. The
172 given value to `TEST_FILE` must be a path to an existing file.
173* Both `TEST_TAG` and `TEST_FILTER` filter tests by the string descriptions
174 found in `it()` and `describe()`.
175
176
177Writing tests
178=============
179
180Guidelines
181----------
182
183- Luajit needs to know about type and constant declarations used in function
184 prototypes. The
185 [helpers.lua](https://github.com/neovim/neovim/blob/master/test/unit/helpers.lua)
186 file automatically parses `types.h`, so types used in the tested functions
187 could be moved to it to avoid having to rewrite the declarations in the test
188 files.
189 - `#define` constants must be rewritten `const` or `enum` so they can be
190 "visible" to the tests.
191- Use **pending()** to skip tests
192 ([example](https://github.com/neovim/neovim/commit/5c1dc0fbe7388528875aff9d7b5055ad718014de#diff-bf80b24c724b0004e8418102f68b0679R18)).
193 Do not silently skip the test with `if-else`. If a functional test depends on
194 some external factor (e.g. the existence of `md5sum` on `$PATH`), *and* you
195 can't mock or fake the dependency, then skip the test via `pending()` if the
196 external factor is missing. This ensures that the *total* test-count
197 (success + fail + error + pending) is the same in all environments.
198 - *Note:* `pending()` is ignored if it is missing an argument, unless it is
199 [contained in an `it()` block](https://github.com/neovim/neovim/blob/d21690a66e7eb5ebef18046c7a79ef898966d786/test/functional/ex_cmds/grep_spec.lua#L11).
200 Provide empty function argument if the `pending()` call is outside of `it()`
201 ([example](https://github.com/neovim/neovim/commit/5c1dc0fbe7388528875aff9d7b5055ad718014de#diff-bf80b24c724b0004e8418102f68b0679R18)).
202- Really long `source([=[...]=])` blocks may break Vim's Lua syntax
203 highlighting. Try `:syntax sync fromstart` to fix it.
204
205Where tests go
206--------------
207
208Tests in `/test/unit` and `/test/functional` are divided into groups
209by the semantic component they are testing.
210
211- _Unit tests_
212 ([test/unit](https://github.com/neovim/neovim/tree/master/test/unit)) should
213 match 1-to-1 with the structure of `src/nvim/`, because they are testing
214 functions directly. E.g. unit-tests for `src/nvim/undo.c` should live in
215 `test/unit/undo_spec.lua`.
216- _Functional tests_
217 ([test/functional](https://github.com/neovim/neovim/tree/master/test/functional))
218 are higher-level (plugins and user input) than unit tests; they are organized
219 by concept.
220 - Try to find an existing `test/functional/*/*_spec.lua` group that makes
221 sense, before creating a new one.
222
223
224Lint
225====
226
227`make lint` (and `make lualint`) runs [luacheck](https://github.com/mpeterv/luacheck)
228on the test code.
229
230If a luacheck warning must be ignored, specify the warning code. Example:
231
232 -- luacheck: ignore 621
233
234http://luacheck.readthedocs.io/en/stable/warnings.html
235
236Ignore the smallest applicable scope (e.g. inside a function, not at the top of
237the file).
238
239
240Configuration
241=============
242
243Test behaviour is affected by environment variables. Currently supported
244(Functional, Unit, Benchmarks) (when Defined; when set to _1_; when defined,
245treated as Integer; when defined, treated as String; when defined, treated as
246Number; !must be defined to function properly):
247
248- `BUSTED_ARGS` (F) (U): arguments forwarded to `busted`.
249
250- `GDB` (F) (D): makes nvim instances to be run under `gdbserver`. It will be
251 accessible on `localhost:7777`: use `gdb build/bin/nvim`, type `target remote
252 :7777` inside.
253
254- `GDBSERVER_PORT` (F) (I): overrides port used for `GDB`.
255
256- `VALGRIND` (F) (D): makes nvim instances to be run under `valgrind`. Log
257 files are named `valgrind-%p.log` in this case. Note that non-empty valgrind
258 log may fail tests. Valgrind arguments may be seen in
259 `/test/functional/helpers.lua`. May be used in conjunction with `GDB`.
260
261- `VALGRIND_LOG` (F) (S): overrides valgrind log file name used for `VALGRIND`.
262
263- `TEST_COLORS` (F) (U) (D): enable pretty colors in test runner.
264
265- `TEST_SKIP_FRAGILE` (F) (D): makes test suite skip some fragile tests.
266
267- `TEST_TIMEOUT` (FU) (I): specifies maximum time, in seconds, before the test
268 suite run is killed
269
270- `NVIM_LUA_NOTRACK` (F) (D): disable reference counting of Lua objects
271
272- `NVIM_PROG`, `NVIM_PRG` (F) (S): override path to Neovim executable (default
273 to `build/bin/nvim`).
274
275- `CC` (U) (S): specifies which C compiler to use to preprocess files.
276 Currently only compilers with gcc-compatible arguments are supported.
277
278- `NVIM_TEST_MAIN_CDEFS` (U) (1): makes `ffi.cdef` run in main process. This
279 raises a possibility of bugs due to conflicts in header definitions, despite
280 the counters, but greatly speeds up unit tests by not requiring `ffi.cdef` to
281 do parsing of big strings with C definitions.
282
283- `NVIM_TEST_PRINT_I` (U) (1): makes `cimport` print preprocessed, but not yet
284 filtered through `formatc` headers. Used to debug `formatc`. Printing is done
285 with the line numbers.
286
287- `NVIM_TEST_PRINT_CDEF` (U) (1): makes `cimport` print final lines which will
288 be then passed to `ffi.cdef`. Used to debug errors `ffi.cdef` happens to
289 throw sometimes.
290
291- `NVIM_TEST_PRINT_SYSCALLS` (U) (1): makes it print to stderr when syscall
292 wrappers are called and what they returned. Used to debug code which makes
293 unit tests be executed in separate processes.
294
295- `NVIM_TEST_RUN_FAILING_TESTS` (U) (1): makes `itp` run tests which are known
296 to fail (marked by setting third argument to `true`).
297
298- `LOG_DIR` (FU) (S!): specifies where to seek for valgrind and ASAN log files.
299
300- `NVIM_TEST_CORE_*` (FU) (S): a set of environment variables which specify
301 where to search for core files. Are supposed to be defined all at once.
302
303- `NVIM_TEST_CORE_GLOB_DIRECTORY` (FU) (S): directory where core files are
304 located. May be `.`. This directory is then recursively searched for core
305 files. Note: this variable must be defined for any of the following to have
306 any effect.
307
308- `NVIM_TEST_CORE_GLOB_RE` (FU) (S): regular expression which must be matched
309 by core files. E.g. `/core[^/]*$`. May be absent, in which case any file is
310 considered to be matched.
311
312- `NVIM_TEST_CORE_EXC_RE` (FU) (S): regular expression which excludes certain
313 directories from searching for core files inside. E.g. use `^/%.deps$` to not
314 search inside `/.deps`. If absent, nothing is excluded.
315
316- `NVIM_TEST_CORE_DB_CMD` (FU) (S): command to get backtrace out of the
317 debugger. E.g. `gdb -n -batch -ex "thread apply all bt full"
318 "$_NVIM_TEST_APP" -c "$_NVIM_TEST_CORE"`. Defaults to the example command.
319 This debug command may use environment variables `_NVIM_TEST_APP` (path to
320 application which is being debugged: normally either nvim or luajit) and
321 `_NVIM_TEST_CORE` (core file to get backtrace from).
322
323- `NVIM_TEST_CORE_RANDOM_SKIP` (FU) (D): makes `check_cores` not check cores
324 after approximately 90% of the tests. Should be used when finding cores is
325 too hard for some reason. Normally (on OS X or when
326 `NVIM_TEST_CORE_GLOB_DIRECTORY` is defined and this variable is not) cores
327 are checked for after each test.
328
329- `NVIM_TEST_RUN_TESTTEST` (U) (1): allows running
330 `test/unit/testtest_spec.lua` used to check how testing infrastructure works.
331
332- `NVIM_TEST_TRACE_LEVEL` (U) (N): specifies unit tests tracing level:
333 - `0` disables tracing (the fastest, but you get no data if tests crash and
334 there no core dump was generated),
335 - `1` leaves only C function calls and returns in the trace (faster than
336 recording everything),
337 - `2` records all function calls, returns and executed Lua source lines.
338
339- `NVIM_TEST_TRACE_ON_ERROR` (U) (1): makes unit tests yield trace on error in
340 addition to regular error message.
341
342- `NVIM_TEST_MAXTRACE` (U) (N): specifies maximum number of trace lines to
343 keep. Default is 1024.
344