views:

3157

answers:

14

Most decent programmers know that unit testing is important, but may not know where to begin. For a long time I knew the benefits of unit testing, but could never seem to figure out where a good starting point was.

Most articles I read on the subject dealt with the high level theoretical, rather than the down-and-dirty practical. I've noticed that recently, some articles are appearing that create an application and then walk through where, and why, unit tests are applied to it.

These articles still haven't fully risen to the surface yet; after all, the theoretical articles are good, but they may not be the best thing to start with when trying to learn a new concept. What I want to know is, what is the best practical unit testing article or tutorial you know of? (language doesn't matter)

EDIT: I'm actually interested in amassing a list of tutorials, not complete books.

+17  A: 

Test Driven Development : By Example by Kent Beck, without hesitation.

Axelle Ziegler
Definitely the best introductory book on unit testing.
Bill the Lizard
+7  A: 
01
+1. I like this one. Short but detailed and easy to follow.
RichardOD
+11  A: 

The book "Working Effectively with Legacy Code" by Michael Feathers.

There is also an article from 2002 on the web, but it doesn't say much about testing.

starblue
+1  A: 

I started with CodeProject: Advanced Unit Testing and the other parts!

Gregor
+2  A: 

I don't know if this is the kind of article you're after, but this article from MSDN Magazine is one of the articles that got me kick started into practical unit testing.

Gerrie Schenck
+1  A: 

I found this one to be really good quite recent upto date with JUnit4

Writing JUnit Tests in NetBeans IDE

Mark Davidson
A: 

This excerpt from Robert Martin's Agile Software Development book is a good read and illustrates a session of pair programming and test-driven development:

http://www.objectmentor.com/resources/articles/xpepisode.htm

You also may want to take a look at this: http://jbrains.ca/permalink/208

rant

BTW, despite the flak Martin is currently getting from Joel and Jeff, I recommend his book. The principles in this book help writing cleaner code. You just shouldn't try to apply all those principles a priori, but only when the actual need arises. This point is often overlooked by the "architecture astronauts", who end up creating more complicated code than need be.

rant off

gclj5
Mike Woodhouse
+8  A: 

Although books, this two gems are a really easy read for beginners:

Pragmatic Unit Testing in Java with JUnit
Pragmatic Unit Testing in C# with NUnit

EricSchaefer
+1  A: 

Not trying to plug my own efforts, but I just completed a presentation at the recent NYC CodeCamp as an intro to TDD.

Even though I rarely recommend trying TDD as a way to get started with unit testing (TDD I find to be simpler to grasp AFTER you have written at least SOME unit test), you might still find these links related to the content to be of use:

Post that contains links to the ppt slide deck and the code download This slide deck contains an intro to unit testing, an understanding of why testing is important and the role it plays in the process.

Post that contains links to the recorded screencasts of the content From the screencasts, I recommend viewers select '#3 Studio Re-Recording' from the list provided as its the most concise.

If you are interested in reading a book, I personally found the previously-recommended 'Pragmatic Unit Testing in C# with NUnit' to be very approachable and great mix of concept with technical practice (which is usually the case with the 'Prag' series).

sbohlen
+4  A: 

Unit Testing Java Programs is a good tutorial that presents a small Java class, then shows the "wrong" way to test it, and the "right" way using JUnit.

Bill the Lizard
+2  A: 

Although it is not strictly a resource for beginners (but neither is, I think, the otherwise excellent "Working Effectively with Legacy Code"), I feel compelled to mention "xUnit Test Patterns" by Gerard Meszaros, which has some very good narratives about the benefits and constraints of unit testing, followed by a large catalog of patterns for keeping tests as they should be : simple, consistent across executions, readable, maintainable, etc. The book comes with a website on which most (if not all) of its contents are available at http://xunitpatterns.com/, although with a perhaps less practical layout than in the dead tree version.

Thomas Dufour
A: 

Simply learning Unit Testing might make you think how all this fits in the entire process. Make sure you pick a unit testing tool that is commonly used in the enterprise world along with a very simple mocking framework. I would also suggest you to read Test Driven Development: By Example, and keep the documentation of your test and mocking framework handy. This is a very interesting podcast that you might want to listen.

Perpetualcoder
A: 

Several good resources already posted - I think it's worth making sure that you differentiate between unit tests written after the code is built and tests written before. The former is more "traditional", intended to confirm that the code does what you think it's supposed to do, whereas the latter is more of a design tool, specifying in executable form what the code is expected to do.

Irrespective of your (or Joel's or Jeff's) opinion on the benefits or otherwise of the two approaches, keep in mind that they exist for different reasons and work in different ways. Heck, they often produce radically different code. So whatever your preference, figure out which direction any particular article is coming from, if only to minimise confusion!

Mike Woodhouse
+3  A: 

The bowling game episode, by Bob Martin. It's the transcript of the discussion of a pair of programmers implementing the scoring algorithm for the bowling game.

It's more about (Test-Driven Development), but TDD in based on unit-testing. The article shows how the developers first sketch out an UML diagram, start implementing and finally discover another design.

It obviously is not as exhaustive as "Test-Driven Development : by example", but it would be a good place to start.

philippe
This was an awesome example, exactly the sort of thing I was asking for. This comes a little late since I've been reviewing some of my older questions.
Soviut