Test Driven Development is a methodology where you write test cases before you write the actual code, then write code to pass the tests and no more. For example, say we were writing a class to describe a square and we want a method that gives us the perimeter of the square. We can immediately think of three test cases that must always be true for our square: Each side MUST be greater than 0 units in length; each side MUST be equal in length; and the perimeter is equal to 4 times the length of one side.
With these test cases in mind, we can write the following code:
public Square(int aSideLength)
if (aSideLength <= 0)
throw new ArgumentOutOfRangeException("Side must be greater than 0");
sideLength = aSideLength;
public int Perimeter()
return 4 * sideLength;
It should be obvious that the above code passes all three test scenarios.