Visual Studio 2010: Test Driven Development

Version: 1.1.0

Description

Visual Studio 2010 brings with it several enhancements to help cut development friction and enable the developer to focus on the task at hand: writing high-quality code. In the following exercises we'll highlight several of the new features that the TDD developer can use to enhance his/her development cadence. Visual Studio helps your cadence by cutting the number of keystrokes to accomplish frequently performed tasks, speeds navigation through your solution, and enables you to use test frameworks other than MSTest.

Overview

Test Driven Development (TDD), also frequently referred to as Test Driven Design, is a development methodology where developers create software by first writing a unit test, then writing the actual system code to make the unit test pass. The unit test can be viewed as a small specification around how the system should behave; writing it first helps the developer to focus on only writing enough code to make the test pass, thereby helping ensure a tight, lightweight system which is specifically focused meeting on the documented requirements.

TDD follows a cadence of “Red, Green, Refactor.” Red refers to the visual display of a failing test – the test you write first will not pass because you have not yet written any code for it. Green refers to the step of writing just enough code in your system to make your unit test pass – your test runner’s UI will now show that test passing with a green icon. Refactor refers to the step of refactoring your code so it is tighter, cleaner, and more flexible. This cycle is repeated constantly throughout a TDD developer’s workday.

Note:
This lab is not meant to teach you how to work with TDD; instead, it is intended to highlight Visual Studio 2010’s support for working in TDD. If you are interested in learning more about the TDD methodology and its benefits then you should look to the following books as a starting point:

Test Driven Development in Microsoft .NET by James Newkirk and Alexei Voronstov, ISBN 0735619484

Pragmatic Unit Testing in C# with NUnit, 2nd Edition by Andy Hunt, Dave Thomas, and Matt Hargett, ISBN 0977616673

A critical aspect of TDD is developer cadence. Getting into a productive development rhythm (the elusive “zone”) involves having the right mindset, but also working with tools that assist the developer in working rapidly and with as little friction as possible.

Visual Studio 2010 brings with it several enhancements to help cut development friction and enable the developer to focus on the task: writing high-quality code. In the following exercises, you will learn several of the new features that the TDD developer can use to enhance his/her development cadence. VS10 helps your cadence by cutting the number of keystrokes to accomplish frequently performed tasks, speeds navigation through your solution, and enables you to use test frameworks other than MSTest.

To demonstrate the new features you will build an implementation of a stack in a test-first manner, showing how tests drive the design and implementation of a SimpleStack class.

Note:
In computer science, a stack is an abstract data type and data structure based on the principle of Last In First Out (LIFO).

If you are unfamiliar with the concept of a stack as a data structure, or just need a quick refresher, please see the full Wikipedia article for a more thorough definition.

Objectives

In this Hands-On Lab, you will learn how to:

  • Use the new Smart Tag actions to save keystrokes by letting the IDE generate small bits of code for you.
  • Use Quick Search to move around in the code base with just a few keystrokes.

System Requirements

You must have the following items to complete this lab:

  • Microsoft Visual Studio 2010
  • .NET Framework 4.0

Setup

All the requisites for this lab are verified using the Configuration Wizard. To make sure that everything is correctly configured, follow these steps:

Note:
To perform the setup steps you need to run the scripts in a command window with administrator privileges.

  1. Run the Configuration Wizard for the Training Kit if you have not done it previously. To do this, browse to Source\Setup folder of this lab, and double-click the Dependencies.dep file. Install any pre-requisites that are missing (rescanning if necessary) and complete the wizard.

    Note:
    The Configuration Wizard is used for checking dependencies and setting up the environment. If the Configuration Wizard is not installed on your machine, you must install it running the DependencyChecker.msi file located on the %VS2010TKInstallationFolder%\Assets\DependencyChecker folder (e.g. by default the Training Kit is installed under C:\VS2010TrainingKit).

    For convenience, much of the code you will be managing along this lab is available as Visual Studio code snippets. The Dependencies.dep file launches the Visual Studio installer file that installs the code snippets.

Exercises

This Hands-On Lab comprises the following exercises:

  1. Start test-driving your design by writing the tests and them making them go from Red (failing) to Green (passing).
  2. Refactor your code, run the tests, and ensure they all pass.

Estimated time to complete this lab: 60 minutes.

Note:
This lab shows code examples in both C# and VB.NET; however, screen shots reflect only C# actions. Each action shown in the screenshots has a similar set of steps for VB.NET.

Next Step

Exercise 1: Red, Green…