C# .NET Test Driven Development with Visual Studio 2005

As of late I have been stuck in C++ world. MFC/Win32, pointers and HRESULT’s. Ugh. Earlier this week I got a chance to get back to C# and .NET (aka My Roots).

I needed to create a class library for a project, basically from scratch, since there was no existing library created. I started to go at it, and about 2 minutes into it, I was like, wait, let me try to do this test first. Test Driven Development (TDD) is one of the pillars of the Agile development methodology. In the past, there has always been existing code, that was written Non-TDD, so basically what you would end up doing is adding tests to this existing code. When doing that, you kind of get into a rut where you find it easier (at least you think it is easier), to add new code, then write tests after.

So, I fired up Visual Studio 2005, created a solution, added a test project, with a test class, and one method, and started writing a test, basically on how I would think I would exercise my non existing class library. By doing this, it really makes it easier in my opinion to practice TDD.

Run the test, doesn’t even compile, why? because I haven’t even created my class library project yet. No compile = Tests Fail. What I need to do is make my tests pass. So I add just enough (but not to much) to make it pass.

I keep adding to my class library so I can get my tests to pass, and after a while, I have a nice tight class library for my project, fully tested, great code coverage, and more confidence in my code.

What is really nice, is that when I actually needed to use the class from a webpage, I basically copied my unit test code into the web page and it works, just as expected.

I’d have to say that with Visual Studio 2005, and .NET languages, doing TDD is really kind of fun. People will always say, “well you spend more time writing tests than code”, and well, yes, that might be true the first time you are writing your class library, if a bug does happen to come up, you basically have everything in place to track it down, whereas if you do not have tests, then you spend your time debugging and tracking down things for hours. A little time up front saves you tons of time down the road, and you become way more confident in your code base.

If you haven’t tried TDD with VS2005, go for it!! I bet you will really enjoy it!

Advertisements

One thought on “C# .NET Test Driven Development with Visual Studio 2005”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s