A Developer WeBLOG RSS 2.0

Visual Studio 2008 comes with a unit testing tool, MSTest. The coolest thing about MSTools is that it integrates so well with Visual Studio. Every time you build your solution, Visual Studio will automatically run your test projects and display the results in a nice graphical user interface (similar to NUnit’s one). I’ve been using MSTest for my side projects, until I found out that MSTest is not supported Visual Studio Express, which I have installed in my MSI Wind. Because of this limitation, I had to resort back to the good old NUnit.

After using NUnit again, I really missed MSTest integration with Visual Studio. NUnit provides a very minimal integration with Visual Studio, which requires you to create an external tool that points out to the NUnit console. When you run the NUnit tool, it will spit out the results into your Visual Studio’s output window. The simplistic approach works, but the problem with this is that I have to run it manually each time I run my build. It would be ideal to just have this automatically run each time we build our solution. Luckily, this can be automated by using Visual Studio macro and post build event. Here is how you do it.

Creating NUnit As A Visual Studio External Tool

First we need to create NUnit as a visual studio external tool. To do this, in Visual Studio, go to Tools > External Tools. You will be prompted with a form, in which you can manage all of the available external tools. Click on the Add button and fill in the form as follows.

ss1

You should fill in your form exactly as above, except for the Title field (if you wish to have different name) and the command field. The command field is the path to your NUnit console runner. Click the Ok button to create the external tool. Now that you have created it, if you go to Tools menu in Visual Studio, you will see NUnit in one of the available external tools. When you run the NUnit external tool, it will run NUnit through command line, and display it in your Visual Studio output window.

NOTE: the above will make NUnit runs all of the tests in the current active project. Thus make sure your active project is your test projects when running the tools. You can do this by having your test as your active file in Visual Studio when compiling. You can make it not to be bounded by your current active project by; hardcoding the arguments value to be your test dll and the inital directory value to be the location of your test dll. But keep in mind that the tool is not project/solution specific. That is, if you open up a different solution, it will still run the hardcoded value.

Automating NUnit Integration To Run After Compile

Now that you have the NUni integration set up, its time to make it run automatically after each compile. Here’s how you do it:

  • In Visual Studio go to Tools > Macros > Macros IDE.
  • This will bring up a the Macros IDE.
  • On the left hand side there should be a “Project Explorer” panel.
  • In the Project Explorer panel, double click on EnvironmentEvents module.
  • Insert the following code in the module.
Private Sub BuildEvents_OnBuildDone(ByVal Scope As EnvDTE.vsBuildScope, ByVal Action As EnvDTE.vsBuildAction) Handles BuildEvents.OnBuildDone
    DTE.ExecuteCommand("Tools.ExternalCommand1")
End Sub

As you can see that the above custom macro tries to run an external tool by its order. Thus depending on where you put your NUnit tool, you might want to change the parameter of right number based on the order of your NUnit tool. For example, if your NUnit is the third external tool, you can change the macro to this: DTE.ExecuteCommand(“Tools.ExternalCommand3”).

Debuging Your NUnit Tests.

Now that you have everything set up, how can one debug their NUnit tests? Surely you want to debug your tests once it fails. Here’s how you do it:

  • Right click on your test project and select "properties”.
  • On the right hand side, click on the Debug tab.
  • Change the start action from “Start Project” to “Start External Program”.
  • Insert the path to your NUnit GUI runner as your External Program.
  • In the Start Option section, insert your test dll, as your command line args, followed by “ /run”. For example: “tests.dll /run”.
  • Lastly set your working directory to the path of your test dll.
  • Your form should look something like this.

ss2

With this configuration set up, whenever you run your solution (don’t forget to set your test project as your Start Up project), this will run NUnit’s GUI runner to run your tests. Moreover, Visual Studio will automatically attach the debugger to the NUnit’s GUI runner process. This means that you can start debugging by putting breakpoints in your test code.

Wait a minute, isn’t this another way of automatically running NUnit after compile? Yes it is. So why mentioning the “External tool” method? Well the problem with running NUnit as external program is that, you have to always run your solution (F5) and set your test project as your start-up project. Doing it as an external tool, you can just build your solution and it runs your test automatically. Moreover, I find that NUnit runs much faster in the “External tool” method.

Hope you find this useful. Now write some TESTS!

RWendi

Friday, July 31, 2009 9:28:54 PM UTC |  Comments [2]
NUnit | TDD | Unit Test
All Content © 2012, RWendi