1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
5 ;;;; Name: example.lisp
6 ;;;; Purpose: Example file for XLTest
7 ;;;; Authors: Kevin Rosenberg and Craig Brozefsky
9 ;;;; Put in public domain by Kevin Rosenberg and onShore, Inc
10 ;;;; $Id: example.lisp,v 1.1 2003/08/04 06:00:01 kevin Exp $
11 ;;;; *************************************************************************
13 (defpackage #:xltest-example
18 (in-package #:xltest-example)
20 ;;; First we define some basic fixtures that we are going to need to
21 ;;; perform our tests. A fixture is a place to hold data we need
22 ;;; during testing. Often there are many test cases that use the same
23 ;;; data. Each of these test cases is an instance of a test-fixture.
25 (def-test-fixture math-fixture ()
30 (:documentation "Test fixture for math testing"))
32 ;;; Then we define a setup method for the fixture. This method is run
33 ;;; prior to perfoming any test with an instance of this fixture. It
34 ;;; should perform all initialization needed, and assume that it is starting
35 ;;; with a pristine environment, well to a point, use your head here.
37 (defmethod setup ((fix math-fixture))
38 (setf (numbera fix) 2)
39 (setf (numberb fix) 3))
41 ;;; Then we define a teardown method, which should return the instance
42 ;;; to it's original form and reset the environment. In this case
43 ;;; there is little for us to do since the fixture is quite static.
44 ;;; In other cases we may need to clear some database tables, or
45 ;;; otherwise get rid of state built up while perofmring the test.
46 ;;; Here we just return T.
48 (defmethod teardown ((fix math-fixture))
51 ;;; Once we hav a fixture we can start defining method on it which
52 ;;; will perform tests. These methods should take one argument, an
53 ;;; instance of the fixture. The method performs some operation and
54 ;;; then performs some tests to determine if the proper behavior
55 ;;; occured. If there is a failure to behave as excpeted the method
56 ;;; raises a test-failure object by calling the method FAILURE. This
57 ;;; is much like calling ERROR in that it stops processing that
58 ;;; method. Each method should only check for one aspect of behavior.
59 ;;; This way triggering one failure would not result in another
60 ;;; behavior check from being skipped. It does not matter what these
63 (defmethod test-addition ((test math-fixture))
64 (let ((result (+ (numbera test) (numberb test))))
65 (test-assert (= result 5))))
67 (defmethod test-subtraction ((test math-fixture))
68 (let ((result (- (numberb test) (numbera test))))
69 (assert-equal result 1)))
71 ;;; This method is meant to signal a failure
72 (defmethod test-subtraction-2 ((test math-fixture))
73 (let ((result (- (numbera test) (numberb test))))
74 (assert-equal result 1)))
77 ;;; Now we can create a test-suite. A test-suite contains a group of
78 ;;; test-cases (instances of test-fixture) and/or other test-suites.
79 ;;; We can specify which tests are in a test-suite when we define the
80 ;;; test-suite, or we can add them later. See the documentation and
81 ;;; argument list for make-test-case for details on how to specify a
84 (defparameter *manual-math-test-suite*
87 "Simple test suite for arithmetic operators."
88 '(("Addition Test" math-fixture
89 :test-thunk test-addition
90 :description "A simple test of the + operator")
91 ("Subtraction Test" math-fixture
92 :test-thunk test-subtraction
93 :description "A simple test of the - operator"))))
95 (add-test (make-test-case "Subtraction Test 2" 'math-fixture
96 :test-thunk 'test-subtraction-2
97 :description "A broken substraction test, should fail.")
98 *manual-math-test-suite*)
101 (defparameter *dynamic-math-test-suite* (make-test-suite 'math-fixture))
103 ;;;; Finally we can run our test suite and see how it performs.
104 (report-result (run-test *manual-math-test-suite*
105 :handle-errors t) :verbose t)
107 (report-result (run-test *dynamic-math-test-suite*
108 :handle-errors t) :verbose nil)