Monday, April 06, 2009

Unit Tests with VSTS 2008 in a Context Menu

Unit testing in the .Net Framework 3.5 is quite usable and has so far managed to replace the dependency on NUnit libraries in most of my new unit tests. One of the issues I was having recently was that TestDriven.Net plugin would hang on different occasions so I started using default test runner for Microsoft’s unit test library.

In Visual Studio, you can display Test Tools toolbar and Run or Debug unit tests from there. There are also keyboard shortcuts that do the same thing, which is convenient if working on unit tests or code and then wanting to run the current test without moving a hand for the mouse. The shortcuts are as follows:

  • Ctrl+R, Ctrl+T = Debug tests in current context.
  • Ctrl+R, Ctrl+A = Debug all tests in solution.
  • Ctrl+R, T = Run tests in current context.
  • Ctrl+R, A = Run all tests in solution.

Running or debugging tests in current context means that, if the cursor is within a test method, only that test will be run/debug. If the cursor is outside the test method but within a test class then all the tests in the current test class will be run. Etc.

It is also convenient to see the Test Results window during debugging. One thing that is important, though, is that debugging unit tests in this way has not crashed Visual Studio 2008 once so far!

One thing that would make life even easier is to have the convenience of running and debugging unit tests through context menu, just like TestDriven.Net does.  This is fairly simple thing to do yourself:

1. Right-click toolbars and select Customize.


2. Display Context Menus


3. Now, in the toolbars, open the Editor Context Menus


4. In the Customize window, go to Commands tab, then click Test category.


5. All the available context-menu options will be displayed in the area to the right. Find whichever option you like (for example, “Debug Tests in Current Context”)


and drag it to the Editor Context menu to a position you like.

6. When you close Customize window everything will be set up. The final result will look like this:


One more thing that is extremely positive is that running tests with VSTS framework, as opposed to TestDriven.Net, allows you to edit code while debugging which was not the case with TestDriven!

No comments: