Mapping a shortcut to execute unit test in current context with ReSharper

It shouldn’t be a surprise to you that I’m a big fan of ReSharper since its early versions.  Its productivity boost and refactoring possibilities should alone convey a reason to learn and master it to be more effective when programming with C#. 

One thing that I wish came out of the box, but is currently missing, is a shortcut that allows me to run my unit test in the current context (by context I mean where my cursor is currently located.  If my cursor is currently in the body of TestA, then that shortcut will run the TestA unit test).  This will allow me to use the mouse even less, because right now I’m using the mouse to tell ReSharper to execute the current test. It turns out that though this functionality is lacking in ReSharper 4.1, the possibility to do so still exists. 

Here are the steps to follow if you want to map a keyboard shortcut that will run a test in the current context:

  1. In Visual Studio, go to Tools –> Options
  2. Expand the Environment node, then go to Keyboard
  3. In the commands list, search for Resharper.Resharper_UnitTest_ContextRun
  4. Assign a shortcut for that command.  In my case, I have decided to map it with Alt + X in the Global context.  That shortcut is intuitive (X for "execute”) and easy to trigger.  You can choose whatever mapping you want, just make sure there are no conflicts with other existing shortcuts.

Here’s a screenshot of the above steps in action:

image

Now you can place your cursor in a test context (test body or test method name) and hit that shortcut (Alt + X in my case) and that test will get executed.  A good tip to follow is to save your Visual Studio keyboard settings (and ReSharper settings too) under a backup repository (such as Live Mesh for example) so that you (or your team) can re-use those same settings in your Visual Studio sessions in other machines.

This post has been viewed: 735 times. kick it on DotNetKicks.com

 

Similar posts you might be interested in reading:

2 Comments

  1. Ilya Ryzhenkov:

    This command also senses context in Solution Explorer, like running all tests in current file, folder or project (or multiple items selected). You can also select several tests in editor and it will execute the selection.

  2. Brian Di Croce:

    Thanks Ilya for the precision! Another reason why I love this tool so much! ;)

Leave a comment

Powered by WP Hashcash