Click here to Skip to main content
15,895,370 members
Articles / Programming Languages / C#

Visual Studio 2005 Unit Testing Code Snippets

Rate me:
Please Sign up or sign in to vote.
4.71/5 (22 votes)
23 Feb 20064 min read 140.4K   790   76  
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.
  • testfirstsnippets.zip
    • codesnippetsmanager.jpg
    • CreatedCodeSnippet.jpg
    • invokefull.jpg
    • keyboard.jpg
    • selectsnippetfolder.jpg
    • Test Driven Development
      • NUnit
        • Assertions
          • Comparisons
            • assert.greater.snippet
            • assert.less.snippet
          • Condition
            • assert.isempty.snippet
            • assert.isfalse.snippet
            • assert.isnan.snippet
            • assert.isnotempty.snippet
            • assert.isnotnull.snippet
            • assert.isnull.snippet
            • assert.istrue.snippet
          • Equality
            • assert.areequal.snippet
            • assert.arenotequal.snippet
          • Identity
            • assert.arenotsame.snippet
            • assert.aresame.snippet
            • assert.contains.snippet
          • String
            • stringassert.areequalignoringcase.snippet
            • stringassert.contains.snippet
            • stringassert.endswith.snippet
            • stringassert.startswith.snippet
          • Type
            • assert.isassignablefrom.snippet
            • assert.isinstanceoftype.snippet
            • assert.isnotassignablefrom.snippet
            • assert.isnotinstanceoftype.snippet
          • Utility
            • assert.fail.snippet
            • assert.ignore.snippet
        • Attributes
          • category.snippet
          • expectedexception.snippet
          • explicit.snippet
          • ignore.snippet
          • platform.snippet
          • testattribute.snippet
          • testfixtureattribute.snippet
          • testfixturesetupattribute.snippet
          • testfixtureteardownattribute.snippet
          • testsetupattribute.snippet
          • testteardownattribute.snippet
        • Code Fragments
          • class.snippet
          • fixture.snippet
          • fixturesetup.snippet
          • fixtureteardown.snippet
          • full.snippet
          • method.snippet
          • setup.snippet
          • teardown.snippet
      • VisualStudio.UnitTesting
        • Assertions
          • Collection
            • collectionassert.allitemsareinstancesoftype.snippet
            • collectionassert.allitemsarenotnull.snippet
            • collectionassert.allitemsareunique.snippet
            • collectionassert.areequal.snippet
            • collectionassert.areequivalent.snippet
            • collectionassert.arenotequal.snippet
            • collectionassert.arenotequivalent.snippet
            • collectionassert.contains.snippet
            • collectionassert.doesnotcontains.snippet
            • collectionassert.isnotsubsetof.snippet
            • collectionassert.issubsetof.snippet
          • Condition
            • assert.isfalse.snippet
            • assert.isnotnull.snippet
            • assert.isnull.snippet
            • assert.istrue.snippet
          • Equality
            • assert.areequal.snippet
            • assert.arenotequal.snippet
            • generic.assert.areequal.snippet
            • generic.assert.arenotequal.snippet
          • Identity
            • assert.arenotsame.snippet
            • assert.aresame.snippet
            • generic.assert.arenotsame.snippet
            • generic.assert.aresame.snippet
          • String
            • stringassert.contains.snippet
            • stringassert.doesnotmatch.snippet
            • stringassert.endswith.snippet
            • stringassert.matches.snippet
            • stringassert.startswith.snippet
          • Type
            • assert.isinstanceoftype.snippet
            • assert.isnotinstanceoftype.snippet
          • Utility
            • assert.fail.snippet
            • assert.inconclusive.snippet
        • Attributes
          • agentproperty.snippet
          • assemblycleanup.snippet
          • assemblyinitialize.snippet
          • classcleanup.snippet
          • classinitialize.snippet
          • cssiteration.snippet
          • cssprojectstructure.snippet
          • datasource.snippet
          • deploymentitem.snippet
          • description.snippet
          • expectedexception.snippet
          • hosttype.snippet
          • ignore.snippet
          • owner.snippet
          • priority.snippet
          • testclass.snippet
          • testcleanup.snippet
          • testinitialize.snippet
          • testmethod.snippet
          • testproperty.snippet
          • workitem.snippet
        • Code Fragments
          • class.snippet
          • classcleanup.snippet
          • classinitialize.snippet
          • full.snippet
          • method.snippet
          • testclass.snippet
          • testcleanup.snippet
          • testinitialize.snippet
    • testmethod.jpg
    • vs2005UnitTestingSnippets.html
<!--------------------------------------------------------------------------->  
<!--                           INTRODUCTION                                

 The Code Project article submission template (HTML version)

