Resharper templates for unit testing with NUnit

If you’ve ever done test driven development for (TDD) over an extended period of time, you’ll probably agree that writing the same boilerplate test-code over and over can be a hassle. I’m talking specifically about structures like the following:

[TestMethod]
public void SetUp(){ ... }

[TestMethod]
public void Should_Do_Something_Or_Other(){ ... }

[TestCase("input")]
public void Should_Get_Some_Info(string targetPerson){ ... }

 

Writing a large number of tests quickly is perhaps the primary defining characteristic of a TDD process, so being able to generate code like the above quickly is an obvious advantage. If you use Resharper, you can add your own code templates to help you achieve this.

Below you’ll find three templates that I’ve been using for some time now, along with a description of how you can set them up in Visual Studio in under a minute.

Resharper Live Template Editor

Resharper Live Template Editor

Adding the templates to Visual Studio / Resharper

Under the Resharper item in the main VS menu, look for Templates Explorer and Live Templates. That should open a window like the one above, where you can add or edit templates. The icon to add a new template is marked with a red circle in the image above. We will add three new templates in a moment, but I’d just like to point out a couple of things about the interface first though.

To the left you can specify which scope a template will be valid for. You could specify that the following shortcuts should only work when you are working with C# code, but I generally just use the global scope, since I tend to have a limited number of templates anyway. You can also create your own categories for template collections (click the icon that looks like a folder), and add templates to a them by clicking their name dragging them into the folder.

Something else I find interesting here though, is the ability to look through the large number of already existing templates. There are a lot of built in shortcuts for you, and here you get to see them, how they work, and modify them to your own taste.

When you click to add a new template, the template file will be opened in a Visual Studio editor window. This is where you’ll enter the actual template content. To the right you should see a couple of input fields where you can specify a short description and a shortcut for the template. In the following example, the shortcut will let me create a test method by entering “tm” followed by either tab or the  return / enter key:

ResharperLiveTemplateSettings

 

In the template text, any word that starts and ends with $ will become a variable, i.e. a chunk of text that will be marked for editing wen the template is activated later. You will then be able to jump forth and back between these using tab and shift + tab, respectively. For each of these variables, there will be a line with it’s name and a check box marked “Editable” to the right, as well as a “Choose macro” link below. Editable obviously means you will be able to change the text in place of the variable later. The macro option will let you insert something else there instead – as an example, you could call a macro that would evaluate to the current date in a specified format. We’ll leave that alone for now though.

The templates

Here are the three templates that I’ve found most useful. Create them separately and add shortcuts as shown above, and you should be ready to go (I use the shortcuts tm, tc, and su for TestMethod, TestCase, and SetUp respectively, but you can obviously use whatever you prefer).

[Test]
public void Should_$DO_SOMETHING$(){
  $Test_logic$
}

[SetUp]
public void SetUp(){
  $Set_up_logic$
}

[TestCase($input$)]
public void Should_$DoSomething$(){
  $TestLogic$
}

 

Engage and contribute for the common benefit of mankind!

This site uses Akismet to reduce spam. Learn how your comment data is processed.