Test Method Code Snippet For Visual Studio – created with the Snippet Designer

I came across the Snippet Designer earlier today and I have to say I’m really impressed – it was really easy to use and I have already created some snippets that will save me from a great deal of repetition.

One of the snippets I’ve made is Test Method template – mainly because I use TDD at work and pretty much spend most of my life writing test.

Anyway, here we go…

Test Method Code Snippet

Once you’ve installed the Snippet Designer, create a new code snippet file in visual studio (file => new => file: code snippet file).

Write the following code in the code editor:

  1. [TestMethod]
  2. public void $MethodName$_$Action$_$ExpectedResult$()
  3. {
  4.     // Arrange
  5.  
  6.     // Act
  7.  
  8.     // Assert
  9. }

Notice the $ signs around MethodName, Action and ExpectedResult – this are the sections I want the template to allow for easy replacing when I run the snippet.

In the codesnippet properties window you can set various properties, probably the most important is the shortcut – I’ve set this snippet to: test.

Test Method Code Snippet in Action

So now when I start typing ‘test’ the code snippet appears in intellisense, then by hitting tab, the test method template is added to the code editor with the test method name in an editable state ready for completing:

clip_image002

Anyway, that’s it, really simple to do but extremely powerful and useful, especially if you don’t have tools like code rush installed.

I’m really chuffed I came across this today, it’s definitely worth checking out.

Kind Regards,

Sean McAlinden.

www.asp-net-mvc.com

2 Comments

Comments have been disabled for this content.