views:

210

answers:

3

I've been tasked with writing a short document about unit testing. Basically guidance for devs who are required to write unit tests for the first time, and are somewhat unfamiliar with it.

My first response was sure I can do that, but realized that there are whole books dedicated to the subject. I'm not quite sure how to distill the topic down to a few pages, and I sure don't want to spend a lot of time writing a book report.

I thought maybe someone might already have a document for devs new to Unit Tests and TDD, or a reference to a real good website with one.

+1  A: 

There are a ton of links on Google, so I'm not going to give you any specific links; but for me, one of the biggest draws of unit testing is code confidence.

Of bugs you find in code, how many are due to your code not performing the way you thought you programmed it? Sure, we run through our cursory use tests as we program it, but we hardly ever exhaust all the possible inputs or branches in the code, and that is where unit testing comes in.

As your test coverage increases, so does your confidence that your code is working the way it's supposed to, so in that respect unit tests are almost a form of self-documentation.

The other large benefit for me is regression testing. I can't imagine any manager who wouldn't be pleased at the aspect of bugs that never reappear, because they are tested to not be there on any new builds.

I suppose you could treat the above 4 paragraphs as a short document if you want ;)

Mark Rushakoff
+1: I Googled for "short intro to TDD". Out of the first 2 pages there seemed to be 4 good short intro's to TDD.
S.Lott
-1: Question asked for "a document for devs new to Unit Tests and TDD, or a reference to a real good website with one". Yours is a good answer to a different question.
John Saunders
+1  A: 

You're right, this is a pretty big topic, but having recently done this myself, I'll throw in my 2c.

First off, skip the philosophizing about "why", or at least keep it to a minimum. Most people get it - unit testing is good, it's best practices, it's safer, etc.

Concentrate on the "how". When we introduced unit tests to our devs (2 senior and 5 intermediate), it was boggling how badly they wrote tests. The "unit" part of unit testing can't be stressed enough, and getting them to structure their tests properly will be an initial hurdle. I actually ended up writing my 5 laws of writing unit tests which are still being followed at my company, and it helped immensely. Once they understood structuring, the actual test writing came more naturally. The more guidance on "how" to write good tests, the easier it will be for your devs to do it properly.

Have you picked a toolset yet? (Test framework, mocking library, build tools, etc). If so, awesome - keep your examples concentrated on your selected tools.

womp
+2  A: 

In a few pages, you won't have time to cover anything in depth, so I'd suggest briefly outlining the core ideas (from the reasons behind doing unit testing, to specific ways of doing it like dependency injection), then include a lengthy list of "Further Reading" type stuff - books, blog posts, videos, etc.

One of the things that really made unit testing click for me was these set of Tech Talks by Misko Hevery from Google:

http://www.youtube.com/watch?v=wEhu57pih5w

http://www.youtube.com/watch?v=RlfLCWKxHJ0

http://www.youtube.com/watch?v=-FRm3VPhseI

http://www.youtube.com/watch?v=4F72VULWFvc

Zack Elan