Click here to Skip to main content
Click here to Skip to main content

Visual Studio 2005 Unit Testing Code Snippets

, 23 Feb 2006
Rate this:
Please Sign up or sign in to vote.
In this article, I will be introducing Unit Testing Code Snippets, showing you how to add a Code Snippet in your page, and how to quickly navigate and modify the Unit Testing snippets to give you the ability to quickly create test methods and fixtures.

Visual Studio 2005 Code Snippets

Introduction

Visual Studio 2005 ships with a huge number of new features, and one of the nice and useful features is Code Snippets. The Code Snippet feature allows you to add a piece of generic code that you can customize to make it work with your specific needs. In this article, I will be introducing Unit Testing Code Snippets, showing you how to invoke a code snippet, and how to quickly navigate and edit the code snippet tokens to give you the ability to quickly create test methods and fixtures. You will find both NUnit and Microsoft.VisualStudio.TestTools.UnitTesting snippets in the download.

Background

I've been doing test-first development for a couple of years now, and when I started to use Visual Studio 2005, I was a bit disappointed at how unfriendly the unit testing tools where for developer productivity. The other reason for these snippets is to allow other developers who are not as comfortable with writing tests, to learn the unit test tools quicker and have some guidance while writing their tests.

How to install

Download the testfirstsnippets.zip file, then extract the contents of the file to either <My Documents>\Visual Studio 2005\Code Snippets or to <Visual Studio 2005 install directory>\VC#\Snippets\1033. Next, we want to open Visual Studio 2005 and open the code snippet manager window (found in the Tools menu).

On the Code Snippet Manager window, click the Add button, and the Code Snippet Directory dialog opens. Now, you need to navigate to the directory you have extracted the code snippets to, and then select the Test Driven Development folder.

After selecting the Test Driven Development folder, you should see the folder displayed in the Code Snippet Manager window. You can now browse the folder structure to see all the new snippets that are available. The Code Snippet Window should display the Test Driven Development folder structure like the topmost image on this page.

Using the code

You can use the Unit Test code snippets in four different ways in Visual Studio 2005:

  1. Keyboard shortcut - place your cursor in the code window and press Ctrl+K, Ctrl+X. The code snippet inserter will appear. You can now use the up and down arrows to navigate up and down, and Enter to select a folder, or backspace to move back up the hierarchy. When you've found the snippet you are after, you can invoke the snippet by either the Tab or Enter keys.

  2. IntelliSense auto-completion - place your cursor in the code window, and type the shortcut for the code snippet that you want to add, and then use Tab to invoke the code snippet.
  3. Edit menu - place your cursor in the code window and from the Edit menu, select IntelliSense, and then select Insert Snippet Command.
  4. Context menu - right click in the code window and then select the Insert Snippet Command.

You can now add a new code file and then invoke Test Driven Development > NUnit > Code Fragment > Full Test Class snippet.

After you have invoked a code snippet, you'll see the code editor window displaying the following:

You can now modify the green highlighted values. Use Tab and Shift+Tab to navigate between the highlighted values, and press either Enter or Esc to exit the edit mode on the code snippet. When you use Enter, your cursor will move to the specified end tag of the snippet. On the Full Test Class snippet, you will be taken to the test method.

Points of Interest

As the developer, you will need to think about what specification, behavior, or functionality the tests are describing. And here is where it becomes critical to name your test with an intent describing the name to communicate the behavior you are implementing with the test.

The following suggestions help to construct a good test name:

  • Use the [desired-result][coordinator][conditions] pattern to specify the behavior.

E.g., create a stack and verify that IsEmpty is true.

- Desired Result:   IsEmptyTrue
- Coordinator:      When 
- Conditions:       StackCreated 
=> IsEmptyTrue_When_StackCreated

When you create a Test method using the Unit Testing snippets, you will find this naming pattern:

One of the real nice features of the Test Method snippet, is that your method name is automatically updated as you complete the Desired Result, Coordinator, and Conditions sections of your comments, saving you heaps of coding time.

Finally

I would appreciate if people using these snippets can give me any feedback on what they'll like to see added, or if they find any bugs to leave a comment so that I can improve the library.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Maruis Marais
Web Developer
New Zealand New Zealand
No Biography provided

Comments and Discussions

 
GeneralNice PinmemberCIDev20-May-11 5:21 
QuestionNUnit.Framework Pinmembersbayat5-Jun-08 20:44 
AnswerRe: NUnit.Framework PinmemberMaruis Marais6-Jun-08 12:02 
AnswerRe: NUnit.Framework Pinmembersbayat7-Jun-08 0:51 
GeneralVery clear explination PinmemberNancy Neira26-Mar-08 12:36 
GeneralSuperb -- thanks! Pinmemberrikardon21-Nov-07 10:45 
GeneralGreat Job PinmemberAvery Moore15-Jun-07 5:45 
GeneralRe: Great Job PinmemberMaruis Marais15-Jun-07 11:48 
Generalteam test Pinmembertantoun9-Apr-07 23:14 
GeneralRe: team test PinmemberJoeSharp9-Apr-07 23:36 
GeneralTest menu in VS 2005 PinmemberIhteshamQazi15-Oct-06 2:24 
GeneralRe: Test menu in VS 2005 PinmemberMaruis Marais15-Oct-06 19:50 
GeneralRe: Test menu in VS 2005 PinmemberSpiff Dog24-Jan-07 13:29 
Generallooks good Pinmemberabhigad12-Mar-06 22:18 
NewsNote about snippet shortcuts PinmemberMaruis Marais27-Feb-06 21:31 
QuestionNew Features PinmemberMaruis Marais25-Feb-06 10:35 
QuestionRe: New Features PinmemberBill3627-Feb-06 14:03 
AnswerRe: New Features PinmemberMaruis Marais27-Feb-06 18:38 
GeneralGreat Work PinmemberDaniel Petersen23-Feb-06 9:16 
Generalsimple and nice! Pinmemberjconwell23-Feb-06 9:08 
GeneralRe: simple and nice! Pinmembercomputerguru9238225-Feb-06 8:02 
GeneralRe: simple and nice! PinmemberMaruis Marais25-Feb-06 10:32 
GeneralRe: simple and nice! Pinmembercomputerguru9238225-Feb-06 12:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 23 Feb 2006
Article Copyright 2006 by Maruis Marais
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid