views:

101

answers:

4

I'm looking for a good introduction/tutorial for unit testing C#. Most tutorials I've come across so far have either been too basic to be useful or too complex for someone new to unit testing.

(Using Visual Studio 2008 Professional for Windows applications)

+2  A: 

Is it just a specific tool for which you're having trouble finding good tutorials? When I was new to the subject I found the NUnit tutorial to be a good starting point:

http://www.nunit.org/index.php?p=quickStart&r=2.4

Rhino Mocks would be good to learn as well to complement the unit testing framework:

http://stackoverflow.com/questions/185021/rhino-mocks-good-tutorials

David
It wasn't a particular tool, more about using unit testing in my projects in general.The NUnit tutorial does seem pretty useful, I'll give it a whirl, and have a look at Rhino Mocks once I'm up to speed.
Andy
+1  A: 

Hello,

Perhaps a book? I would recommend you the Pragmatic Unit Testing in C# with NUnit.

It's very complete in my opinion.

Bruno Costa
Thanks. I'm going to give NUnit a try and if I like it I'll definitely get hold of the book.
Andy
+3  A: 

Read The Art of Unit Testing by Roy Osherove. It is very good. alt text

P.K
+1 for a *VERY* good intro to Unit Testing; it's another plus that Roy Osherove (the author) is a fellow StackOverflow user--I've asked Unit Testing questions in the past which he's answered.
STW
...also consider reading "Clean Code" by Uncle Bob (Robert C. Martin). Unit-testing requires well-written code (unit-testing poor code can actually have very negative consequences).
STW
A: 

It was when I started reading about Moq that I realized unit testing didn't have to be painful. There are some good links near the bottom of the page as to how unit tests can be built with mocking.

One nice thing about using interfaces for controlled coupling and testing is that adding an interface to an existing code base is not a breaking change. I'm adding new features to some legacy code and I've been creating interfaces for existing classes so that the new features can be developed and tested in isolation. It's been working well so far and I plan to continue this style of testing on other projects. For me, the key was to avoid designing complex stub classes with lots of ugly conditional code to expose different cases for my tests. It got to the point where the test code was so complex that I couldn't be sure if it was the code or the test that was broken.

Dan Bryant