Do you find yourself often writing the same pieces of code? I guess we all do: the INotifyPropertyChanged interface and the PropertyChanged method in WPF projects are one example. Unit tests more often than not have the same structure (at least on a per-project basis).

Visual Studio’s snippets functionality can speed up your work in this regard. I knew about snippets since some time, but never actually used them. I recently noticed I had to type the same code for unit tests over and over again, and decided to check out how snippets could help. I encourage you to do the same.

First, see the structure in your code. I like unit tests that look like:

[TestFixture]
public class WhenAddingElementToCollection() : UnitTestBaseClass
{
     private MyCollection _collection;
     private Element _element;
     public override Given()
     {
          _collection = new MyCollection();
     }

     public override When()
     {
          _collection.Add(_element);
     }

     [Test]
     public void ShouldIncrementCount()
     {
          Assert.AreEqual(1, _collection.Count);
     }
}

 

The class inherits from a UnitTestBaseClass that provides the structure to make all unit tests alike. You can easily make a snippet for this. Let’s make a snippet for the Given() and When() overrides:

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>GivenWhen overrides</Title>
      <Description>Provides two method overrides (Given and When) for unit testing in Cassis.</Description>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
      <Shortcut>gw</Shortcut>
    </Header>
    <Snippet>
      <Code Language="CSharp">
        <![CDATA[/// <summary>
  /// Initializes the context in which the test should run
  /// </summary>
  protected override void Given()
  {
   base.Given();
  }

  /// <summary>
  /// Does something in the system that will be asserted later on
  /// </summary>
  protected override void When()
  {
   base.When();
  }]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

 

Save this as a .snippet file (I saved it in my Visual Studio 2010\Code Snippets\Visual C#\My Code Snippets folder) and it should be available. You will also see it in your Code Snippets Manager (available under the Tools menu in Visual Studio). The XML should be fairly straight-forward. Now, typing in ‘gw’ and pressing tab twice will generate the snippet. Then, you can tab through the different fields.

I made a different snippet for the should method:

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>Should method</Title>
      <Description>Provides a Should... method for unit testing in Cassis</Description>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
      <Shortcut>should</Shortcut>
    </Header>
    <Snippet>
      <Declarations>
        <Literal>
          <ID>method</ID>
          <ToolTip>What to assert.</ToolTip>
          <Default>HaveDoneSomething</Default>
        </Literal>
        <Literal>
          <ID>summary</ID>
          <ToolTip>Summary of what this method is asserting.</ToolTip>
          <Default>something was done</Default>
        </Literal>
        <Literal>
          <ID>implementation</ID>
          <ToolTip>Implementation of the method.</ToolTip>
          <Default>Assert.</Default>
        </Literal>
      </Declarations>
      <Code Language="CSharp">
        <![CDATA[/// <summary>
  /// Asserts that $summary$.
  /// </summary>
  [TestMethod]
    public void Should$method$()
  {
   $implementation$
  }]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

 

At first, this takes some discipline to actually use the snippets. But once you get used to it, it really speeds up your work.

If you have ReSharper, these snippets will work, but you won’t see them in Intellisense, because ReSharper overrides your Visual Studio Intellisense. ReSharper offers Live Templates that do the same and more. That might be for another post, but they’re easier than Visual Studio’s snippets, and have more possibilities.

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=""> <s> <strike> <strong>

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