Skip to content

Instantly share code, notes, and snippets.

@sionjlewis
Last active February 21, 2022 21:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sionjlewis/7da94d59f3fc0216a314bca16b27e1e4 to your computer and use it in GitHub Desktop.
Save sionjlewis/7da94d59f3fc0216a314bca16b27e1e4 to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>Unit Test Method</Title>
<Author>Siôn J. Lewis</Author>
<Description>Adds a unit test method with comments based on the AAA (Arrange,Act,Assert) pattern</Description>
<HelpUrl>https://msdn.microsoft.com/en-us/library/ms165394.aspx</HelpUrl>
<Shortcut>utest</Shortcut>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal Editable="true">
<ID>MethodName</ID>
<ToolTip>Method name: e.g. IsStrawberryRed</ToolTip>
<Default>MethodName</Default>
<Function>
</Function>
</Literal>
<Literal Editable="true">
<ID>Condition</ID>
<ToolTip>Condition: e.g. WhenRipe</ToolTip>
<Default>Condition</Default>
<Function>
</Function>
</Literal>
<Literal Editable="true">
<ID>ExpectedResult</ID>
<ToolTip>Expected result: e.g. True</ToolTip>
<Default>ExpectedResult</Default>
<Function>
</Function>
</Literal>
</Declarations>
<Code Language="CSharp" Kind="method decl"><![CDATA[[TestMethod]
public void $MethodName$_$Condition$_$ExpectedResult$()
{
// Arrange
$end$
// Act
// Assert
Assert.Fail();
}]]></Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
@sionjlewis
Copy link
Author

sionjlewis commented Dec 19, 2016

While researching Unit Testing, I reviewed several different C# Code Snippet examples for generating Unit Test Methods (Functions) within Visual Studio and this is my take...

How to Use

  1. Save the Code Snippet file as UnitTestMethod.Snippet
  2. For for VS 2015, move the UnitTestMethod.Snippet file into this directory: %userprofile%\Documents\Visual Studio 2015\Code Snippets\Visual C#\My Code Snippets
  3. Once installed type: utest + [Tab] + [Tab]

@sionjlewis
Copy link
Author

I've added the Assert.Fail(); to make sure the Test is Implemented.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment