r5445: *** empty log message ***
[xlunit.git] / example.lisp
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:        example.lisp
6 ;;;; Purpose:     Example file for XLTest
7 ;;;; Authors:     Kevin Rosenberg and Craig Brozefsky
8 ;;;;
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 ;;;; *************************************************************************
12
13 (defpackage #:xltest-example
14   (:use #:cl #:xltest)
15   (:export
16    #:math-test-suite))
17
18 (in-package #:xltest-example)
19
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.
24
25 (def-test-fixture math-fixture ()
26   ((numbera
27     :accessor numbera)
28    (numberb
29     :accessor numberb))
30   (:documentation "Test fixture for math testing"))
31
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.
36
37 (defmethod setup ((fix math-fixture))
38   (setf (numbera fix) 2)
39   (setf (numberb fix) 3))
40
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.
47
48 (defmethod teardown ((fix math-fixture))
49   t)
50
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
61 ;;; methods return
62
63 (defmethod test-addition ((test math-fixture))
64   (let ((result (+ (numbera test) (numberb test))))
65     (test-assert (= result 5))))
66
67 (defmethod test-subtraction ((test math-fixture))
68   (let ((result (- (numberb test) (numbera test))))
69     (assert-equal result 1)))
70
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)))
75
76
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
82 ;;; test-case.
83
84 (defparameter *manual-math-test-suite*
85     (make-test-suite
86      "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"))))
94
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*)
99
100
101 (defparameter *dynamic-math-test-suite* (make-test-suite 'math-fixture))
102
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)
106
107 (report-result (run-test *dynamic-math-test-suite*
108                          :handle-errors t) :verbose nil)