Home > TDD > Test-Driven Development (TDD) Principals

Test-Driven Development (TDD) Principals

Following text in a MSDN article is the best description of required steps for test-driven development using Microsoft Visual Studio. It is not a walkthrough, instead it express the principals:

MSDN (http://msdn.microsoft.com/en-us/library/aa730844(VS.80).aspx):

  1. Understand the requirements of the story, work item, or feature that you are working on.
  2. Red: Create a test and make it fail.
    1. Imagine how the new code should be called and write the test as if the code already existed. You will not get IntelliSense because the new method does not yet exist.
    2. Create the new production code stub. Write just enough code so that it compiles.
    3. Run the test. It should fail. This is a calibration measure to ensure that your test is calling the correct code and that the code is not working by accident. This is a meaningful failure, and you expect it to fail.
  3. Green: Make the test pass by any means necessary.
    1. Write the production code to make the test pass. Keep it simple.
    2. Some advocate the hard-coding of the expected return value first to verify that the test correctly detects success. This varies from practitioner to practitioner.
    3. If you’ve written the code so that the test passes as intended, you are finished. You do not have to write more code speculatively. The test is the objective definition of “done.” The phrase “You Ain’t Gonna Need It” (YAGNI) is often used to veto unnecessary work. If new functionality is still needed, then another test is needed. Make this one test pass and continue.
    4. When the test passes, you might want to run all tests up to this point to build confidence that everything else is still working.
  4. Refactor: Change the code to remove duplication in your project and to improve the design while ensuring that all tests still pass.
    1. Remove duplication caused by the addition of the new functionality.
    2. Make design changes to improve the overall solution.
    3. After each refactoring, rerun all the tests to ensure that they all still pass.
  5. Repeat the cycle. Each cycle should be very short, and a typical hour should contain many Red/Green/Refactor cycles.


And I like following advices quite useful from (http://msdn.microsoft.com/en-us/library/ms379625.aspx):

  • Avoid creating dependencies between tests such that tests need to run in a particular order. Each test should be autonomous.
  • Use test initialization code to verify that test cleanup executed successfully and re-run the cleanup before executing a test if it did not run.
  • Write tests before writing the any production code implementation.
  • Create one test class corresponding to each class within the production code. This simplifies the test organization and makes it easy to choose where to places each test.
  • Use Visual Studio to generate the initial test project. This will significantly reduce the number of steps needed when manually setting up a test project and associating it to the production project.
  • Avoid creating other machine dependent tests such as tests dependent on a particular directory path.
  • Create mock objects to test interfaces. Mock objects are implemented within a test project to verify that the API matches the required functionality.
  • Verify that all tests run successfully before moving on to creating a new test. That way you ensure that you fix code immediately upon breaking it.
  • Maximize the number of tests that can be run unattended. Make absolutely certain that there is no reasonable unattended testing solution before relying solely on manual testing.


To read a walkthrough for creating a unit test using VSTT refer following articles:

  1. A Unit Testing Walkthrough with Visual Studio Team Test
  2. Unit Testing and Generating Source Code for Unit Test Frameworks Using Visual Studio 2005 Team System
Categories: TDD
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

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

%d bloggers like this: