r5451: *** empty log message ***
[xlunit.git] / example.lisp
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; ID:      $Id: example.lisp,v 1.4 2003/08/04 12:16:13 kevin Exp $
6 ;;;; Purpose: Example file for XLUnit
7 ;;;;
8 ;;;; *************************************************************************
9
10 (defpackage #:xlunit-example
11   (:use #:cl #:xlunit)
12   (:export #:math-test-suite))
13
14 (in-package #:xlunit-example)
15
16 ;;; First we define some basic fixtures that we are going to need to
17 ;;; perform our tests.  A fixture is a place to hold data we need
18 ;;; during testing.  Often there are many test cases that use the same
19 ;;; data.  Each of these test cases is an instance of a test-fixture.
20
21 (defclass math-fixture (test-fixture)
22   ((numbera :accessor numbera)
23    (numberb :accessor numberb))
24   (:documentation "Test fixture for math testing"))
25
26 ;;; Then we define a setup method for the fixture.  This method is run
27 ;;; prior to perfoming any test with an instance of this fixture.  It
28 ;;; should perform all initialization needed, and assume that it is starting
29 ;;; with a pristine environment, well to a point, use your head here.
30
31 (defmethod setup ((fix math-fixture))
32   (setf (numbera fix) 2)
33   (setf (numberb fix) 3))
34
35 ;;; Then we define a teardown method, which should return the instance
36 ;;; to it's original form and reset the environment.  In this case
37 ;;; there is little for us to do since the fixture is quite static.
38 ;;; In other cases we may need to clear some database tables, or
39 ;;; otherwise get rid of state built up while perofmring the test.
40 ;;; Here we just return T.
41
42 (defmethod teardown ((fix math-fixture))
43   t)
44
45 ;;; Once we hav a fixture we can start defining method on it which
46 ;;; will perform tests.  These methods should take one argument, an
47 ;;; instance of the fixture.  The method performs some operation and
48 ;;; then performs some tests to determine if the proper behavior
49 ;;; occured.  If there is a failure to behave as excpeted the method
50 ;;; raises a test-failure object by calling the method FAILURE.  This
51 ;;; is much like calling ERROR in that it stops processing that
52 ;;; method.  Each method should only check for one aspect of behavior.
53 ;;; This way triggering one failure would not result in another
54 ;;; behavior check from being skipped.  It does not matter what these
55 ;;; methods return
56
57 (defmethod test-addition ((test math-fixture))
58   (let ((result (+ (numbera test) (numberb test))))
59     (test-assert (= result 5))))
60
61 (defmethod test-subtraction ((test math-fixture))
62   (let ((result (- (numberb test) (numbera test))))
63     (assert-equal result 1)))
64
65 ;;; This method is meant to signal a failure
66 (defmethod test-subtraction-2 ((test math-fixture))
67   (let ((result (- (numbera test) (numberb test))))
68     (assert-equal result 1)))
69
70 ;;;; Finally we can run our test suite and see how it performs.
71 (text-testrunner (make-test-suite 'math-fixture))
72