Seamless test authoring with ReSharper and AutoHotKey

Although I’m no big believer of code generation, the micro-code generation of ReSharper can be a huge timesaver.  For every common snippet of code or common file I would create, I could create a simple template that might create a class, method, or just a small snippet of code.

Another tool I love to use is AutoHotKey.  For about a year and a half now, I’ve used BDD-style names in my tests, which leads to a lot of underscores in test names.  Underscores are a pain to type however, especially when you would use it as much as a spacebar.  AutoHotKey allows me to map keystrokes to other keystrokes, such as say, spacebar to underscores.  With ReSharper and AutoHotKey integrated, you’ll have about as seamless TDD/BDD experience as you can get.

To integrate the two, I’ll first need to get some ReSharper templates going.

Some simple test templates

I only use underscores in test class/method names, so we’ll just create templates for those two items.  Step zero of course, is to have ReSharper, but you can also do this with Visual Studio’s code snippets.  I prefer ReSharper templates just because they’re ridiculously easy to create, and more powerful.  First, let’s create a fixture template by going to ReSharper –> Live Templates:

This brings up the Templates Explorer.  In the Live Templates tab, navigate to the Shared Solution Templates C# in the tree view and click the “New Template” button in the toolbar:

I already have a couple of templates in there, and our team has some shared templates we keep in source control (makes pairing waaaay easier).  Clicking the New Template button brings up the template editor, where we need to supply three pieces:

  • Shortcut
  • Description
  • Template

For the “Shortcut”, I’ll use “tc” for “test class”, which is what I’ll also put in the Description.  Also, uncheck “Reformat”, which automatically reformats when you put the template in (and we don’t want).  Finally, we’ll put the following text in our template section:

[TestFixture]
public class $TESTNAME$
{
    $END$
}

The dollar signs create editable areas that take focus whenever we execute the shortcut.  The final result should look something like this:

There are tons of other options I could look at, such as setting the available context, but I’ll leave it alone for now.

To execute the template, I go to a C# file and type in “tc+TAB”, and my template pops out.  Before we look at AutoHotKey, make sure your template is working properly.

Integrating AutoHotKey

I was introduced to AutoHotKey by JP Boodhoo a long time ago, and I still use his template as a starting point for mine.  In his starting template, you can use “Ctrl+Shift+U” to enable the TestNamingMode script, and from there on out your spacebars turn into underscores.  The starting point script can be found here: http://pastie.org/395769.  Additionally, you’ll need the two images that JP created for your system tray.

AutoHotKey is a script, which you compile into an executable.  You’ll need to download AutoHotKey (free) to compile the script we create.

However, I don’t really like turning on and off the test naming mode, I want this to just turn on right after I execute the ReSharper template shortcut, “tc+TAB”.  To do so, first I’ll need to comment out the part that checks to make sure Visual Studio is active in our AutoHotKey script:

;==========================
;Test Mode toggle
;==========================
;#IfWinActive Microsoft Visual Studio
^+u::
SetTestNamingMode(!IsInTestNamingMode)
return

I did this by putting a semi-colon in front of the “#IfWinActive” piece.  Next, I insert the following at the bottom of the script:

;==========================
;R# Template hotkeys
;==========================
::tc::tc^+u
return

This piece takes any “tc” plus a terminator key (enter, tab, etc.) and pipes out the original “tc+TAB”, plus “Ctrl+Shift+U”, which turns on my TestNamingMode.  Save this file, and right click and hit “compile” from Windows Explorer:

Once the script is compiled, run the executable that was created, and you’ll see a little tray icon pop up in your system tray:

Now that my AutoHotKey script is up and running, I can go back to Visual Studio and use my R# template integrated with AutoHotKey.

When I execute my template:

  • “tc+TAB” turns on TestNamingMode in my AutoHotKey script
  • The AHK script pipes “tc+TAB” back to Visual Studio
  • R# uses “tc+TAB” to spit out my template
  • I type away with test names and spacebars, and AHK turns my spacebar key into underscores
  • When I’m done with the test name, I hit “ENTER”, which both turns off my TestNamingMode, and moves the caret to the “$END$” part in my template (the body of the test class)

This is about the most seamless way I’ve seen to do TDD.  I hit “tc+TAB”, or “t+tab” (my test method template), and I can immediately start typing normally and my test names show up just fine, without having to fight with either the underscore key, or a macro that doesn’t play well with IntelliSense.

You can find the final script, plus the compiled version and images here.

Related Articles:

Post Footer automatically generated by Add Post Footer Plugin for wordpress.

About Jimmy Bogard

I'm a technical architect with Headspring in Austin, TX. I focus on DDD, distributed systems, and any other acronym-centric design/architecture/methodology. I created AutoMapper and am a co-author of the ASP.NET MVC in Action books.
This entry was posted in Tools. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • http://lancefisher.net/blog Lance Fisher

    Thanks, this is great! I downloaded your script, and noticed the ‘t’ shortcut for test method was not included. I added this to the bottom of the script:

    ::t::t^+u
    return

    Seems to work.

  • http://www.lostechies.com/members/bogardj/default.aspx bogardj

    @Lance

    That’s something my teammates let me know about a dozen times too :) thanks!

  • Kirstin

    Awesome! Thanks.

  • Aeden Jameson

    Great approach, I’m going to try it. Quick question though, any tips on modifying your script to work with multiple R# live templates. I tried adding the lines below to the last section of your script

    ::obs::obs^+u
    ::spec::spec^+u
    return

    and I tried

    ::obs::obs^+u
    return

    ::spec::spec^+u
    return

    I did recompile and only the last one is endowed with the magic of AHK.

  • Joseph Leo

    I’m coming across this nearly 3 years after it was written and still finding tremendous value in it.  Thanks for your post!

    • http://profiles.google.com/jimmy.bogard Jimmy Bogard

      Awesome, thanks!

  • Krzysztof