Unit Test Class ReSharper Template and Snippets for nUnit and MS Test

Quick helper boiler plates for setting up your Unit Test.

I have included ReSharper Template here that you can import directly Resharper Templates. Also thanks you to Alessandro Aeberli for making the these Visual Studio Snippets

After importing the ReSharper Templates then you can use ‘uTestBoiler’ or ‘uTestnUnitBoiler’ shortcuts to add the code below into any test class.

If you want to make your own snippets you can use the Visual Studios Snippet Manager by following the instructions here https://msdn.microsoft.com/en-us/library/ms165394.aspx

MS Test Boiler Snippet

Use this within a [TestClass]

private TestContext _testContextInstance;
 
/// <summary>
///Gets or sets the test context which provides
///information about and functionality for the current test run.
///</summary>
public TestContext TestContext
{
    get { return _testContextInstance; }
    set { _testContextInstance = value; }
}
 
#region Additional test attributes
 
//
// You can use the following additional attributes as you support your tests:
//
// Use ClassInitialize to run code before running the first test in the class
// [ClassInitialize()]
// public static void MyClassInitialize(TestContext testContext) { }
//
// Use ClassCleanup to run code after all tests in a class have run
// [ClassCleanup()]
// public static void MyClassCleanup() { }
//
// Use TestInitialize to run code before running each test 
// [TestInitialize()]
// public void MyTestInitialize() { }
//
// Use TestCleanup to run code after each test has run
// [TestCleanup()]
// public void MyTestCleanup() { }
//
 
#endregion
 
//Note: Use 'utest' snippet to insert methods
 
[TestMethod]
public void Method_Scenario_Expected()
{
    // ==== Arrange ====
 
    var expected = ;
 
    // ==== Act ====
 
    var actual = "actual";
 
    // ==== Assert ====
 
    Assert.Fail("Test method not implemented");
}

nUnit Test Boiler Snippet

Use this within a [TestFixture]

Snippet

#region Additional test attributes
 
// You can use the following additional attributes as you support your tests:
 
// Use to run code Before any tests in a class have run
// [OneTimeSetUp]
// public static void TestFixtureSetup() { }
 
// Use to run code before each test in the class
// [SetUp]
// public void Setup(TestContext testContext) { }
 
// Use to run code after each test has run
// [TearDown]
// public void TearDown() { }
 
// Use to run code afer all tests have run 
// [OneTimeTearDown]
// public void TestFixtureTearDown() { }
 
#endregion
 
//Note: Use 'utestnUnit' snippet to insert methods
 
// Add or remove [TestCase] attributes for multiple cases
[TestCase(5, 3, 9)]
[Test]
public void Method_Scenario_Expected(int a, int b, int expected)
{
    // ==== Arrange ====
 
    a = a + 1;
 
    // ==== Act ====
    
    var actual = a + b;
 
    // ==== Assert ====
 
    Assert.AreEqual(expected, actual);
}

 

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.