5,691,626 members and growing! (12,889 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » .NET Framework » General     Intermediate

Visual Studio 2005 Unit Testing Code Snippets

By Maruis Marais

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.
C#, Windows, .NET 2.0, .NETVisual Studio, VS2005, Dev

Posted: 23 Feb 2006
Updated: 23 Feb 2006
Views: 60,210
Bookmarked: 60 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
19 votes for this Article.
Popularity: 5.60 Rating: 4.38 out of 5
1 vote, 5.3%
1
1 vote, 5.3%
2
0 votes, 0.0%
3
5 votes, 26.3%
4
12 votes, 63.2%
5

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



Occupation: Web Developer
Location: New Zealand New Zealand

Other popular .NET Framework articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 22 of 22 (Total in Forum: 22) (Refresh)FirstPrevNext
QuestionNUnit.Frameworkmembersbayat21:44 5 Jun '08  
AnswerRe: NUnit.FrameworkmemberMaruis Marais13:02 6 Jun '08  
AnswerRe: NUnit.Frameworkmembersbayat1:51 7 Jun '08  
GeneralVery clear explinationmemberNancy Neira13:36 26 Mar '08  
GeneralSuperb -- thanks!memberrikardon11:45 21 Nov '07  
GeneralGreat JobmemberAvery Moore6:45 15 Jun '07  
GeneralRe: Great JobmemberMaruis Marais12:48 15 Jun '07  
Generalteam testmembertantoun0:14 10 Apr '07  
GeneralRe: team testmemberJoeSharp0:36 10 Apr '07  
GeneralTest menu in VS 2005memberIhteshamQazi3:24 15 Oct '06  
GeneralRe: Test menu in VS 2005memberMaruis Marais20:50 15 Oct '06  
GeneralRe: Test menu in VS 2005memberSpiff Dog14:29 24 Jan '07  
Generallooks goodmemberabhigad23:18 12 Mar '06  
NewsNote about snippet shortcutsmemberMaruis Marais22:31 27 Feb '06  
QuestionNew FeaturesmemberMaruis Marais11:35 25 Feb '06  
QuestionRe: New FeaturesmemberBill3615:03 27 Feb '06  
AnswerRe: New FeaturesmemberMaruis Marais19:38 27 Feb '06  
GeneralGreat WorkmemberDaniel Petersen10:16 23 Feb '06  
Generalsimple and nice!memberjconwell10:08 23 Feb '06  
GeneralRe: simple and nice!membercomputerguru923829:02 25 Feb '06  
GeneralRe: simple and nice!memberMaruis Marais11:32 25 Feb '06  
GeneralRe: simple and nice!membercomputerguru9238213:35 25 Feb '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 23 Feb 2006
Editor: Smitha Vijayan
Copyright 2006 by Maruis Marais
Everything else Copyright © CodeProject, 1999-2008
Web13 | Advertise on the Code Project