Show Menu
Cheatography

Code example of defining tests in MS Test

References

Example Test

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace MSTestPoc
{
    [TestClass]
    public class MSTestPocTests
    {
        // Code run once before all tests
        [ClassInitialize]
        public static void ClassInit(TestContext context)
        {}

        // Code that is run once after all tests
        [ClassCleanup()]
        public static void ClassCleanup()
        {}

        // Code that is run once before each test
        [TestInitialize]
        public void Initialize()
        {}

        // Code that is run once after each test
        [TestCleanup]
        public void Cleanup()
        {}

        //Example Test
        [TestMethod]
        public void ExampleTest()
        {
            //Arrange.  
            int expected = 1;

            //Act
            int? actual = MethodToTest();

            //Assert
            Assert.IsTrue(expected == actual, "expected == actual");

            //Other example assets
            Assert.AreEqual(expected, actual);
            Assert.IsFalse(false);
            //Assert.Fail("Explicitly Fail");
            Assert.ThrowsException<NotImplementedException>(NotImplementedMethod);
        }

        [DataRow(1)]
        [DataRow(2)]
        [DataRow(3)]
        [DataTestMethod]
        public void ParameterizedTest(int value)
        {
            //Do some testing here
            Console.WriteLine(value);
        }

        private int MethodToTest()
        {
            return 1;
        }

        private void NotImplementedMethod()
        {
            throw new NotImplementedException();
        }
    }
}
           
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Selenium WebDriver Cheat Sheet Cheat Sheet
          Cypressio Cheat Sheet
          ISTQB Test Automation Engineering Cheat Sheet

          More Cheat Sheets by GregFinzer

          C# Naming Conventions Cheat Sheet
          Gremlin Cheat Sheet
          NUnit Cheat Sheet