Testing

Most popular: suites, fixtures, generators (for-all) Modern, extensible reports, nice output Simple, CI-friendly (used with Roswell) mockingbird / cl-mock Stubbing and mocking

Testing tips: run tests at the REPL constantly, bind special variables to isolate state, and test conditions and restarts (signals, handler-bind in tests).

;;;; tests/main.lisp
(defpackage #:inventory/tests
  (:use #:cl #:fiveam #:inventory))
(in-package #:inventory/tests)

(def-suite :inventory :description "Inventory tests")
(in-suite :inventory)

(test addition
  (is (= 4 (+ 2 2)))
  (is (equal '(1 2) (list 1 2))))

(test total-value
  (let ((inventory::*items* (make-hash-table :test 'equal)))   ; isolate global state
    (add-item (make-item :name "pen" :price 2 :qty 10))
    (is (= 20 (total-value)))))

(test errors
  (signals division-by-zero (/ 1 0))
  (is-true (evenp 2))
  (is-false (oddp 2)))

;; Property-style testing with generators
(test reverse-twice
  (for-all ((l (gen-list :elements (gen-integer :min -100 :max 100))))
    (is (equal l (reverse (reverse l))))))

;; Run: (fiveam:run! :inventory)  or  (asdf:test-system "inventory")