I'm new to using ERT for testing my Elisp code, and I have a question that doesn't seem to be covered in the manual: according to best practices, where does one typically put the ERT tests? Do they go at the end of the elisp file that is to be tested, or should I put them in a separate file? I'm sure they'll work either way, but is one or the other preferred?
Asked
Active
Viewed 310 times
4
-
Please _never_ define test cases inline. There's no need to force your tests upon end users. – Oct 07 '15 at 17:40
-
Yeah, I figured that wasn't the answer, but I still wasn't sure what the convention was. – Ryan C. Thompson Oct 07 '15 at 18:10
2 Answers
6
I would place the tests in a test
directory and name it thepackage-test.el
. Alternatively, thepackage-test-onething.el
and thepackage-test-anotherthing.el
.
Using a test directory is especially useful if the tests require auxiliary files.
Another advantage is that it simplifies the MELPA recipe, as subdirectories aren't included by default.
For an example of a package with relative complex tests, you can take a look at my font-lock rules for CMake: https://github.com/Lindydancer/cmake-font-lock

Lindydancer
- 6,095
- 1
- 13
- 25
-
1I used [this code](https://github.com/Lindydancer/cmake-font-lock/blob/47687b6ccd0e244691fb5907aaba609e5a42d787/test/cmake-font-lock-test-setup.el#L20-L27) from the package linked in the answer in order for the tests to be able to `require` the source files. Just putting that at the top of your test file (before any `require`s) should do it. – mindthief Jan 02 '21 at 17:57
4
Separate file, of course. The end users need not load it. In fact, if you distribute through MELPA, the tests won't be bundled. The convention is to name the file foo-test.el
.

abo-abo
- 13,943
- 1
- 29
- 43