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:


public void Method_Scenario_Expectation()






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"?>



  <CodeSnippet Format="1.0.0">


      <Title>MsTest unit test snippet</Title>

      <Author>Dave Lowe</Author>

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





        <Literal Editable="true">


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




        <Literal Editable="true">


          <ToolTip>scenario under test</ToolTip>




        <Literal Editable="true">


          <ToolTip>expected behaviour</ToolTip>





      <Code Language="CSharp">



        public void $Method$_$Scenario$_$Expectation$(){









As you can see its pretty straightforward, there are three literal regions which you can tab between. I assigned the shortcut “aaa” (for Arrange Act Assert) to make it nice and easy to use. The last step was to add the folder containing this file to Visual Studio (Tools –> Code Snippet Manager –> Add…), back in any C# class file typing “aaa” and hitting tab twice generated the snippets desired – bingo! Incidentally there is tool to do this for you but you gotta love getting into the XML right?


I wrote the snippet on the train on my laptop and the user account I was running under didn’t have ReSharper installed normally any account I use would have but I’ve just started at a new clients and only just put the laptop on their domain so I haven’t had time to set everything up yet. When I got home I was working on my Desktop PC, added the snippet but to my dismay nothing happened when I tried to use my new snippet – after a bit of research it turns out ReSharper has some very similar functionality that goes under the term of “Live Templates” and they don’t play so well together – I’ve used ReSharper for over a year now and I hadn’t seen this before (which makes me wonder how much other cool stuff I don’t know about….?). I came across this blog post where the author talks about the same issue and highlights how to use the ReSharper live template feature – whats really cool is in the actual snippet code (the CDATA section in the XML above) can be pasted into the ReSharper live template designer.


I did this and it just worked straight away. Yet another reason to love ReSharper

  • (This will not appear on the site)