views:

1004

answers:

5

After spending some hours reading about Unit Testing and test driven development here on StackOverflow and on other sites posts pointed me to, I know two things:

  1. I want to use it
  2. I don't know where to start

There are lots of good posts here about details, best practices etc, but what I am looking for is a beginners tutorial/introduction - maybe even a book.

Where do I start? Assume I know nothing. :)

+2  A: 

Watch unit testing related screencasts and videos at http://www.asp.net/learn/

And also check here (MSDN Webcasts related to Unit Testing):

http://www.google.com/search?q=unit+testing+site%3Amsevents.microsoft.com

Koistya Navin
+4  A: 

Get the book Pragmatic Unit Testing in C# with NUnit or Test-Driven Development with Microsoft.NET and work through one of them. The principles will be applicable to many different testing frameworks although the specific idioms may be different.

tvanfosson
Russ Cam
+2  A: 

Write a simple calculator class with a few methods for operations and a property for the indicator. Create unit tests as you write the class.

Then read Working Effectively With Legacy Code -- it will show you how to add unit-testing to old nasty projects.

zvolkov
+2  A: 

For me I got started by writing unit tests for some of our low-level string manipulation code. I created a new project to be the tests and linked in the string library. Then, i went thru each method and wrote unit tests for them - boundary conditions, expected outputs for known inputs, etc. At first it seemed like a mindless excersize but then.........

I found a BUG!!

I'm sold.

So now I'm adding to the testing project as i move higher up the foodchain in our libraries.

n8wrl
+2  A: 

I would download ReSharper (and the following plugins: Exceptional, Agent Smith, Agent Johnsson), the #1 tool to improve code quality and speed up development. Resharper is not free, but it IS worth the money ;)

Then download Xunit: http://xunit.codeplex.com

To create a test is simple:

public class MyObjectTest
{
  MyObject _objectToTest = new MyObject();

  [Fact]
  private void TestStart()
  {
    _objectToTest.Start();
    Assert.True(_objectToTest.IsStarted);
  }
}

ReSharper, integrated with xunit, will give you an icon next to each test method. The icon lets you test (and debug) the method directly in the IDE without you having to start the entire program. Can't be easier.

Most developers would say that Test driven development means that you create the test first and the object after it. I usually begins with creating the class, adding the mandatory methods to it and then create the test. It's a bit faster and you'll probably start to refactor the class when you've started to build the test.

When you create the class, spend most of the time document each method. The documentation should motivate why the method exists, what it expects from the arguments, and what it returns. If you can't describe it properly, you'll most likely have to refactor it.

jgauffin