Thursday, October 11, 2012

[Tutorial] Test Driven Development with Visual Studio 2012
Part1: Introduction to TDD

In one of my blog posts series, I showed you how to increase the quality of your code and how to make it much easier to maintain by applying the S.O.L.I.D. principles. I then focused on the Liskov Segregation Principle (LSP) by using Code Contracts in .NET 4.0. This blog series is going to talk about Test Driven Development (TDD) in an Agile environment using the latest Visual Studio 2012 features and how that is going to help you to be more efficient and productive.

The goal of this series will be to show you some of the new features within Visual Studio 2012, that are going to help you when trying to apply the TDD approach. The first parts of the series are more theoretical, while as the last part will be fully practical, since we are going to implement some example code using TDD.

Contrary to traditional software development methodologies and processes, where writing unit test is often done as last step after code implementation, TDD consists of writing unit test upfront as first step in the software development cycle. No development can be started without having implemented the corresponding unit tests. You understand that TDD has large impacts on the organization of your teams.

The following are the different steps when applying Test Driven Development :


  1. Write a unit test for a new functionality, then validate that the test fails, since its implementation has not been realized
  2. Implement the minimum code necessary to pass the test, then validate that the test passes successfully, meaning that the expected behavior is now provided by the implementation
  3. Refactor and optimize your code, the unit tests ensure the coherence of the functionalities

Using TDD helps in obtaining a code source that is very reliable, predictable, robust - and after refactoring - highly optimized. Unit tests ensure a correct behavior, independently from where it is going to be used, resulting in a code source that will work as expected under any circumstances.

For being able to create good unit tests, you have at first to think about the conception and software design of your application. You must not hurry into doing your implementations, before being clear about your objectives (which should be true for any development, but sadly often it is not).

Conceptual errors can be detected and resolved much quicker and in a much more efficient way. As explained the implementation is started only after the conceptual phase has been validated and tested via thorough unit tests. In this case, your unit tests become much than just tests. They become some sort of general specification, describing  “units of functionality” for your application.

When refactoring and optimizing your code, you may restructure your code without any risks, since all your modifications are now verifiable. Unit tests ensure that there are no technical or functional regressions and furthermore the coherence of behavior. They validate that your application will always behave in the same way, if they are executed with success.

Additionally if you combine TDD with Extreme Programming (XP) and pair programming, you obtain a code with a very high degree of quality.

The next blog post in the series is going to talk about some the new features in Visual Studio 2012, that allow you to apply TDD quickly and easily, stay tuned!


No comments: