Book Review: Growing Object Oriented Software Guided By Tests by Steve Freeman and Nat Pryce

The second programming book I read on holiday was Growing Object Oriented Software Guided By Tests by Steve Freeman and Nat Pryce. To sum up early: its a very, very good book on TDD.

When I first came across TDD, I immediately saw it as a good idea. Don't write any code without first having a test to cover it and you get automated tests to run any time which prove your system works and document how. Awesome! Getting stuck in with TDD threw up a few questions I couldn't find easy answers to, though:

  • How much up-front design should I be doing, and how much should be 'designed' through the act of writing tests?
  • How far through the system's layers should I be attempting to test?
  • Should I write tests in the same coding style as production code?
  • What can tests tell you about the way you're designing a system?
  • What should and shouldn't I try and mock in a test?
  • Are there any best practices for writing tests to get the most value from them?

Well, Growing Object Oriented Software answers all these questions. It's excellently structured, starting off with simple examples each described in lots of detail, and moving steadily on to more complex examples and advanced topics. Mocking is dealt with in great detail (the authors wrote JMock), and there's excellent advice on stucturing tests for readability, structuring objects for testability, getting started with a new project, working with legacy systems, testing asynchronous and distributed systems, and lots more.

The book contains a lot of detail on mocking using JMock, and sometimes reads a bit like a JMock instruction book. The .NET equivilent to JMock is of course NMock, which in my opinion isn't as nice to use as Moq, but that doesn't affect the quality of the advice or the examples, as it's all pretty portable. Similarly the examples are all in Java, but everything applies just as well to C#.

Based on the testing style in the book, I've started refactoring my tests for SimpleDI; as an example of making tests more readable, this:

public void TestDefaultTypesAreRegisteredWithNoAssemblyFilter()
    IInjectionContainer container = SetupContainer(_defaultEmptyConfig);


...has been refactored into this:

public void TestDefaultTypesAreRegisteredWithNoAssemblyFilter()
    Assert.That(AContainer(WithAnEmptyXmlConfiguration()), HasTheExpectedDefaultRegistrations());
} I think that's a lot more readable. I wonder if a programming language with that type of syntax would work? In any case, I really like it for tests and find it much more expressive.

This is an excellent book. As with Appenticeship Patterns, I think it's improved how I do my job, so that's got to be a good thing :)

Print | posted @ Wednesday, May 18, 2011 7:25 PM

Comments on this entry:

No comments posted yet.

Post A Comment