Using this template will help us post your article sooner. To use, just 
follow the 3 easy steps below:
 
     1. Fill in the article description details
     2. Add links to your images and downloads
     3. Include the main article text

That's all there is to it! All formatting will be done by our submission
scripts and style sheets. 

-->  
<!--------------------------------------------------------------------------->  
<!--                        IGNORE THIS SECTION                            -->
<html>
<head>
<title>The Code Project</title>
<Style>
BODY, P, TD { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10pt }
H2,H3,H4,H5 { color: #ff9900; font-weight: bold; }
H2 { font-size: 13pt; }
H3 { font-size: 12pt; }
H4 { font-size: 10pt; color: black; }
PRE { BACKGROUND-COLOR: #FBEDBB; FONT-FAMILY: "Courier New", Courier, mono; WHITE-SPACE: pre; }
CODE { COLOR: #990000; FONT-FAMILY: "Courier New", Courier, mono; }
</style>
<link rel="stylesheet" type=text/css href="http://www.codeproject.com/styles/global.css">
</head>
<body bgcolor="#FFFFFF" color=#000000>
<!--------------------------------------------------------------------------->  


<!-------------------------------     STEP 1      --------------------------->
<!--  Fill in the details (CodeProject will reformat this section for you) -->

<pre>
Title:       Visual Studio 2005 Unit Testing Code Snippets
Author:      Maruis Marais 
Email:       maruis@xtra.co.nz	
Environment: VS2005, .NET 2.0
Keywords:    Visual Studio 2005, Visual C# 2005 Express Edition, IDE
Level:       Beginner
Description: 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 these Unit testing snippets to give you the ability to quickly create test methods and fixtures.
Section      IDE, VS.NET Addins
SubSection   C#, .NET
</pre>

<!-------------------------------     STEP 2      --------------------------->
<!--  Include download and sample image information.                       --> 

<ul class=download>
<li><a href="testfirstsnippets.zip">Download source - 197 Kb</a></li>
</ul>

<p><img src="codesnippetsmanager.jpg" alt="Visual Studio 2005 Code Snippets Manager showing the Unit testing code snippets." width=531 height=399></p>


<!-------------------------------     STEP 3      --------------------------->
<!--  Add the article text. Please use simple formatting (<h2>, <p> etc)   --> 

<h2>Introduction</h2>

<p>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. 
<h2>Background</h2>

<p>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 is not as comfortable with writing tests, to learn the unit test tools quicker and have some guidance while writing their tests. 
<h2>How to install</h2>
<p>Download the testfirstsnippets.zip file then extract the content of the file to either the &lt;My Documents&gt;\Visual Studio 2005\Code Snippets or to the &lt;Visual Studio 2005 install directory&gt;\VC#\Snippets\1033. Next we want to open Visual Studio 2005 and open the code snippet manager window (Found in the Tools menu).</p>
<p>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. </p>
<p><img src="selectsnippetfolder.jpg" ></p>
<p>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. </p>
<h2>Using the code</h2>

<p>You can use the Unit test code snippets in four different ways in Visual Studio 2005:
<ol>
  <li>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 arrow 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.</li>
  <br><img src="keyboard.jpg" width="414" height="193"><br>
  </li>
  <li>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.</li>
  <li>Edit Menu - place your cursor in the code window and from the Edit menu, select IntelliSense and then select Insert Snippet Command. </li>
  <li>Context Menu - right click  in the code window and then select the Insert Snippet Command. </li>
</ol>
<p>You can now add a new code file and then invoke the Test Driven Development&gt;NUnit&gt;Code Fragment&gt;Full Test Class snippet. 
<p><img src="invokefull.jpg" width="600" height="242">
<p>After you have invoked a code snippet you'll see the code editor window displaying the following:
<p><img src="CreatedCodeSnippet.jpg" width="553" height="402">
<p>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 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.
<h2>Points of Interest</h2>

<P>As the developer you will need to think about what specification, behavior or functionality the test are describing. And here is where it becomes critical to name your test with an intent describing name to communicate the behavior you are implementing with the test.</p>
<P>The following suggestions help to construct a good test name:</P>
<UL>
  <LI>Use the [desired-result][coordinator][conditions] pattern to specify the behavior.</LI>
</UL>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.g. Create a <I>Stack</I> and verify that <I>IsEmpty</I> is true.
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- Desired Result:&nbsp;&nbsp;IsEmptyTrue<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- Coordinator:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;When
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- Conditions:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;StackCreated
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&gt; IsEmptyTrue_When_StackCreated</P>
<P>When you create a Test Method using the Unit Testing snippets, you will find this naming pattern. </P>
<P><img src="testmethod.jpg" width="400" height="234"></P>
<P>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.</P>
<h2>Finally</h2>
<p>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. </p>
</body>
</html>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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


Written By
Web Developer
New Zealand New Zealand
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions