I have a few confessions to make. First, I still read books, although for technical books I’ve switched to electronic formats instead of dead trees thanks to my long-running Netbook. But I just prefer the format of concise and complete information logically grouped together. Blogs and sites like StackOverflow are nice for specific topics, but even article series in blogs often cannot compete with the layout of books.
My second confession is that I do not practice Unit testing. Sure, I’ve spent some time reading about Unit Testing and played around with both NUnit and xUnit.net, but I never made tests that were more complicated than calling a function and asserting on the result. Mainly because I never understood more complicated concepts, I was never sure about the difference between unit and integration testing, and I thought that Mock Framework is black arcane magic.
Mainly because that’s what most blog articles in the internet tell you. There are plenty of articles among the lines of “Assert(4,Add(2,2))” but that’s about it. There are plenty of other articles about Unit testing, but I did not find any “Tutorial” that goes all the way from a simple assert to a full-blown solution involving stubs, a mock object and a database.
Until two weeks ago. I was browsing Manning for another book (C# in Depth, Second Edition by Jon Skeet) and saw Roy Osherove’s The Art Of Unit Testing. I was looking through the reviews at Amazon and decided to buy it – and I am absolutely thrilled by it!
First, it’s a very practical book. Roy doesn’t have many long-winded explanations, but instead he briefly introduces a concept, then presents some (C#) code, and then explains a bit more. This is great, because it allows the reader to take baby-steps. He starts writing Test Code without a Unit Test Framework to introduce the concept, but then quickly switches to NUnit throughout the rest of the book. That’s a nice little touch because for people who have absolutely no idea what problem Unit Test frameworks solve, this should explain it.
It then goes on to some more complicated (but more real-world) scenarios, i.e. involving testing a function that normally calls the file system. Again, Roy first illustrates how you could do it entirely manually (briefly covering dependency injection) before he moves on using a Framework. Only after laying all the groundwork he starts involving a Mock Framework – Rhino.Mocks – and starts with relatively simple scenarios as well.
This may sound like a “How to use NUnit and Rhino.Mocks” book, but it isn’t. As said, Roy explains all the concepts and starts building completely manual solutions at first, which means that all concepts should be known and you should easily be able to adapt it to whatever Unit Test and Mock Frameworks you use.
And that’s the beauty of this book: It’s about concepts, explained through practical examples. If you know what a Mock is and how it’s different from a Stub, if you know the difference between Unit and Integration testing, if you know how to approach any given problem, it does not matter what framework you’re using.
The little icing on the cake is the chapter about “Working with legacy code” which explains how you can add unit testing into existing applications, which can often be overwhelming if the legacy application is old, big, badly documented and mainly held together with duct tape and prayers, which means that no one wants to touch it.
Now, no good review is complete without listing some negative points. (Side note: Any review that is absolutely and 100% positive is not a review, but a marketing publication and therefore useless): Mainly, I would have wished a bit more about unit testing user interfaces. Neither ASP.net nor WinForms nor WPF is covered, although there is a whole Appendix at least listing additional tools and frameworks. I think there would be some room in a second edition to make a mostly complete Step-By-Step-Tutorial even more complete.
Conclusion: It’s great that Roy Osherove is the author of this book, because he is not just an expert on one specific framework, but he rather knows about the whole concepts and foundations of unit testing. That results in a well-balanced and complete book. He uses NUnit and Rhino.Mocks only as a “device” to explain the concepts instead of making them the “stars” of this book.
I can wholeheartedly recommend it.