Code Snippet Basics – now with NUnit!
Download the NUnit Code Snippets.
As I was working through using NUnit for the first time, I started to notice that the majority of the tests I wrote followed this pattern:
var expected = someValue;
var result = methodUnderTest();
Assert.AreEqual(expected, result);
Realizing that I was liable to write hundreds, if not thousands, of these tests, I decided this was an excellent opportunity to finally try my hand at writing Code Snippets.
Code Snippets
If you Code Snippets are old hat to you, skip on to the next section. If you haven’t used Code Snippets yet, you are in for a real treat.
I remember the first time I saw Code Snippets. I was at a presentation at VSLive! in 2005, and the presenter kept making code templates (seemingly) appear out of nowhere. Then he was able to quickly tab through them and fill in bits of the code. I immediately recognized how cool this was, and a few code snippets in particular have become second nature to me.
If you aren’t sure yet what I’m talking about, try this out. Open a project in Visual Studio. Go to a class, and just inside the class declaration type the letter “c”. Intellisense should popup with something like this:
What we are looking for is anything with the orange box icon: this indicates a Code Snippet. If you select that item and press Tab, Visual Studio will place a template in your code. This template may even have defined sections that you can navigate with the Tab key and fill in with the correct data.
You can search through the library of snippets by going to Tools –> Code Snippet Manager. This will open a window that will allow you to browse your snippets. For starters, expand the C# folder and you will see it is chock full of goodies. Here are a few that I use all the time:
- ctor – this snippet will insert an empty Constructor
- prop – this snipper will create an automatic property and allow you to easily fill in the return type and name (also check out propg, which will make the property setter private)
- foreach – lays out the template for a foreach loop (you can also use for to insert a traditional for loop.)
- try – inserts a try…catch… block template.
There are many more shipped by default with Visual Studio. You can also import snippets from a different source, which we’ll discuss in a little bit.
Writing your own Code Snippet
A Code Snippet is an XML file that lays out the rules for inserting this block of code. To create one, just create an XML file and name it whatever you like with a .snippet extension. The name of the file doesn’t matter, but I named mine based on the snippet shortcut, so “nutm.snippet” for Code Snippet nutm. When you edit it, of course it has to conform to the snippet standard. I don’t claim to be an expert: I just found a couple of samples on line and butchered them until they did what I want. All in all it was pretty easy. If you are really interested in trying this yourself, I suggest you read the MSDN Documentation – Creating Code Snippets.
For my purposes, I created two Code Snippets.
- nutf – creates an NUnit Test Fixture (the class that holds the Unit Tests)
- nutm – inserts an NUnit Test method.
Here is what the snippet XML looks like for nutm, the Code Snippet for inserting a Test Method:
<?xml version="1.0" encoding="utf-8"?> <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"> <CodeSnippet Format="1.0.0"> <Header> <Title>NUnit Test Method</Title> <Author>Joel Cochran</Author> <Shortcut>nutm</Shortcut> <Description>Inserts an NUnit Test method</Description> <SnippetTypes> <SnippetType>Expansion</SnippetType> </SnippetTypes> </Header> <Snippet> <Declarations> <Literal Editable="true"> <ID>method</ID> <Default>Method</Default> <ToolTip>Insert the Method name you are testing.</ToolTip> </Literal> <Literal Editable="true"> <ID>scenario</ID> <Default>Scenario</Default> <ToolTip>Insert the name of the scenario you are testing.</ToolTip> </Literal> <Literal Editable="true"> <ID>expectedBehavior</ID> <Default>ExpectedBehavior</Default> <ToolTip>Insert the expected behavior of your test.</ToolTip> </Literal> <Literal Editable="true"> <ID>expectedValue</ID> <Default>ExpectedValue</Default> <ToolTip>Insert the expected return value for this test.</ToolTip> </Literal> </Declarations> <Code Language="CSharp"> <![CDATA[ [Test] public void $method$_$scenario$_$expectedBehavior$() { var expected = $expectedValue$; var result = _instance.$method$(); Assert.AreEqual(expected, result); } $end$]]> </Code> </Snippet> </CodeSnippet> </CodeSnippets>
Importing a Code Snippet
There are lots of Code Snippets available online for download, including my NUnit Code Snippets. Whether you are writing your own snippets or importing them from somewhere else, you will need to go through the same process to make them available to Visual Studio. Once you have the snippet on your local machine, just follow the documentation.
I will share a frustration with you: I had no way of knowing whether or not my Code Snippet was valid until I tried to import it into Visual Studio. If the format is invalid, the Code Snippet will simply not import, and that is all the help you get. Once the format is acceptable the import goes off without a hitch. Abd that’s all the help Visual Studio will give you.
Using Your Code Snippet
Now that your Code Snippet is installed, you use it just as we defined before: enter the snippet shortcut, press Tab, and watch the magic! Unfortunately, there is one bit of bad news. Intellisense does not show your snippet in its listing.
At least, it doesn’t for me: that’s not to say it can’t, but more that I don’t know how to make it show up in Intellisense. It’s possible that there is a way to define it in the XML, or perhaps it is ReSharper intruding on Intellisense a little (although I doubt it.) In either case, I don’t know how to do it: but if you do, please post it in a comment below!
Hi Joel,
There are also some NUnit snippets included with Resharper which I believe you use.
Cheers,
Stuart
I’ll have to dig around and see – it’s kind of like the .NET framework itself: there are a lot of great things in there I’ve never heard of: the trick becomes finding them!
Ever check out the Code Snippet editors?
There are a few.
http://code.msdn.microsoft.com/SnippDogg
Is neat…but there seems to be an issue with actually saving your keywords.
Then http://snippy.codeplex.com/
or http://snippetdesigner.codeplex.com/ as well.
If you have ReSharper, you can use their live templates – much easier to create and edit than VS’s snippets.
http://www.jetbrains.com/resharper/features/code_templates.html
Nick, I meant to mention the Code Snippet Editors but forgot. Thanks for catching my slack.
Pat, Great tip – ReSharper FTW!
Joel,
Thanks so much for sharing these NUnit snippets. These were exactly what I wanted for writing a bunch of new unit tests and I would have made them myself had I not found these. Thanks for saving me the trouble!