Ryan Rivest

  Home :: Contact :: Syndication  :: Login
  8 Posts :: 0 Stories :: 0 Comments :: 0 Trackbacks

Archives

Post Categories

I recently finished reading The Art of Unit Testing by Roy Osherove (published by Manning).

As the title implies, this book covers the topic of unit testing, which is still a mystery for a lot of developers. Unfortunately, I think that many believe unit testing to be a shadowy art that seems hard to grok, and even harder to practice.  The truth is, it’s not as hard as you might think, and once you understand what unit testing truly is, you may find yourself enjoying writing your unit tests as much as your production code.

The examples in the book are in .NET, so having an understanding of C# (or a similar language) will be helpful.  An understanding of .NET unit testing frameworks might be helpful as well, but it's definitely not necessary.  The book is ripe with great examples, and the author starts at the basics, so any developer should be able to understand unit testing with or without any sort of unit testing experience.

The book is organized into four parts, which I have outlined below:

1. Getting Started

Roy sets the foundation with a great introduction to the basics of unit testing. He defines what makes a good unit test, and what the differences are between unit and integration tests.

A brief introduction to Test Driven Development is also provided at the end of the first chapter. This is one area that I wish the book would have had more of an emphasis on, but I understand that the author didn't want to fray from the overall direction of the book.

The second half of part one walks you through setting up your first unit testing framework – NUnit.  Then the author tells you how to write your first unit tests and run them with the NUnit GUI test runner.

2. Core Techniques

Obviously we're not always working with a perfectly decoupled system that was built with unit testing in mind, and we don't always have the luxury to work on Greenfield projects. As developers, we spend a lot of time in the maintenance phase, making improvements to existing legacy code.

This is testing in the real world, and where fake objects come into play.

Roy introduces both kinds of fake objects - mocks and stubs. Prior to reading this part of the book, I didn't really know (or care) the difference between a mock and a stub. But we are introduced to both sides of the spectrum, including handwritten mocks and stubs.

There is also a great deal of time spent showing us how to refactor our designs to be more testable by including seams that allow us to easily swap dependencies with fakes.

We can finally understand how to use Isolation (mock object) Frameworks by the end of chapter 5 - which is dedicated entirely to them. There's an abundance of examples showing us how to utilize Rhino Mocks to create our fakes, and some time is spent with TypeMock Isolator as well.

3. The Test Code

Personally, this was my favorite part of the book. I really enjoyed these chapters because it's not an area that I've heard a lot about.  What are the pillars of good unit tests? In the author's words, it means writing tests that are:

  • Trustworthy
  • Maintainable
  • Readable

I haven’t seen a lot of writing on the topic of unit test maintainability, and sometimes I wonder if it’s a second-class citizen. Fortunately, there's a whole part of the book dedicated to what good unit tests are, and how you should organizing your tests.

4. Design and Process

The conclusion of the book comes with an interesting twist that is a little less technical, and more political. The chapter is called: Integrating unit testing into the organization, and the author speaks from a number of experiences with unit testing and some tips for how you might successfully introduce it into your organization.

If you've ever been in an organization that was starting to practice unit testing, you've probably seen how people initially react. It's one of those areas that is scary for a lot of people because they don't fully understand it. It's also hard to learn if you have no guidance, and it can significantly increase the development time for a project when you're starting.

Fortunately, the author arms us with guidelines for successfully introducing unit testing into our organizations. There's also an entire section called "Tough questions and answers," that is devoted entirely to helping to predict the hard questions that you will undoubtedly face when starting to discuss unit testing at work.

Finally, the last chapter is dedicated to writing tests for legacy code. Roy outlines a strategy for developing tests for existing systems that don't have any unit tests. He shows you how to prioritize the components based on different characteristics, so that you can figure out what to tackle first. Large legacy codebases can be overwhelming, and having a plan of attack will be greatly beneficial!

Overall, I thought the book was excellent. It's a great front to back overview of unit testing, ranging from the absolute basics, to more advanced topics like test hierarchies, writing maintainable tests, and more.  I wish this book was around when I was just starting to unit test.  I’m recommending it to all of my friends – beginners and veterans!

Learn more at Roy Osherove's blog, and the book website at The Art of Unit Testing

posted on Friday, July 03, 2009 12:05 AM