Foreword to the Second Edition
The year must have been 2009. I was speaking at the Norwegian Developers Conference in Oslo. (Ah, Oslo in June!) The event was held in a huge sports arena. The conference organizers divided the bleachers into sections, built stages in front of them, and draped them with thick black cloth in order to create eight different session rooms. I remember I was just about finished with my talk, which was about TDD, or SOLID, or astronomy, or something, when suddenly, from the stage next to me, came this loud and raucous singing and guitar playing.
The drapes were such that I was able to peer around them and see the fellow on the stage next to mine, who was making all the noise. Of course, it was Roy Osherove.
Now, those of you who know me know that breaking into song in the middle of a technical talk about software is something that I might just do, if the mood struck me. So as I turned back to my audience, I thought to myself that this Osherove fellow was a kindred spirit, and Id have to get to know him better.
And getting to know him better is just what I did. In fact, he made a significant contribution to my most recent book The Clean Coder and spent three days with me co-teaching a TDD class. My experiences with Roy have all been very positive, and I hope there are many more.
I predict that your experience with Roy, in the reading of this book, will be very positive as well because this book is something special.
Have you ever read a Michener novel? I havent; but Ive been told that they all start at the atom. The book youre holding isnt a James Michener novel, but it does start at the atomthe atom of unit testing.
Dont be misled as you thumb through the early pages. This is not a mere introduction to unit testing. It starts that way, and if youre experienced you can skim those first chapters. As the book progresses, the chapters start to build on each other into a rather startling accumulation of depth. Indeed, as I read the last chapter (not knowing it was the last chapter) I thought to myself that the next chapter would be dealing with world peacebecause, I mean, where else can you go after solving the problem of introducing unit testing into obstinate organizations with old legacy systems?
This book is technicaldeeply technical. Theres a lot of code. Thats a good thing. But Roy doesnt restrict himself to the technical. From time to time he pulls out his guitar and breaks into song as he tells anecdotes from his professional past or waxes philosophical about the meaning of design or the definition of integration. He seems to relish in regaling us with stories about some of the things he did really badly in the deep, dark past of 2006.
Oh, and dont be too concerned that the code is all in C#. I mean, who can tell the difference between C# and Java anyway? Right? And besides, it just doesnt matter. He may use C# as a vehicle to communicate his intent, but the lessons in this book also apply to Java, C, Ruby, Python, PHP, or any other programming language (except, perhaps COBOL).
If youre a newcomer to unit testing and test-driven development, or if youre an old hand at it, youll find this book has something for you. So get ready for a treat as Roy sings you the song The Art of Unit Testing.
And Roy, please tune that guitar!
R OBERT C. M ARTIN (U NCLE B OB ) CLEANCODER.COM
Foreword to the First Edition
When Roy Osherove told me that he was working on a book about unit testing, I was very happy to hear it. The testing meme has been rising in the industry for years, but there has been a relative dearth of material available about unit testing. When I look at my bookshelf, I see books that are about test-driven development specifically and books about testing in general, but until now there has been no comprehensive reference for unit testingno book that introduces the topic and guides the reader from first steps to widely accepted best practices. The fact that this is true is stunning. Unit testing isnt a new practice. How did we get to this point?
Its almost a clich to say that we work in a very young industry, but its true. Mathematicians laid the foundations of our work less than 100 years ago, but weve only had hardware fast enough to exploit their insights for the last 60 years. There was an initial gap between theory and practice in our industry, and were only now discovering how it has impacted our field.
In the early days, machine cycles were expensive. We ran programs in batches. Programmers had a scheduled time slot, and they had to punch their programs into decks of cards and walk them to the machine room. If your program wasnt right, you lost your time, so you desk-checked your program with pencil and paper, mentally working out all of the scenarios, all of the edge cases. I doubt the notion of automated unit testing was even imaginable. Why use the machine for testing when you could use it to solve the problems it was meant to solve? Scarcity kept us in the dark.
Later, machines became faster and we became intoxicated with interactive computing. We could just type in code and change it on a whim. The idea of desk-checking code faded away, and we lost some of the discipline of the early years. We knew programming was hard, but that just meant that we had to spend more time at the computer, changing lines and symbols until we found the magical incantation that worked.