Unit testing in Lisp

There are some frameworks and libraries available for doing unit tests in Common Lisp. You’ll find a list for example in Wikipedia.

I have decided to have a look at lisp-unit from Chris Riesbeck, Associate Professor in the Department of Electrical Engineering and Computer Science at Northwestern University.

It is very easy to use lisp-unit. You define a package for your tests and use the lisp-unit package to have access to the defined assertion forms. In your defined package you define your tests that use the functions you want to test. lisp-unit supports test-driven development, so in your tests, you can use functions that are not defined yet. The following will test a function named ‘add’.

(defpackage :mhnfoo-tests
  (:use :common-lisp :lisp-unit :mhnfoo))

(in-package :mhnfoo-tests)

(define-test mhnfoo
  (assert-true (eql 3 (add 1 2))))

Here is a very simple implementation of the function ‘add’.

(defpackage :mhnfoo
  (:use :common-lisp)
  (:export #:add))

(in-package :mhnfoo)

(defun add (x y) (+ x y))

Now you can run all the tests in your test package as follows. I’m using CLISP on Microsoft Windows 7 for this example. The ‘[6]>’ and ‘[7]>’ are the prompts of CLISP.

[6]> (lisp-unit:run-all-tests :mhnfoo-tests)
MHNFOO: 1 assertions passed, 0 failed.


As you see, it is very easy to do unit testing in Common Lisp. And lisp-unit is just one file to load.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s