Show Menu
Cheatography

MS Test Example

Example Test

using Microsoft.VisualStudio.TestTools.UnitTesting;
using ValidationLogic;

namespace UnitTests
{
    //Class Decorator
    [TestClass]
    public class EmailValidationTests
    {
        private EmailValidationLogic _validationLogic;

        //Optional, rarely used. Executes before all tests in the assembly are run
        [AssemblyInitialize]
        public static void AssemblyInit(TestContext context)
        {
        }

        //Optional, rarely used. Runs once before all the tests in this class run
        [ClassInitialize]
        public static void TestFixtureSetup(TestContext context)
        {
        }

        //This code runs before each test
        [TestInitialize]
        public void Setup()
        {
            _validationLogic = new EmailValidationLogic();
        }


        //Optional, rarely used. Runs once after all tests in the assembly are run
        [AssemblyCleanup()]
        public static void AssemblyCleanup()
        {
        }

        //Optional, rarely used. This runs once after all the tests in this class are run
        [ClassCleanup]
        public static void TestFixtureTearDown()
        {
        }

        //This is code runs after each test
        [TestCleanup]
        public void TearDown()
        {
        }

        //Example Test Method
        [TestMethod]
        public void EmailShouldNotHaveMoreThanOneAtSymbol()
        {
            //Arrange
            bool expected = false;

            //Act
            bool result = _validationLogic.IsEmailValid("jsmith@@hotmail.com");

            //Assert
            Assert.AreEqual(expected, result, "Email should not have more than one @");
        }

        //Test Exception Being Thrown
        [TestMethod]
        [ExpectedException(typeof(System.DivideByZeroException))]
        public void DivideMethodTest()
        {
            int x = 1;
            int y = x/0;
        }
    }
}
           
 

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

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

          More Cheat Sheets by GregFinzer

          T4 Templates Cheat Sheet
          Crownpeak Models Cheat Sheet