.NET Test-Driven Development
Has anyone got any pointers on how I should start doing test-driven development in .NET?
It’s something I’ve been meaning to look into for years now. I’ve got all the usual Visual Studio add-ons installed… NUnit, TestDriven.NET, etc. But no idea how to use them!
Usually I learn this stuff on the job, yet from most other developers I speak to – it’s all guess-work!
Any advice is much appreciated!



… and Mock Objects too! I keep hearing about them, but no idea how to use them!!
I just reading this CodeProject article about Test-Driven Development:
http://www.codeproject.com/KB/dotnet/tdd_in_dotnet.aspx
It was written back in 2003! I’m sure the principles are the same, but the technology must have advanced since then!?
Lee Kelleher
July 2, 2008 at 1:47 am
I’ve used NUnit quite a lot in the past, but don’t know if the way that I used it would be called TDD.
Basically I create a package (folder) called tests in each assembly and then create a set of tests for each component in the system. Then I write stubs for the code and complete the tests – at this point everything will fail because it returns null, or throws an ArgumentException. Then I just fix the code until all of the tests pass
Writing NUnit Text Fixtures is pretty easy, just a matter of using Nunit.Framework, adding a few attributes to some methods and then “Bob est ton Oncle”
Ross
July 2, 2008 at 8:34 am
@Ross, have you looked at xUnit?
I keep hearing that it’s the “next NUnit”, yada yada. Since I know nothing about using NUnit, should I skip on to xUnit? or go back to basics? (so I’d have an underlying knowledge)
There’s obviously a lot more community support for NUnit:
http://www.google.com/trends?q=nUnit%2C+xUnit
Lee Kelleher
July 2, 2008 at 11:45 am
Go with NUnit, it is simple and works, and, well, I’ve never seen xUnit
If you are using Team Server I believe Unit Testing is built right in.
I would doubt that xUnit has that many differences in approach from NUnit.
But be careful, all code has bugs, and adding more code through testing means potentially more bugs (in the tests)
Ross
July 2, 2008 at 1:03 pm
Create a new C# class library project. Then create a public class and mark it up with attribute [TestFixture]
Create a public void method next and mark that up with the attribute [Test]. If you compile this and then right click your project and click “Test with Nunit” you should get a new Gui with your test in it. Now it’s time to learn about asserts. It’s all awesomeness from here on!
Scott
September 25, 2008 at 11:50 pm