How to create and test a method in Elisp

☕️ 1 min read

Declaration of a function called add-nums who receives two parameters called a and b, after that will sum and return.

(defun add-nums (a b) (+ a b))
(add-nums 10 10)



Notice it isn’t required to pass a return statement in (+ a b) and commas to separate the parameters.

In the following lines I define the following steps:

  • Load package ert to test the method add-nums
  • Declara the ert-deftest add-nums-pos will be evaluated later the return
  • should be equal return of add-nums of 10+10 to 20
(require 'ert)
(ert-deftest add-nums-pos ()
   (equal (add-nums 10 10) 20)))



Notice if you try to evaluate with C-c C-c will throws a Symbol's function definition is void: add-nums-pos because the ert-deftest hides the add-nums-pos to avoid rewrite the method outside the scope.

In this case I have just one test I’ll use the M-x + ert (ert-run-interactively) to evaluate the add-nums-pos.