I want to check for a warning but not sure how to go about it.

My warning is:

Warning (my-func-file-not-found): /path/to/somefile.txt

This is created with display-warning 'my-func-file-not-found.

I have tried this:

(should (with-temp-buffer
            (my-func "init/nf")
            (search-forward "file-not-found" nil t)))

and this

(should-error (my-func "init/nf") :type 'my-func-file-not-found )
  • emacs.stackexchange.com/tags/elisp/info
    – Drew
    Commented May 7 at 0:38
  • Do you have a function that is supposed to produce the warning under certain circurmstances and you are trying to test that indeed that is the case? If so, you should provide the function (or maybe a simplified example of one) that you are trying to test and specify under what circurmstances it should produce the warning.
    – NickD
    Commented May 7 at 14:07
  • @NickD Glad to see you here again :) . I have a big function and inside that function I have a dolist that checks to make sure the files exist. If the files do not exist then produce a warning. I want to make sure that warning is being produced when I run (my-func "path/to/no/file") how do I test for this with ERT ?
    – ritchie
    Commented May 7 at 23:07

1 Answer 1


Here's some code (as babel source blocks in an Org mode file, but you can extract them and run them in an Elisp file instead) - I hope it's self-explanatory, but I have to post-and-run, so I will add some verbiage later on.

* Code                                                                                                        :noexport:

Define the function to test:

#+name: my-func
#+begin_src elisp 
  (defun my-func (file)
    (if (not (file-exists-p file))
        (display-warning 'my-func-file-not-found file)
      ;; else file exists: do something with it - here I just return the string "yes"

#+RESULTS: my-func
: my-func

Write the test:

#+name: my-func-test
#+begin_src elisp
  (require 'ert)

  (defun check-for-warning (msg)
    (with-current-buffer "*Warnings*"
      (goto-char (point-min))
      (search-forward msg nil t)))

  (ert-deftest test-my-func/warning-produced ()
    "Call my-func with non-existent file and check that the warning is produced."
    (let ((fname "/some/non/existent/path"))
      ;;; This (killing a buffer) is something that tests are *NOT* supposed to do,
      ;;; but I can't think of a clean way to *test* a side-effect producing function like `display-warning'.
      ;; if the *Warnings* buffer exists, kill it.
      (condition-case x
        (kill-buffer "*Warnings*")
        ('error t))
      ;; run the function to test
      (my-func fname)
      ;; check that the side-effect has happened
      (should (check-for-warning (format "Warning (my-func-file-not-found): %s" fname)))))


Run the test:
#+name: run-tests
#+begin_src elisp
 (ert "test-my-func/")

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.