Book review: The Art of Unit Testing, 2nd edition (Roy Osherove)
After buying and reading the book Dependency Injection in .NET (Mark Seemann), I was about to buy The Art of Unit Testing (Roy Osherove), when I learned that the second edition will be available in a couple of months. I contacted Roy and Manning Publications Co were kind enough of providing me a complimentary copy of the Early Access Edition of The Art of Unit Testing, Second Edition, which I have read avidly in the last few days.
I was interested in the book because while I had worked with automated testing for the version 7.0 of my MZ-Tools add-in, I was not sure if I was doing it properly and I wanted to learn about integration testing / unit testing, the difference between fakes, mocks and stubs and the best practices. After reading the book, there is no doubt that Roy knows the stuff, and explains everything to make it clear:
- Part 1 explains the basics of unit testing and a first unit test.
- Part 2 explains the three kinds of testing: result testing, state testing and interaction testing, and how stubs are used for result / state testing and mocks are used for interaction testing. The last chapter of this part (and the first chapter of the next part) explains isolation (mocking) frameworks (as well as appendix B). It seems that not all isolation frameworks are created equal (constrained vs unconstrained) and the book explains quite well how they differ.
- Part 3 has one chapter to dig deeper into isolation frameworks and two chapters that I was very interested in: test hierarchies/organization and the pillars of good unit tests. The book puts an enormous effort in the importance of writing tests that are 1) Trustworthy 2) Maintainable and 3) Readable, and I couldn't agree more because I have written tests that don't follow those principles... this part of the book provides tons of good practices to the point of almost reaching perfectionism.
- Part 4 has two chapters: one devoted to integrating unit testing into the organization and other to working with legacy code (lacking unit testing). It is sad that books have to have such chapters because many developers and managers don't see the value of automated testing, thinking only about the short term costs and not about the QA in the long term. But since this is the reality, this part can help you a lot in that case.
I have enjoyed the book a lot. It seems that as part of his consulting job, Roy has seen tons of bad
practices (as any team leader has seen with any kind of code, sadly) and this edition is a new attempt to explain developers how to become artists of unit testing. If they fail, it won't be because of this excellent book.