Reason #467 to use ReSharper: Live Templates

I just learned to use ReSharper’s code snippet feature, called Live Templates. I was looking for a code snippet to generate an NUnit test method. Oddly, despite ReSharper’s support for running NUnit tests, they didn’t see fit to include an NUnit test snippet. (Instead they included templates for the xUnit framework [Fact] and [Theory] methods, so I guess we know what framework they’re using.) So, I just created my own. Here’s how:

Step 1: Open ReSharper’s Live Templates dialog from the ReSharper menu in Visual Studio.

image

Step 2: Navigate to the folder where you’d like to add a Live Template and click the New Template button: (For me, this was the C# folder under the User Templates folder.)

image

(ALT+PrtScn didn’t capture the mouse pointer, but you get the idea.)

Step 3: Give your Live Template a shortcut and description, then enter the same kind of snippet code that you’d put in a traditional Visual Studio code snippet.
image

Step 4: Save your new Live Template and turn it on by selecting the checkbox next to it in the Templates Explorer dialog.

image

(Again, ALT+PrtScn didn’t capture the mouse pointer, but you get the idea.)

So, now, assuming I’m using ReSharper’s IntelliSense (to check go to ReSharper / Options / IntelliSense / General), I can create a test simply by typing “te” and selecting my Live Template from IntelliSense.

image

Sure, you could do all of this in Visual Studio, without ReSharper. In fact, Visual Studio 2010 includes a code snippet to generate tests in their syntax (where [Test] becomes [TestMethod]). But, you would have had to hand edit the snippet XML. Plus, ReSharper also includes Surround Templates (i.e. “surround the selected code in an if/for/try statement, please”) and File Templates (to create that new Test Fixture classes with all the correct NUnit using clause and [TestFixture] statement, for example).

To paraphrase Red Green: If your peers don’t find you handsome, they should at least find you handy.

0 responses