Next: , Previous: , Up: How to Test   [Contents][Index]


9.2.2.2 How to Organize Tests with Packages

Tests are grouped internally by the current package, so that a set of tests can be defined for one package of code without interfering with tests for other packages.

If your code is being defined in cl-user, which is common when learning Common Lisp, but not for production-level code, then you should define your tests in cl-user as well.

If your code is being defined in its own package, you should define your tests either in that same package, or in another package for test code. The latter approach has the advantage of making sure that your tests have access to only the exported symbols of your code package.

For example, if you were defining a ‘date’ package, your date.lisp file would look like this:

(defpackage :date
      (:use :common-lisp)
      (:export #:date->string #:string->date))

    (in-package :date)

    (defun date->string (date) ...)
    (defun string->date (string) ...)

Your date-tests.lisp file would look like this:

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

    (in-package :date-tests)

    (define-test date->string
      (assert-true (string= ... (date->string ...)))
      ...)
    ...

You could then run all your ‘date’ tests in the test package:

(in-package :date-tests)

    (run-tests)

Alternately, you could run all your date tests from any package with:

(lisp-unit:run-all-tests :date-tests)