Thursday, October 22, 2009

The Art of Unit Testing review

The Art of Unit Testing: with Examples in .NET is a very complete book on the testing paradigm, which guides the naive developer from the start of his journey in unit testing to more elaborate techniques. It starts from manual integration testing and explains the path of the technology evolution towards testing automation frameworks and programmatic stubs/mocks generation.
As the title says, the book is focused on .NET technology and presents C# code samples. However, like in the case of other software engineering books, the knowledge and art taught here are areas of general interest, and these patterns can be successfully applied in every language where a xUnit tool does exist. Especially since this book starts testing components without any external library or tool.

Troughout the book, there are valid topics treated extensively and without specific assumptions:
  • Stubs and mocks: the author starts from manual generation (similar to self-shunting and fake object pattern) and slowly shift to programmatic one. He also recommends to specify expectations on only one mock per test (and how many stubs you like), a suggestion that I rarely seen correctly followed in production.
  • Test code organization: how to map test cases to production code is a fundamental question that your codebase organization has to answer, to allow isolation of components. Other interesting topics are automation of test runs and refactoring of test classes in an Api that can be shared in testing of similar components.
  • Pillars of good tests: the author explains trustworthiness, maintainability and readability of tests. This means your tests should pass (or, more importantly, fail) consistently, you should be able to change the specifications of your system without throwing away tests, and you should maintain a good standard in naming and asserting to ensure your test code is of the same quality of the production code it covers.
  • Integrating unit testing into the development process: testing is a technique that should be applied constantly during a project development. A chapter is dedicated to help you evangelize these practices in your organization, answering the common questions of the people who hinder unit testing.
  • Writing tests for legacy code: one of the most difficult problems in unit testing, testing a system that is not separated into units, is tackled with a pragmatic mindset. Obviously legacy code is a problem that cannot be solved completely and with a general approach, but often a 20% testing solution is enough to perceive benefits.
  • List of .NET tools for web, ui, database testing and much more; this section will be interesting for the .NET developers only, and in fact I skipped it.

This book is probably the most complete guide for the test-infected beginner in circulation nowadays. You could give it to a developer that does not know anything about unit testing, apart the name of this practice, and he would be able to read it all and earn an honest theorical preparation on this topic.
A thing that I not liked is the little focus on Test-Driven Development, as it is probably the best way to ensure you design a testable application and keep writing testable code. I guess probably in a beginner reading there is no need to stress TDD before learning how to write good tests.
As a seasoned test-infected developer, I encountered pretty boring parts that were targeted to a wider audience, for example where the differences of state based and interaction based testing are explained. I think that the technical level does not rise much even towards the last chapters and this is indeed a pro for the inexperienced developer, but an annoying thing for me since I have already written hundred of tests in my life and already solved some of these problems.
So if you want to master unit testing, go read this book to learn the theory. And then, start practicing: after you will have written a thousands of tests your perspective will be very different, and you will have experienced a paradigm shift in respect to when you wrote code that just works.

2 comments:

Daniel Marbach said...

Hello
I totally agree! I read the book a while ago and must say that even though I'm not new to unit testing and especially TDD the book gave a great insight and offered good problem solution approaches.

Daniel

Giorgio said...

Daniel,
thanks for adding feedback to this review. There is a shortage of complete testing books.

ShareThis