Tuesday, September 29, 2009

Book Review: Art of Unit Testing

image Somehow this year has been one where I spend a lot time reading. Last week I finished Roy Osherove's The Art of Unit Testing, and I've almost finished reading Kent Beck's TDD by Example. After that, I'll jump right into Robert (Uncle Bob) Martin's Clean Code; A Handbook of Agile Software Craftsmanship.

I met Roy about two years ago when I, Don Smith and Olaf Conijn attended a late-night dinner during the 2007 TechED Europe in Barcelona. The funny thing is that I don't even remember what his sessions were about, but I won't forget the fact that he finished them with some hilarious guitar playing. I do remember though some guys from the company behind TypeMock stalking him. And apparently that worked; he is now one of the company's most visible employees. Since then he has blogged a lot of about Test Driven Development and Unit Testing, so I was really eager to read his recent book, The Art of Unit Testing.

Let me start by making it very clear that this book is not about Test Driven Development. One of my colleagues recommended it when somebody else asked about a good book about TDD. If you're looking for that, refer to Kent Beck's TDD by Example, Jimmy Nilsson's Applying Domain Driven Design or the many blog posts written by Jeremy D. Miller. At least, those are the ones that helped me get started.

The book does a great job in explaining the advantages of mocking frameworks in comparison to hand-written stubs and fakes. And since Roy is working for TypeMock these days, I was really expecting him to plug their product TypeMock Isolater throughout the entire book. But I'm not sure whether he was already working for that company at the time he had written the book, I never found him suggesting one product was better than the other.

One thing I didn't like were some his examples in chapter 6. From my point of view, he was demonstrating how to end up with obscure tests. Especially his suggestion to refactor all kinds of shared logic to a general setup method or a test base-class causes the stuff you need to know to understand a test to end up on lots of different places. xUnit Patterns has some much better solutions for solving such problems. And as in many test-related books and articles, it seems that almost nobody is mentioning the patterns of all patterns, the Test Data Builder. That one has proved to really help keep my unit tests readable.

Also, if Roy is ever going to publish another edition, it would be nice to add an intro on Behavior Driven Development (BDD) as well. BDD really helps to ease the thinking process of writing proper specifications.

All in all, I think this book deserves a score of 3.5 (on a scale from 1 to 5). In essence it is a great introduction to everything you need to know to get started with unit testing. Especially chapters 7 and later provide very useful guidance on how to get started and how to deal with the more difficult challenges, both technical as organizational. He shares a lot of his own experiences, and I particularly liked the ones where he failed because he's not afraid to share the lessons he learned himself. Granted, Gerard Meszaros' xUnit Patterns contains a lot more in-depth coverage of proper test automation, but Roy's book manages to cover all aspects in a very readable form.