Home > Programming > VS Snippet for Test methods

VS Snippet for Test methods

Ok so you have started doing TDD and you really dislike having to type

   1: [TestMethod]
   2: public void SomeTeast()
   3: {
   4:  // Insert Test code here...
   5: } 

And you really really wish there was a way that you could do this quickly… well there is!!!! It is called a snippet.

To set this up open your "My Documents\Visual Studio 2008\Code Snippets\Visual C#\My Code Snippets" folder, and then create a new Xml file (using whatever hacks you like.  Then paste this xml into it…  (I’ve left it plain to make it easier to copy.)

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>Test Method</Title>
            <Shortcut>test</Shortcut>
            <Description>Code snippet for basic unit test function</Description>
            <Author>David McEwing</Author>
            <SnippetTypes>
                <SnippetType>Expansion</SnippetType>
            </SnippetTypes>
        </Header>
        <Snippet>
            <Declarations>
                <Literal>
                    <ID>testName</ID>
                    <ToolTip>Name of the Test</ToolTip>
                    <Default>true</Default>
                </Literal>
            </Declarations>
            <Code Language="csharp">
        <![CDATA[
      [TestMethod]
      public void $testName$()
        {
              //Insert code here…
        }]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

Then in Visual studio a new TEST snippet will appear in your list. You use it like this…

Start typing the word "test" you will find that in the drop down a snippet is there for you to use, it looks like this…

When you have selected like above… press tab, twice.

The following will magically appear…

(red squiggles are because I’ve generated these shots using a standard CS file not one setup for tests.)

Type the name for your test and press enter. The cursor will be located ready for you to enter the code for your test. Simple, time saving, and flexible.

Yes Flexible… if you don’t like this snippet or think you can improve, or want variations, just load it, or copy it and then open it, in visual studio. Snippets are a really simple XML file that you can tweak or create new ones to your own content.

NOTE: For Resharper users. Resharper has its own templating system. This method is just for the VS built in way. If you want to use VS’s snippets and not Resharper’s then you will need to set Resharper to use Visual Studio snippets.

Advertisements
Categories: Programming
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: