Right-Click, Create Unit Tests...

One of the many things I felt disappointed with Visual Studio 2012 was the removal of the "Create Unit Tests..." context menu. With the feature missing, creating unit tests have become more painful than before. The explanation from Microsoft was...

Generate Unit Test Wizard – In VS2010 you could right click on a method in your code and we would generate a unit test into your test project. This wizard was very tightly coupled to MS-Test and depended on features like Private Accessors to do its work, so it was cut. We are exploring alternatives here, but don’t have any good solutions yet.

Taken from here. But hey, what happens to those of us who are ok with MS-Test and don't really care about 3rd-party unit test frameworks? We want back the feature for our productivity!

Searching around the web, we will see that many developers have complained about it and most of them had given up. Doing some search and research there are actually a few ways to re-enable the feature. I will save the best one for last.

Method 1 - Command Window

If you find hard enough, you will discover that the command to invoke the Create Unit Tests... is actually:

EditorContextMenus.CodeWindow.CreateUnitTests

You can actually type that in the Command Window to invoke the Create Unit Test Wizard. If you find it too verbose, you can actually create an alias for it.

alias ut EditorContextMenus.CodeWindow.CreateUnitTests

Then just type ut to launch the wizard.

Method 2 - Keyboard Shortcut

The next method is to go to Tools->Options->Environment->Keyboard and set a shortcut key of your preference.


Method 3 - Re-enable the Create Unit Tests... Context Menu.

This is by far the best method ever. Thanks to the perseverance of my developer, Dawa Law, who seem to just refuse to accept that the option is gone *LOL*. He has even a better way but I'm just going to talk about the way I discovered after his continuous persistence in asking me about the issue.

To re-enable the Create Unit Tests... menu, go to Tools->Customize... to launch the Customize dialog. At the Customize dialog, go to the Commands tab, select the Context menu radio button and choose Editor Context Menus | Code Window.


You will realized that the menu item is actually available in the context menu. :) The VS developers were kind enough to leave it there for us. *HeHe* Click on the Create Unit Tests... menu item and click either Move Up or Move Down. [Note: My screenshot is showing Create Unit Test in singular because I had previously tried to rename it]. Then click Close.

Restart Visual Studio 2012 and whalla! The context menu will be available!


Caveats!

You may discover that using any of the above methods may not work. i.e. The Command Window will tell you that the command is not available, the keyboard shortcut will not fire or the Context menu will be available but disabled.

To allow all these to work, you will first need to have a Unit Test Project in your solution. After creating the Unit Test Project, you will need to manually Add a Unit Test item to the project even though the project unfolds with one on its own. After you have done this, everything will function as it is like back in Visual Studio 2010. You can safely remove those UnitTest1.cs files.

What about the 'Dawa Law' Method ?

I just thought to give a word or two on what my developer did to even simplify the re-enabling of the option. He actually exported his settings to a vssettings file and modified it to add the menu item. Other developers will only need to import the settings file and the option will be enabled - How thoughtful was that? :) Of course, the caveats still apply. I will leave it to him to explain it as the credit goes to him.

If you are like me who wants to see the option being added back Out-of-the-box in Visual Studio 2012, don't forget to vote for it here.

Have fun unit testing! :)




No comments:

Post a Comment

Popular Post