Art of Unit Testing 2nd edition

Art of Unit Testing 2nd edition

I originally bought this book on Fab’s recommendation. I had read the XUnit Test Patterns book and I liked it, but I thought it was rather large and cumbersome. It was 900 pages, so I was looking for something smaller and lightweight that I could recommend to people. I asked Fab about it and this is what she recommended. I went out and bought the 2nd edition not realizing that the 3rd edition was about to be released. So this is a review of the 2nd edition.

First things first

The first thing I will say is that at 292 pages, The Art of Unit Testing (AUT) is certainly shorter than XUnit Test Patterns (XTP), but I’m not convinced it is an easier read. I think AUT makes some assumptions about the skill of the reader. I personally found the C# examples difficult to follow (I am not a C# developer). The examples in XTP, I found much easier to understand. XTP seemed to explain things in a much simpler way. Perhaps that is why it is so much longer.

Now that is out of the way

Even though I found the examples hard to follow, there is a lot to like about AUT. Overall I really like the tone of the book. It’s more pragmatic than dogmatic. Roy talks a lot about how his views have evolved over the years, particularly with regard to the first edition. I’m sure the third edition will probably have some new recommendations and opinions.

Part 1 – Getting Started

The first chapter which defines unit testing and talks about the qualities of a good unit test is a must-read chapter. Roy does a very good job of explaining why unit testing is important, what a good unit test looks like and the difference between unit testing and integration testing. He also briefly mentions and discusses Test Driven Design (TDD).

A unit test is an automated piece of code that invokes the unit of work being tested, and then checks some assumptions about a single end result of that unit. A unit test is almost always written using a unit testing framework. It can be written easily and runs quickly. It’s trustworthy, readable, and maintainable. It’s consistent in its results as long as production code hasn’t changed.

Updated And Final Definition 1.2 on page 11

Part 2 – Core Techniques

Chapter 3 on test stubs is excellent! Its treatment of seams is very thorough. It offers various ideas on how to override certain behaviors while testing. It also offers suggestions on refactorings to make adding seams easier. Chapter 4, which introduces mocks is also quite good. It does a great job of differentiating between mocks and test stubs and why you would use one over the other and how they complement each other. Chapters 5 and 6 delve into various C# mocking frameworks. Being a LabVIEW Developer I didn’t really find those chapters that useful.

Part 3 -The Test Code

Chapter 7 is Test Hierarchies and Organization. It covers everything about naming, storing, organizing, finding, and automating tests. Chapter 8 is all about Trustworthy, Maintainable, and Readable Tests. It covers everything needed to make sure your tests don’t become a burden in the future. My only complaint about Chapter 8 is that I would put readability first. It’s hard to even assess the other two if the test isn’t readable.

Part 4 – Design and Process

Chapter 9 discusses how to integrate tests into your organization and how to get management and your colleagues on board. Chapter 10 discusses how te deal with Legacy Code. It discusses a few useful strategies and tools. Chapter 11 deals with designing for testability. It references back to the seams in Chapter 3.

Who should read this book

I still like both books and still recommend both. However, I think XTP is a much better book for beginners and for those managing large amounts of tests (It’s got lots of advice in that arena). AUT covers unit testing at a much more advanced level. AUT is much better for those thinking about Test Driven Design and exploring how to put seams into their code. I find it better for more advanced programmers and those already familiar with XUnit Testing in general.