;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*- ;;;; ************************************************************************* ;;;; FILE IDENTIFICATION ;;;; ;;;; ID: $Id: example.lisp,v 1.4 2003/08/04 12:16:13 kevin Exp $ ;;;; Purpose: Example file for XLUnit ;;;; ;;;; ************************************************************************* (defpackage #:xlunit-example (:use #:cl #:xlunit) (:export #:math-test-suite)) (in-package #:xlunit-example) ;;; First we define some basic fixtures that we are going to need to ;;; perform our tests. A fixture is a place to hold data we need ;;; during testing. Often there are many test cases that use the same ;;; data. Each of these test cases is an instance of a test-fixture. (defclass math-fixture (test-fixture) ((numbera :accessor numbera) (numberb :accessor numberb)) (:documentation "Test fixture for math testing")) ;;; Then we define a setup method for the fixture. This method is run ;;; prior to perfoming any test with an instance of this fixture. It ;;; should perform all initialization needed, and assume that it is starting ;;; with a pristine environment, well to a point, use your head here. (defmethod setup ((fix math-fixture)) (setf (numbera fix) 2) (setf (numberb fix) 3)) ;;; Then we define a teardown method, which should return the instance ;;; to it's original form and reset the environment. In this case ;;; there is little for us to do since the fixture is quite static. ;;; In other cases we may need to clear some database tables, or ;;; otherwise get rid of state built up while perofmring the test. ;;; Here we just return T. (defmethod teardown ((fix math-fixture)) t) ;;; Once we hav a fixture we can start defining method on it which ;;; will perform tests. These methods should take one argument, an ;;; instance of the fixture. The method performs some operation and ;;; then performs some tests to determine if the proper behavior ;;; occured. If there is a failure to behave as excpeted the method ;;; raises a test-failure object by calling the method FAILURE. This ;;; is much like calling ERROR in that it stops processing that ;;; method. Each method should only check for one aspect of behavior. ;;; This way triggering one failure would not result in another ;;; behavior check from being skipped. It does not matter what these ;;; methods return (defmethod test-addition ((test math-fixture)) (let ((result (+ (numbera test) (numberb test)))) (test-assert (= result 5)))) (defmethod test-subtraction ((test math-fixture)) (let ((result (- (numberb test) (numbera test)))) (assert-equal result 1))) ;;; This method is meant to signal a failure (defmethod test-subtraction-2 ((test math-fixture)) (let ((result (- (numbera test) (numberb test)))) (assert-equal result 1))) ;;;; Finally we can run our test suite and see how it performs. (text-testrunner (make-test-suite 'math-fixture))