Saturday, June 9, 2012

Book: The Art of Unit Testing

The Art of Unit TestingThe Art of Unit Testing is a great book by Roy Osherove, the TDD Guru, which tries to instill us the philosophy of writing unit tests in the fastest way without neglecting the basics and the important fundamentals, and he does that perfectly. The book is written with examples in .NET with NUnit as the Unit testing framework and Rhino Mocks as the Isolation framework which are free and open source. It’s suitable for both newbies and experienced – everyone can find something that he can learn about in this book.

The book is divided to 4 parts, but I prefer to divide it into 2 parts:

The first part is all about Unit Testing basics – the definition of “unit test” and what is its goals. It explains that a unit test must meet the following requirements:

  • Trustworthy
  • Readable
  • Maintainable

Without one of these requirements, your test is no good. Then he shows how to write a simple unit test. Later on it starts to get a little deeper, and provides a definition to “stub”. He starts to talk a lot about the design of your code, about how to break dependencies and then how to inject those dependencies as stubs. Starts to get interesting, huh?

The second part (by my division), Roy describes the importance of organizing your tests – how to locate them in terms of solution structure, projects and classes. He discusses on making a testing infrastructure in your team, on integrating your unit tests in your Continuous integration process so that your tests will run on every commit. After that, Roy explains in detail the three requirements that a unit test must have (mentioned above). While reaching the end of the book, he talk about how to integrate unit tests in your organization, which tools you may find useful and what you should be aware of. The last chapter of that book, which in my opinion was the most interesting and enriching, is about working with legacy code – how do test code that its design isn’t testable and preventing you to inject dependencies? How you refactor this kind of code? And many more philosophic topics. Super interesting. Of course it was impossible to cover the whole topic; therefor Roy makes references to Michael Feathers' book Working Effectively with Legacy Code.

TDD

While I was reading this book, I was sort of a beginner developer but I already had a little bit experience in writing unit tests and practicing TDD, therefor I found the first chapters a bit less relevant for me. But it was still nice to understand a little deeper – why it’s important to have a good coverage to your code, and to have readable tests. What I specially loved about this book, besides it is suitable both for beginners and experienced, is that it’s manages to be very practical and yet enough theoretical to be interesting. In addition it’s written in very comfortable English (not so high), so it’s not that boring. :)

I recommend everyone to read this book – from newbies to seniors, team leaders, and even hardcore TDD programmers who want to refresh their basics about TDD.

No comments:

Post a Comment