VS自身的单元测试方法DEMO

 ///用来修饰测试类

    [TestClass()]

    public class Program

    {

        private TestContext testContextInstance;

        /// <summary>

        ///获取或设置测试上下文,上下文提供

        ///有关当前测试运行及其功能的信息。

        ///</summary>

        public TestContext TestContext

        {

            get

            {

                return testContextInstance;

            }

            set

            {

                testContextInstance = value;

            }

        }

        static int a = 0;

        static int b = 0;

        [TestMethod()]

     

        static void Main(string[] args)

        {

        }

    

        [ClassInitialize()]

     

        public static void TextInital(TestContext testContext)

        {//初始化a,可以初始化数据连接等

            a = 10;

            Console.WriteLine("测试开前执行一次");

            Console.WriteLine("a=10");

        }

  

        [ClassCleanup()]

        public static void TextDispose()

        {//初始化a,可以初始化数据连接等



            Console.WriteLine("测试完成后执行一次");

        }

        [TestInitialize()]

        public void BeforeEvery()

        {

            b = 1;

            Console.WriteLine("b=1");

            Console.WriteLine("每个方法测试开始前都执行我");

        }



        [TestCleanup()]

        public void afterEvery()

        {

            b = 0;

            Console.WriteLine("b = 0");

            Console.WriteLine("每个方法测试完成后都执行我");

        }

        [TestMethod()]

        public void Add()

        {

            bool t = a == 10;

            Assert.AreEqual(true, t);

        }



        [TestMethod()]

        public void Sub()

        {

            int a = 0;

            int b = 10 / a;

            Assert.AreEqual(true, true);

        }

        public void Ignore()

        {

            Assert.AreEqual(true, true);

        }





    }

区别 VS没有Ignore,没有异常属性

你可能感兴趣的:(单元测试)