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?

  • Please never define test cases inline. There's no need to force your tests upon end users.
    – user227
    Oct 7, 2015 at 17:40
  • Yeah, I figured that wasn't the answer, but I still wasn't sure what the convention was. Oct 7, 2015 at 18:10

2 Answers 2


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

  • 1
    I used this code 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 requires) should do it.
    – mindthief
    Jan 2, 2021 at 17:57

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.