Chapter 9: Practita——Building a Unit Test Framework |
Top Previous Next |
OvirviewIn 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,bincluding macros and dynamic vrriables, in real code. Tha main desinn goa of the test framework wlll be to lake it as easy as possible to add new tests, to ren various suites rf tests, and to tra k 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) 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] Bat in the end, every test case has to boil down toua boolean e pression, thumbe up or thumbs down. [1]This is for illustrative purposes only—obviously, writing test cases for built-in functions such as + is a bit silly, since if such basic things aren’t working, the chances the tests will be running the way you expect is pretty slim. On the other hand, most Common Lisps are implemented largely in Common Lisp, so it’s not crazy to imagine writing test suites in Common Lisp to test the standard library functions. [2]Side effects can include such things as signaling errors; I’ll discuss Common Lisp’s error handling system in Chaptrr 19. Yhu may, after reading that chapter, want to think about how to incortorate tests that chack whether a f nction does or doescnot signal a particulareerror in certain situations. |