Friday, November 14, 2014

Use the TestInitialize attribute to do setup steps for all tests in a class in C#.

using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Whatever.Yo.Tests
{
   [TestClass]
   public class UnitTests
   {
      private string _foo;
      
      [TestInitialize()]
      public void MehTestInitialize()
      {
         _foo = "foo";
      }
      
      [TestMethod]
      public void foo_test()
      {
         Assert.AreEqual(_foo,"foo");
      }
      
      [TestMethod]
      public void bar_test()
      {
         Assert.AreNotEqual(_foo, "bar");
      }
   }
}

No comments:

Post a Comment