Jim Newkirk got his 'day in the sun' to speak about Test Driven Development and the tool out in public promoting Test Driven Development and the tools support he's been involved with using Microsoft Visual Studio Team System.
He started out with a quick audience poll of how many people had heard of Test Driven Development (around 80%) and how many were actually using it (about 30%). So a clear victory for marketing over behaviour change there!
The Two Tenets of Test Driven Development:
Never write a single line of code unless you have a failing unit test. The goal is to take requirements and express them as test
Eliminate duplication
How to do TDD
Jim starts by blocking out 4 - 8 hour sessions of development. He spends 15 - 20 minutes at the start of each session thinking about what he is going to do and brainstorming a list of unit tests.
A key part is not to get hung up on completeness, you can always add more later. The purpose of the tests is to describe completion requirements.
The flow of a TDD session: Red, Green, Refactor
The process is:
- Start by writing a test for a new capability
- Compile
- Fix any compile errors
- Run the test and see it fail
- Write the code to make the test pass
- Refactor as needed (clean up any duplication)
The purpose is about how to use the functionality, not how to implement it! The process allows you to build confidence through having a set of tests that pass.
The most successful way to do test is to do it before the development. If you start it first then you need to think about how to test.
Features in Visual Studio Team Systems
Jim used a stack example to demonstrate the process of TDD as well as the support in Visual Stuido Team systems. The first test looked as follows:
[TestClass]
Public class StackFixture
{
[TestMethod]
Public void IsEmpty()
{
Stack stack = new Stack();
Assert.IsTrue(stack.IsEmpty);
}
}
So, the same approach as NUnit, just with new names!
One cool feature was writing a class name followed by a method name that didn't exist yet. After compiling, Jim used a 'smart tag' to choose to create the method stub inside the target class. It wrote this stub and had a 'NotImplementedException' inside it. This is functionality similar to Eclipse and is good to see.