Test-driven development made easy

How many software development projects actually start with writing unit test cases? None perhaps. Writing unit test cases is often an after-thought, typically by the same developers who implemented the same classes in .NET. In Visual Studio Team Edition for Software Developer and Software Tester, you could be more disciplined about writing unit test cases before a single line of code is actually written.

Check out the code refactoring feature available in C#. Code refactoring can be used in a number of scenario, some of them are listed below:

  • renaming classes
  • promote local variable as parameter
  • extract section of code as method

What you should do when you want to practice test-driven development (TDD):

  1. Create a new project that would like to target in your unit testing. Typically these should be your business logic classes. 
  2. Create a test project
  3. In the test project, add a reference to the target project to be tested.
  4. Write your TestMethods.
  5. Use the TestContext, especially when you want to iterate through list of values which will be used as arguments to test your method. Please read up more on this on MSDN. Live search it!
  6. Run your test. If you're strict about TDD, your unit test cases are always meant to fail for the first time. There's no such thing as happy day scenario to begin with. The last thing is your happy day scenario.
  7. Then ask your developer colleague to implement the feature.

One big advantage of practising TDD is that you will not be biased when it comes to testing. You will write your unit test cases according to the functional specs or requirements. You should be true to yourself when writing the unit test cases, not testing what you are going to write in the target method.

Try it out, and let us know what you think about adopting TDD in your software projects.