ReSharper: Code snippets for tests.

I’ve always felt too lazy to key in full words, write different standard formulatings, it usually drives me crazy or make me bored. Some time before it hasn’t been so ruefully, but during these two years of working with ReSharper everything redoubled, for instance I don’t type «public» at all, I prefer to type «р» and press space, so ReSharper will finish writing. The only thing that I write by myself is names of methods, classes and tests. I would like to stress that only the names and not all notes. And variables are always named by ReSharper too. =) I’m a real sluggard!

I would like to share my knowledge with you how to develop such laziness and how ReSharper helps in it. Because of we hold to Test Driven Development, all code snippets will hold to texts.

All texts begin with an add of text class.

using Microsoft.VisualStudio.TestTools.UnitTesting;

public class classname { }

Everything doesn’t change except the title of text class. I physically suffer from the look how the others write it. In information about myself I’ve told that at work we practice XP and that’s why pair programming is the imprescriptible point.

After choosing the class you need to choose the text method.

public class classname {
    public void name() {

Look how much text there is! And there are only 2(two) comprehensible words! Typing everything isn’t the easy way, so we need to automatize it. With the help of the ReSharper it does quite fast and easy.

Image should be here. Checkout your's AdBlocker

Open “ReSharper > Live Templates…” , we see the window where we can make code snippets. Next open the node “User Templates”, invoke context menu for it or “<No Languages>” and choose “New template”.

Image should be here. Checkout your's AdBlocker

After it in the main studio’s area we can see the tab, where we are going to format a draft.

Copy the code which we’ll use as a draft, paste into the window, name it “testc” and format alterable parts. So, this time it should be like it:

Image should be here. Checkout your's AdBlocker

Variables in the ReSharper label in the dollar sign from both sides. In the draft we’ll have 2 variables:

  • Classname
  • Testname

Also we need to designate the place, where to place the cursor after pasting the draft. For it, there is a special alterable $END$.

The code now looks in such way:

public class $classname$ {
    public void $name$() {

In the right side of the window there are variables which we use. We can allocate more appropriate types of data for them or the value at once. Also we can indicate if it’s need to stop on the editing of the element after pasting only with a check mark.

I think that the majority follows the rule «One class-one file». If yes, so you can name the text class just as the file. For it, click on “Choose macro” opposite the variable in the rigt part of the screen and choose “Current file name without extension”.

Image should be here. Checkout your's AdBlocker

Remove a check mark opposite a classname, as the title will be already given.

Save everything and now you can test it. Generate a new file in the text project. Open it, write testc and click Tab. ReSharper will paste a draft of the code and place the file’s name, brighten the method’s name and it will offer you to enter. Enter the text’s name, and click Tab again so you will be in the method’s body. Now you can start writing the needful code.

As usual, there are several methods in the one text class and they are not similar in the first value. Or the religion doesn’t allow you to copy-paste the code at all. In this case you can make the other draft of the code, which pastes only the text method.

public void $name$(){

You won’t have a chance to write texts without Assert statement, so you can automatize this operatoin too. These are the most popular constructoins for me:

  • Assert.AreEqual
  • Assert.IsTrue
  • Assert.IsFalse

Shortening of them I do in first letters. And all drafts are going to be simple




Also you can testInitialize to the draft’s code. At all I advise you to use active yours drafts as well as embedded. I hope that you’ve seen the example that writing is easy and simple. I don’t think that it will disaccustom you to type fast, but it will help you to save your time which you spend on checking new approaches and solving tasks.

The only minus of all is that without a ReSharper you will have withdrawal pains. Maybe you won’t work without this instrument at all =)


Tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>