3fc9e3a2ce65a1cb3cd7c15a2291f29899bf87a0
[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.5 2003/08/04 16:13:58 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-case.
20
21 (defclass math-fixture (test-case)
22   ((numbera :accessor numbera)
23    (numberb :accessor numberb))
24   (:documentation "Test fixture for math testing"))
25
26 ;;; Then we define a set-up 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 set-up ((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 tear-down ((fix math-fixture))
43   t)
44
45 (def-test-method test-addition ((test math-fixture))
46   (let ((result (+ (numbera test) (numberb test))))
47     (test-assert (= result 5))))
48
49 (def-test-method test-subtraction ((test math-fixture))
50   (let ((result (- (numberb test) (numbera test))))
51     (assert-equal result 1)))
52
53 ;;; This method is meant to signal a failure
54 (def-test-method test-subtraction-2 ((test math-fixture))
55   (let ((result (- (numbera test) (numberb test))))
56     (assert-equal result 1)))
57
58 ;;;; Finally we can run our test suite and see how it performs.
59 (textui-test-run (make-test-suite 'math-fixture))
60