Pressing F5 in Test Methods to Test

04 December 2014

Last night, as I was about to stop work to spend some time with the family, I noticed this tweet:

F5Run

And I thought: You can do this in CodeRush. Easily. CodeRush has a remarkably flexible shortcut-binding system that can associate a sophisticated context (that must be satisfied) with any key binding. So it’s relatively simple to bind F5 to a command to run the test when the caret is inside a test method. You just need a context that tells the key-binding engine whether you’re in a test method or not.

CodeRush ships about 200 different contexts, that can do anything from tell you whether you are inside a property’s getter to whether the active project references a particular assembly. Contexts are used in shortcut bindings as well as other editor features.

After seeing the tweet, I checked to see if we already shipped a context that was satisfied when the caret was inside a test method. We did not.

This morning I noticed more discussion about this feature, including a suggestion to use another shortcut (which increases user burden and cognitive load – lots of good reasons NOT to do this), followed by this tweet from Caleb Jenkins:

AllCodeRushLike

Time to get to work.

Here’s how I built the feature, in about three minutes:

  1. CodeRush | New Plug-in.
  2. Dropped a ContextProvider control onto the design surface.
  3. Named it “Editor\Code\InTestMethod”.
  4. Double-clicked the ContextSatisfied event. Added this code:

    private void ctxInTestMethod_ContextSatisfied(ContextSatisfiedEventArgs ea)
    {   Method activeMethod = CodeRush.Source.ActiveMethod;   if (activeMethod != null && activeMethod.Attributes != null)     foreach (DevExpress.CodeRush.StructuralParser.Attribute attribute in activeMethod.Attributes)       if (attribute.Name != null && attribute.Name.StartsWith("Test"))       {         ea.Satisfied = true;         return;       }
    }
  5. Pressed F5 to run (to test my plug-in).
  6. In the new instance of VS, added the following CodeRush shortcut binding:

    ShortcutBinding

And that’s it.

Now I can press F5 inside a test method and only that method test is run. If I press F5 outside of a test method, the startup project runs.

F5ToRunTestCases

 
5 comment(s)
Crono

Neat trick! I'll definitely use this!

4 December, 2014
Konstantin K (DevExpress)

great!

i've just created my first dx coderush plugin!

simple & very useful!

great blogpost!

5 December, 2014
Michael Stortz

I've done something wrong, my plugin does not show up in the context list.  I named the context provider ctxInTestMethod and set its DisplayName to Editor\Code\InTestMethod. It shows up in the plugin manager... what else?

5 December, 2014
Mark Miller (DevExpress)

Hi Michael,

Make sure the ProviderName matches the DisplayName. If the ProviderName is empty, this might cause what you're seeing.

5 December, 2014
Miha Markic - DevExpress MVP

Also you guys would want to exclude System."In Debug" in context. Othwerise F5 won't continue while stopped at breakpoint.

7 December, 2014

Please login or register to post comments.