NOTE: Before you continue you need to install MS Visual Studio Team Edition for Testers.

I’ll explain how to use TDD with MS Visual Studio using a real life example. As stated in my previous post, there are a couple of steps that you can check to make sure that you are aligned with TDD.

1st – Requirement

Consider a bag that implements the following methods IsEmpty, Pop, Push. These are the behavior considerations about the bag.

  • If the bag is empty; the IsEmpty method must return true. If not it must return true.
  • If you pop an item into the bag, the IsEmpty method must return false.
  • If you push an item from the bag , where the item count equals to one it should return true.

2nd – Test List

  • Create a Bag and check that isEmpty is true.
  • Push a single object into the Bag and check that isEmpty is false.
  • Push a single object and then pop it from the bag and check that isEmpty is true

3rd – Choose you first test

For this example we’ll use Create a Bag and check that isEmpty is true.”.

4th – Follow the implementation list that I

mentioned in the other post

Step 0 – Create you solution
  1. Open Microsoft Visual
  2. Go to File -> New -> Project
  3. Select Other Project Types -> Visual Studio Solutions -> Blank Solution
  4. Type TDD Example in the Name textbox.
  5. Go to File -> Add -> New Project
  6. Select Visual C# -> Test Projects -> Test Projects
  7. Type Bag.Test in the Name textbox.
Step 1 – Write Test Code

Write the following code as your test code

[TestMethod]

public void CheckBagIsEmpty()

{

Bag myBag = new Bag();

Assert.AreEqual(true, myBag.IsEmpty);

}

Step 2 – Compile the test code

Nothing to say, just press Shift + Ctrl + B.

Step 3 – Implement enough to compile
  1. Add a class to your test project called bag.
  2. And write the following code

public class Bag

{

public bool IsEmpty

{

get { return false; }

}

}

Step 4 – Run the test and see it fail
  1. Press Shift + Alt + X.
  2. See it fails with the following error message “Assert.AreEqual failed. Expected:<True>, Actual:<False>.”
Step 5 – Implement just enough to make test pass

Replace isEmpty method with the following code

public bool IsEmpty

{

get { return true; }

}

Step 6 – Run the test and see it pass.
  1. Press Shift + Alt + X.
  2. See it pass.
  3. You’ll get 1/1 passed as result
Step 7 – Refactor for clarity and to eliminate duplications

You bag class should look like

public class Bag

{

private bool _isEmpty;

public bool IsEmpty

{

get { return _isEmpty; }

set { _isEmpty = value; }

}

public Bag()

{

this._isEmpty = true;

}

}

Re run your test and see it pass, with the refactored bag class.

Summary

Now you know how to write a simple test on Microsoft Visual Studio Team Edition For Testers, you’ve seen the development lifecycle of a requirement with VS for Testers.

Leave a Reply