Test Driven Development in Visual Studio 2010 Team Foundation Server

by Dmitry Kirsanov 27. December 2011 22:50

Team Foundation Server 2010This is the 3rd post in a series of articles about Visual Studio Team Foundation Server 2010 for developers. And this time it is about test driven development. Previous one is available here.

 

Test Driven Development is the concept, which dictates creating unit tests before actually writing the code. To help you to understand this, let’s assume that you are writing the calculator program. And that whole calculation process is done by a separate class named Calculator with functions such as “Addition”, “Subtraction”, “Multiplication” and “Division”.

Now, according to Test Driven Development process, you should first create the test project, and write your tests for each of these functions. Since you know what result should be given for what input (like – “2 + 2 = 4”) you can make your tests to assert, that if you are running the Addition function with parameters of 2 and 2, the result is 4. If the result is different or exception occurs – the test is failed.

So, we might say that Test Driven Development allows you to make sure you’ve solved the problem with your code, and lets you to focus on that. You emphasize the strategy over the tactics.

Test Driven Development has both advantages and disadvantages, just like any other concept. The main disadvantage is such, that it enforces you to have clear vision of the object prior to starting the work on the module or procedure. The simple thing of giving the task to subordinate developer to write the needed function, becomes more complex, as someone else (with the vision) could be required to write the test module first and perhaps to document it.

However, the advantages of Test Driven Development become obvious in the long run. When developers come and go, the project evolves and grows, codebase is changed in various places and it’s hard to tell whether change in one place would ruin something in another part of the project. Over time it becomes much harder to write unit tests for already written functionality, as even authors forget the purpose and limitations of their code. Writing tests before the code could decrease the overall time spent on testing, especially if you have more than 4 developers in your team and start early with using Team Foundation Server 2010 to perform unit tests on each and every build.

I would even go as far as to state, that test driven development allows you to structure your coding and eliminate the frustration of not knowing what step to do next, so TDD optimizes the whole process of development and makes it smooth.

Also, when your code is about to be consumed by 3rd party developers, for example when you have n-tier architecture and you are testing your dynamic link libraries, then your unit tests serve as samples for those who wants to use your code in their solutions. It really saves time during the process of documenting your product.

In my opinion, the hardest part in embracing the Test Driven Development, is to start. It’s not usual practice and for many established developers it’s like building the house starting with roof. But in fact, it’s like building with description, with the story. The test project is telling you the story about your yet-to-be-written code – how it will behave and what it will do. Creating test modules is quite creative process.

In the following video I am showing you the basic concept of Test Driven Development as it’s implemented in Microsoft Visual Studio 2010 and Team Foundation Server. I also explain what is code coverage and how it helps you to find out what parts of your projects and of your code are the most risky, as they are not tested.

In future articles about Team Foundation Server 2010 for developers we’ll dig further into unit tests, so if you intend to learn more about the subject, watch this short video and subscribe for notifications about future posts (either “Newspaper” or RSS, I would recommend Google Reader).

In the next article about Team Foundation Server, we will talk about the Test Automation – how to build Coded UI tests, Web tests and Load tests, which then could be used by TFS on every server-side build of your code. If you’d like to be notified about that article publication – subscribe to “newsletter” or my YouTube channel, as it will be accompanied with video walkthrough as well.

Team Foundation Server 2010 for Developers, part 3 - Unit Tests
blog comments powered by Disqus

Month List