;

Creating a visual studio Arrange-Act-Assert snippet

Posted : Thursday, 06 December 2012 10:44:00

First things first I’m big on unit testing, I love it! From the time I read this book The Art of Unit Testing I was sold. If anyone reading this wants to know more about unit testing this book is the place to start.

Anyway, I find myself writing a lot of tests (generally using MsTest but that’s purely out of laziness) and while the standard snippet that comes with Visual Studio is ok, its not quite what I want. After reading the above book I like to structure my tests using the skeleton below:

[TestMethod]

public void Method_Scenario_Expectation()

{

    //Arrange

    //Act

    //Assert

}

Creating tests using the built in Visual Studio snippet is better than nothing but then I have to make the same alterations every time, namely changing the method name and adding the Arrange,Act,Assert comments. Recently I decided to try and create a snippet to do this for me and it was alarmingly easy. I found a few blog posts on the subject and its quite a simple process so rather than go over the whole thing – I’ve just included the snippet below.

<?xml version="1.0" encoding="utf-8"?>

<CodeSnippets

    xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">

  <CodeSnippet Format="1.0.0">

    <Header>

      <Title>MsTest unit test snippet</Title>

      <Author>Dave Lowe</Author>

      <Description>Adds a unit test stub method for MsTest</Description>

      <Shortcut>aaa</Shortcut>

    </Header>

    <Snippet>

      <Declarations>

        <Literal Editable="true">

          <ID>Method</ID>

          <ToolTip>name of method under test</ToolTip>

          <Default>MethodName</Default>

          <Type>string</Type>

        </Literal>

        <Literal Editable="true">

          <ID>Scenario</ID>

          <ToolTip>scenario under test</ToolTip>

       

  • (This will not appear on the site)