Copyright © 2003-2005, Peter Seibel
9. Practical: Building a Unit Test Framework
In this chapter you’ll return to cutting code and develop a simple unit testing framework for Lisp. This will give you a chance to use some of the features you’ve learned about since Chapter 3, including macros and dynamic variables, in real code.
The main design goal of the test framework will be to make it as easy as possible to add new tests, to run various suites of tests, and to track down test failures. For now you’ll focus on designing a framework you can use during interactive development.
The key feature of an automated testing framework is that the framework is responsible for telling you whether all the tests passed. You don’t want to spend your time slogging through test output checking answers when the computer can do it much more quickly and accurately. Consequently, each test case must be an expression that yields a boolean value—true or false, pass or fail. For instance, if you were writing tests for the built-in **+**
function, these might be reasonable test cases:1
(= (+ 1 2) 3)
(= (+ 1 2 3) 6)
(= (+ -1 -3) -4)
Functions that have side effects will be tested slightly differently—you’ll have to call the function and then check for evidence of the expected side effects.2 But in the end, every test case has to boil down to a boolean expression, thumbs up or thumbs down.