Author: Lasse Koskela
Audience: Java programmers and others new to testing
Reviewer: Alex Armstrong
Unit testing is easy - don't you believe it.
This book is a guide to writing good unit tests. It uses Java as its example language but it isn't too difficult to follow if you don't know Java and the ideas are general ones. It is important to know that this book isn't about Test Driven Development, although there are references to the idea through much of the book.
The problem that this book tackles is how to write tests that are as good as, perhaps even better than, the code they "protect". One of the standard objections to unit testing is that it simply adds to the amount of code that has to be written and if there is a constant density of bugs then adding testing code adds bugs as much as it enforces some idea of correctness.
The book is divided into three parts. The first is a sort of lightening introduction to testing. Chapter 1 argues why you should test and says a few words about test and behavior driven development. Chapter 2 discusses some very basic ideas about what makes good code. It makes points that every programmer should know about structure and how code should be easy to understand and readable by a human. If you do know all of this then this chapter might make you think that the level of the book is too low and the approach too naive - stick with it as it gets increasingly interesting.
Chapter 3 introduces the ideas of test double, stubs, mocks and so on. Again if you already know these ideas and probably have your own favourite mocking library then you might find some of this a bit too simple. If you don't know it then this is a good and clear introduction to why you want to do these things.
The second part of the book - Catalog - is indeed a catalog of test "smells" that indicate that you could do better. If you don't know the term "smell" it comes from extreme programming where it refers to signs and signals that you pick up on when code isn't quite right. The catalog is divided into three chapters - Readability, Maintainability and Trustworthiness. Each chapter has about ten smells each with an example and what to do about it.
This is the real core of the book and the part that you either value or think that it's all a waste of space. Some of the smells are specialized versions of things you will encounter in general programming. For example, it covers magic numbers and discusses the use of numeric values rather than named constants that give you an idea of what the constants are about. Some are more specialized and really only occur in the testing domain. This is where you can in principle save yourself from making lots of mistakes before you learn the best way to do things.
Part 3, titled Diversions, consists of three chapters that don't fit anywhere else. Chapter 7 is about testable design and this considers what makes code testable. Again this is a distillation of advice you will find elsewhere in the context of writing good code - avoid complex private methods, avoid static methods, favour composition over inheritance and so on. Chapter 8 is about writing test in other JVM languages - mainly Groovy. Chapter 9 is on speeding up test execution and again much of this is the obvious application of good sense - avoid file I/O and if you can't use a RAM disk.
Overall this is very readable as long as you don't mind anecdotes that ramble in a very comfortable way. Occasionally you will find yourself thinking, "I'm a programmer of course I know this", but clearly there is a chance you might not. The book also has a tendency towards "motivational" talk and you will find lots of quotes to back up the expressed opinions and observations.
This is a good book on the topic but if you are fairly well read on general code quality or testing then you probably won't find much that comes as a surprise. However if you are new to testing then a copy of this book will get you in the right frame of mind to do the job properly. Recommended.