Testing code snippets for Visual Studio

October 30th, 2009

If you create many test classes and test methods you probably repeat some code many times.
Here are some code snippets that I use every day at work, feel free to modify them if you want.

Test class

XML:
  1. <?xml version="1.0"?>
  2. <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  3.   <CodeSnippet Format="1.0.0">
  4.     <Header>
  5.       <Title>Create a test class</Title>
  6.       <Description>This snippet adds code to create test class template.</Description>
  7.       <Shortcut>testclass</Shortcut>
  8.     </Header>
  9.     <Snippet>
  10.       <Declarations>
  11.         <Object>
  12.           <ID>Name</ID>
  13.           <ToolTip>Name of a test class</ToolTip>
  14.           <Default>Name</Default>
  15.         </Object>
  16.       </Declarations>
  17.       <Code Language="csharp"><![CDATA[[TestClass]
  18.       public class $Name$
  19.       {
  20.         $end$
  21.       }]]>
  22.       </Code>
  23.     </Snippet>
  24.   </CodeSnippet>
  25. </CodeSnippets>

Test method

XML:
  1. <?xml version="1.0"?>
  2. <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  3.   <CodeSnippet Format="1.0.0">
  4.     <Header>
  5.       <Title>Create a test method</Title>
  6.       <Description>This snippet adds code to create test method template.</Description>
  7.       <Shortcut>testmethod</Shortcut>
  8.     </Header>
  9.     <Snippet>
  10.       <Declarations>
  11.         <Object>
  12.           <ID>Name</ID>
  13.           <ToolTip>Name of a method which is being tested</ToolTip>
  14.           <Default>Name</Default>
  15.         </Object>
  16.         <Object>
  17.           <ID>State</ID>
  18.           <ToolTip>State of input data</ToolTip>
  19.           <Default>State</Default>
  20.         </Object>
  21.         <Object>
  22.           <ID>Result</ID>
  23.           <ToolTip>Result of an assertion</ToolTip>
  24.           <Default>Result</Default>
  25.         </Object>
  26.       </Declarations>
  27.       <Code Language="csharp"><![CDATA[[TestMethod]
  28.       public void $Name$_$State$_$Result$()
  29.       {
  30.         $end$
  31.       }]]>
  32.       </Code>
  33.     </Snippet>
  34.   </CodeSnippet>
  35. </CodeSnippets>

Rhino mocks record / playback routine

XML:
  1. <?xml version="1.0"?>
  2. <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  3.   <CodeSnippet Format="1.0.0">
  4.     <Header>
  5.       <Title>Create a record / playback routine for rhino mocks</Title>
  6.       <Description>This snippet adds code to create record / playback routine for unit testing.</Description>
  7.       <Shortcut>recordplayback</Shortcut>
  8.     </Header>
  9.     <Snippet>
  10.       <Declarations>
  11.         <Object>
  12.           <ID>mocks</ID>
  13.           <ToolTip>Mock repository object name</ToolTip>
  14.           <Default>mocks</Default>
  15.         </Object>
  16.       </Declarations>
  17.       <Code Language="csharp">
  18.         <![CDATA[using(mocks.Record())
  19.             {
  20.             $end$
  21.             }
  22.             using(mocks.Playback())
  23.             {
  24.             }
  25. ]]>
  26.       </Code>
  27.     </Snippet>
  28.   </CodeSnippet>
  29. </CodeSnippets>

Throw ArgumentNullException when a method's argument cannot be null

XML:
  1. <?xml version="1.0"?>
  2. <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  3.   <CodeSnippet Format="1.0.0">
  4.     <Header>
  5.       <Title>Create argument null checking routine</Title>
  6.       <Description>This snippet adds code to create a code to check whether param was null ane throw an exception when applicable.</Description>
  7.       <Shortcut>checkparam</Shortcut>
  8.     </Header>
  9.     <Snippet>
  10.       <Declarations>
  11.         <Object>
  12.           <ID>parameter</ID>
  13.           <ToolTip>parameter</ToolTip>
  14.           <Default>parameter</Default>
  15.         </Object>
  16.       </Declarations>
  17.       <Code Language="csharp"><![CDATA[if ($parameter$ == null)
  18. {
  19.   throw new ArgumentNullException("$parameter$");
  20. }
  21.       ]]>
  22.       </Code>
  23.     </Snippet>
  24.   </CodeSnippet>
  25. </CodeSnippets>

Leave a Reply

Designed by SirMike © All rights reserved

Valid XHTML 1.0! Valid CSS!

Powered by Rootnode