Click here to Skip to main content
Click here to Skip to main content

Tagged as

Test Driven Development with Visual Studio 2010

, 4 Apr 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
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 Smile | :) . 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)

Share

About the Author

Nabeel ur Rehman

Pakistan Pakistan
No Biography provided

Comments and Discussions

 
QuestionSimple and good PinmemberFilip Nne7-Apr-14 0:15 
GeneralGood one PinmemberGovindaraj Rangaraj4-Apr-14 21:47 
GeneralMy vote of 4 PinprofessionalNitij4-Apr-14 6:56 
QuestionAssert PinprofessionalDuncan Edwards Jones4-Apr-14 6:28 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141223.1 | Last Updated 4 Apr 2014
Article Copyright 2014 by Nabeel ur Rehman
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid