Easier TDD with Visual Studio 2005 (Macro)

While doing TDD (Test Driven Development) with Visual Studio 2005 is not always an easy task when you get down and dirty. Microsoft is already working on improvements for the next version (codename "Orcas") of Visual Studio, but right now I needed a better way to compile the current project and run my unit test project.

One issue that Microsoft is looking into is running the tests does not necessarily invoke the build process of the code you have recently edited.

While Rob Osherove (link above) tells you to use TestDriven.NET as a workaround, I want to continue to use the built in Team Testing functionality in Visual Studio Team System.

Inside Visual Studio, you can go on Tools/Options/Environment/Keyboard. In this dialog you can configure the shortcuts that are available to you within the IDE. The first thing I do when I have a fresh installation of Visual Studio is change the CTRL-SHIFT-B binding from Build.BuildSolution to Build.BuildSelection. I tend to do a lot of building and when I do, I don't want the whole solution to compile as that usually takes to much time.

Then if you have a Team Edition of Visual Studio 2005, you have a keybinding of CTRL-SHIFT-X which is binded to Test.StartSelectedTestProjectwithoutDebugger. This is the binding we want to edit to do the following:

Compile Current Project
Wait for Compile to Complete
Run Unit Test Project

On the project I'm currently working on we have separated our tests in two different Team Test Projects. One named PRODUCT.Integration and the other named PRODUCT.Test. We have physically seperated our integration tests and our unit tests.

While it's beyond the scope of this post to explain the difference between integration and unit tests, the quick story is that your unit test should run very, very fast and rely on no manual configuration or setup. They should be so fast that you'd would want to run them every time you build (compile) your code.

So I went ahead and started writing my Visual Studio Macro that I can bind to CTRL-SHIFT-X.

Open up the Macro Explorer (ALT-F8) and make a new Macro project. I called it UnitTesting on my machine and when your project is ready, create a new module which I also called UnitTesting, like the picture below.

MacroExplorer

Now you can double-click the module and it will open it up so you can edit it.

Let us start by adding a simple function to the module which does nothing more than compile the current selection, which is the first operation we want our macro to do.

Module UnitTesting
  
Sub CompileAndTest()
      ' Execute build on the current selection
      DTE.ExecuteCommand("Build.BuildSelection")
   End Sub
End
Module

Now save the macro and close the Microsoft Visual Studio Macros window. You will see your new function appear in the Macro Explorer, right click on it and choose run (or double-click).

MacroExplorerRun

Make sure the macro runs without problems and appears to compile your project. Maybe this is the first time you've written a custom Visual Studio macro? Congratulations!

So without any futher introduction, you can go on to download the full CompileAndTest and macro.

You should notice the following line in the macro and make sure to edit it if your unit test project does not contain the name "Test".

' Check if the project contains the name "Test"
If (p.Name.Contains("Test")) Then

Note: The macro is not tested with multiple projects that contains the name test.

The last and final step is to bind your recently created macro to the keybinding you want. I have chosen to replace the CTRL-SHIFT-X, see screenshot below.

Keyboard

When you now do your CTRL-SHIFT-X, the current selected project where you are editing code will compile, when the compilation is completed, the Test Results window should appear and start running through your unit tests.

TestResults

Hopefully some of you will enjoy this little macro and please leave some feedback on improvements!


Comments

Comments are closed

Pages

Search

Tags

None

    Recent posts

    Recent comments

    Blogroll

    Disclaimer

    The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.