views:

1486

answers:

8

Hi everyone,

I'm interested in adding some tests to my code. I write primarily in C#, but I'm having difficulty finding examples for unit testing that aren't trivial (i.e. unit test a function to capitalize a string). Does anyone know of a good book that explains how to write unit tests for non-trivial examples in c#? I've seen the pragmatic programmer, but that's kind of dated as unit testing has only recently come into vogue and its from 2005. Thanks for any help.

+7  A: 

This book by Kent Beck is the best I have read so far. Besides this, specific to C#, you might want to try this book from Roy Osherove. I think he writes well about unit testing with C# in mind.

Perpetualcoder
Consider changing your 'this' links to book titles. It will make this answer thread a lot more friendly to external searches and what not. :)
Nathan Taylor
+9  A: 

There is Pragmatic Unit Testing in C# with NUnit, the Pragmatic books are usually good. Test Driven Development: By Example is the unit testing book, but its examples are in Java.

Rob Conery's Storefront webcasts contain some decent examples of putting unit tests together.

Garry Shutler
+5  A: 

How about Test-Driven Development in Microsoft .NET

I read it this year and remember it being basic to intermediate. The first example in the book is the implementation of a stack and all the unit tests that go along with it...

I just broke it out from the bookshelf...from the Foreword:

"...With this book, the pieces missing from my book are included" - Kent Beck

Jason Punyon
+4  A: 

I thought this one was decent for teaching the mechanics of using NUnit and creating/using unit tests:

Pragmatic Unit Testing in C# with NUnit

But it's not really about Test Driven Development per se.

I liked this book for explaining how to use unit tests in your day-to-day stuff:

Working Effecitvely With Legacy Code

Although it is more about getting a handle on legacy code, it also spends a lot of time discussing TDD and how to get existing code into a test harness. (It's also not specifically about C#)

Dana
+1  A: 

This isn't a direct answer to your question, but if the code you are testing is non-trivial, then you may have a problem with the design. Unit tests should test a very small amount of code. If it is hard to test, it may need to be broken into smaller pieces that are easy to test.

NotDan
The functions are small, however they reference more complicated objects. Its testing things that depend on the state of other objects that I find confusing
Steve
You might look into mocking the complicated objects. Then you can test the methods separately from the objects and it is easier to manage the state. You should also search for TDD on stackoverflow, it has lots of resources to get you started.
NotDan
+1  A: 

Best book I've read is not 100% complete yet but it is available for free online. It provides a great overview of mock unit testing philosophy and also gives practical advice on how to implement these theories. Give this a try. It's no fast answer to your question but may open your eyes.

http://www.mockobjects.com/book/

Otter
+12  A: 

"The Art of Unit Testing" (written by me) is now available. I wrote in it all the things I was missing in the other books.

http://www.ArtOfUnitTesting.com

Roy

RoyOsherove
thanks, I'll take a look
Steve
I smell a shameless plug! ;)
Nathan Taylor
Shameless plug or not, this combined with Working Effecitvely With Legacy Code pretty much covers everything on a practical level. 2nd best book I've read so far in 2009! (meant in a good way)
FinnNk
+1  A: 

For a start, read Kent Beck "Test-Driven Development By Example" chapter with tdd-way implementation of a stack.

And next, I would recommend roy osherove ndc09 videos about unit testing. Very very good material and everything you need to know about tdd and unit testing on one place.

Cheers

p.s. you can find ndc videos on twitter just search for #ndc09

Marko