Testing code snippets for Visual Studio
October 30th, 2009If 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:
-
<?xml version="1.0"?>
-
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
-
<CodeSnippet Format="1.0.0">
-
<Header>
-
<Title>Create a test class</Title>
-
<Description>This snippet adds code to create test class template.</Description>
-
<Shortcut>testclass</Shortcut>
-
</Header>
-
<Snippet>
-
<Declarations>
-
<Object>
-
<ID>Name</ID>
-
<ToolTip>Name of a test class</ToolTip>
-
<Default>Name</Default>
-
</Object>
-
</Declarations>
-
<Code Language="csharp"><![CDATA[[TestClass]
-
public class $Name$
-
{
-
$end$
-
}]]>
-
</Code>
-
</Snippet>
-
</CodeSnippet>
-
</CodeSnippets>
Test method
XML:
-
<?xml version="1.0"?>
-
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
-
<CodeSnippet Format="1.0.0">
-
<Header>
-
<Title>Create a test method</Title>
-
<Description>This snippet adds code to create test method template.</Description>
-
<Shortcut>testmethod</Shortcut>
-
</Header>
-
<Snippet>
-
<Declarations>
-
<Object>
-
<ID>Name</ID>
-
<ToolTip>Name of a method which is being tested</ToolTip>
-
<Default>Name</Default>
-
</Object>
-
<Object>
-
<ID>State</ID>
-
<ToolTip>State of input data</ToolTip>
-
<Default>State</Default>
-
</Object>
-
<Object>
-
<ID>Result</ID>
-
<ToolTip>Result of an assertion</ToolTip>
-
<Default>Result</Default>
-
</Object>
-
</Declarations>
-
<Code Language="csharp"><![CDATA[[TestMethod]
-
public void $Name$_$State$_$Result$()
-
{
-
$end$
-
}]]>
-
</Code>
-
</Snippet>
-
</CodeSnippet>
-
</CodeSnippets>
Rhino mocks record / playback routine
XML:
-
<?xml version="1.0"?>
-
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
-
<CodeSnippet Format="1.0.0">
-
<Header>
-
<Title>Create a record / playback routine for rhino mocks</Title>
-
<Description>This snippet adds code to create record / playback routine for unit testing.</Description>
-
<Shortcut>recordplayback</Shortcut>
-
</Header>
-
<Snippet>
-
<Declarations>
-
<Object>
-
<ID>mocks</ID>
-
<ToolTip>Mock repository object name</ToolTip>
-
<Default>mocks</Default>
-
</Object>
-
</Declarations>
-
<Code Language="csharp">
-
<![CDATA[using(mocks.Record())
-
{
-
$end$
-
}
-
using(mocks.Playback())
-
{
-
}
-
]]>
-
</Code>
-
</Snippet>
-
</CodeSnippet>
-
</CodeSnippets>
Throw ArgumentNullException when a method's argument cannot be null
XML:
-
<?xml version="1.0"?>
-
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
-
<CodeSnippet Format="1.0.0">
-
<Header>
-
<Title>Create argument null checking routine</Title>
-
<Description>This snippet adds code to create a code to check whether param was null ane throw an exception when applicable.</Description>
-
<Shortcut>checkparam</Shortcut>
-
</Header>
-
<Snippet>
-
<Declarations>
-
<Object>
-
<ID>parameter</ID>
-
<ToolTip>parameter</ToolTip>
-
<Default>parameter</Default>
-
</Object>
-
</Declarations>
-
<Code Language="csharp"><![CDATA[if ($parameter$ == null)
-
{
-
throw new ArgumentNullException("$parameter$");
-
}
-
]]>
-
</Code>
-
</Snippet>
-
</CodeSnippet>
-
</CodeSnippets>
