Slightly improved ERT emacs setup

If anyone else can make use of that: I extended the proposed way to run tests from inside emacs with two improvements for less friction:

  • No need to open the tests file. Hit one key from the source file and the *-test.el files are collected and ran automatically based on the current file’s location.
  • when showing ERT results, keep cursor inside the source buffer
(defun elisp-ert-lookup-and-run-all ()
  "collect all local *-test.el files and run them."
  (interactive)
  (let* ((dir (file-name-directory (buffer-file-name)))
         (test-files (directory-files dir t "-test\\.el$"))
         (source-window (selected-window)))
    (if test-files
        (progn
          ;; load all tests and run ert
          (dolist (file test-files)
            (load-file file))
          (ert t)
          ;; immediately after showing the test report, forget all tests
          ;; this leads to all changes in tests being respected including removals of test functions
          (ert-delete-all-tests)
          ;; jump back into the source buffer (undoing the automatic jump by ert)
          (select-window source-window))
      (user-error "No *-test.el files found in %s" dir))))
1 Like