Click here to Skip to main content
15,892,480 members
Articles / Programming Languages / C#
Tip/Trick

Test Driven Development with Visual Studio 2010

Rate me:
Please Sign up or sign in to vote.
4.00/5 (3 votes)
4 Apr 2014CPOL2 min read 12.3K   10   4
Test Driven Development with Visual Studio 2010

I am quite new to TDD but gone through few tutorials to learn how to get most out of it. Before diving into some code, let’s recap what we’re trying to achieve and what’s new:

What is Test Driven Development?

Test Driven Development (TDD) is a technique where you write unit tests and its change of approach for developers to shift from writing code, then testing it (with unit tests), to writing the tests first and then writing the code to satisfy the tests afterwards. An ideal TDD approach is to write all the unit tests for a specific feature or features, run the unit tests and get 100% failure, then start writing code to satisfy the tests until 100% pass.

for more details http://msdn.microsoft.com/en-us/library/aa730844(v=vs.80).aspx

Walkthrough

Let’s walkthrough a simple scenario to to implement a Shopping Cart class in C# using a TDD approach. First of all simply create a new project (in this case I created a C# Class Library but it could be anything).

image

Now add a unit test file (Test menu, New Test) called ShoppingCartTest:

image

I’ve let this create a new Test Project (MyShopTests) as I like to keep my unit tests and code separate. The solution now looks like this:

image

Let’s add a test method into that class.

image

You are seeing red lines because class doesn't exist, To Generate it Hover the mouse over the line or press Control . and you’ll see this option:

image

I normally use Generate new Type as generate class will create new class using default settings, but i want to create a new class in separate project rather than unit test one

image

Only thing I changed is Project & leave other options to default

image

Note red line disappear, Now lets write a test method that allows me to add items to shopping cart

image

Follow same steps above to generate Item Class:

image

Generate properties for class to store relevant info

image

Now we call the method we want to test, AddItem. Of course AddItem doesn’t exist, so let’s Control . on AddItem to generate the method:

image

There’s only one option, which is what I want, so let’s take it :-). The last step is to actually call Assert comparing the expected and actual results:

image

We’re all done – we’ve written a unit test, and we are now able to run it, although we haven’t written a line of the implementation code ourselves. So let’s run it:

image

It failed!, which is good and reason for this is we haven't implemented method yet:

image

Remove NotImplemenationExeption & re-run your tests again & it should pass now

Repeat

You follow this pattern to write unit tests, generate required code & run test. This approach lets us focus more on required functionality rather than writing lines of code which is never going to be used

I hope this provides a useful overview of how you can start to apply TDD techniques

Cheers,
Nabeel

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Pakistan Pakistan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionSimple and good Pin
Filip Nne6-Apr-14 23:15
Filip Nne6-Apr-14 23:15 
GeneralGood one Pin
Govindaraj Rangaraj4-Apr-14 20:47
Govindaraj Rangaraj4-Apr-14 20:47 
GeneralMy vote of 4 Pin
Nitij4-Apr-14 5:56
professionalNitij4-Apr-14 5:56 
QuestionAssert Pin
Duncan Edwards Jones4-Apr-14 5:28
professionalDuncan Edwards Jones4-Apr-14 5:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.