837906b713cee0fa2945501a33430bd1f6697bab
[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.6 2003/08/04 17:04:49 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 test-cases that we are going to need to
17 ;;; perform our tests.  A test-case 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-test-case (test-case)
22   ((numbera :accessor numbera)
23    (numberb :accessor numberb))
24   (:documentation "Test test-case for math testing"))
25
26 ;;; Then we define a set-up method for the test-case.  This method is run
27 ;;; prior to perfoming any test with an instance of this test-case.  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 ((tcase math-test-case))
32   (setf (numbera tcase) 2)
33   (setf (numberb tcase) 3))
34
35 (def-test-method test-addition ((test math-test-case))
36   (let ((result (+ (numbera test) (numberb test))))
37     (assert-true (= result 5))))
38
39 (def-test-method test-subtraction ((test math-test-case))
40   (let ((result (- (numberb test) (numbera test))))
41     (assert-equal result 1)))
42
43 ;;; This method is meant to signal a failure
44 (def-test-method test-subtraction-2 ((test math-test-case))
45   (let ((result (- (numbera test) (numberb test))))
46     (assert-equal result 1)))
47
48 ;;;; Finally we can run our test suite and see how it performs.
49 (textui-test-run (make-instance 'math-test-case))
50