zoukankan      html  css  js  c++  java
  • Unit Test测试框架中的测试的执行顺序

    [ClassInitialize()] [ClassCleanup()] [TestInitialize()] [TestMethod] [TestCleanup()]
    在执行一个或多个[TestMethod]输出时, [ClassInitialize()] 最先执行,[ClassCleanup()]最后执行,对于执行每个[TestMethod],执行顺序是[TestInitialize()] [TestMethod] [TestCleanup()]

    using System;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using System.IO;
    namespace UnitTestProject1
    {
        [TestClass]
        public class UnitTest1
        {
            static StreamWriter sw = new StreamWriter("C:\temp\Test.txt", false);
            [ClassInitialize()]
            public static void MyClassInitialize(TestContext testContext)
            {
                sw.WriteLine("MyClassInitialize");
                sw.Flush();
            }
            [ClassCleanup()]
            public static void MyClassCleanup()
            {
                sw.WriteLine("MyClassCleanup");
                sw.Flush();
            }
    
            [TestInitialize()]
            public void MyTestInitialize()
            {
                sw.WriteLine("MyTestInitialize");
                sw.Flush();
            }
            [TestCleanup()]
            public void MyTestCleanup()
            {
                sw.WriteLine("MyTestCleanup");
                sw.Flush();
            }
            [TestMethod]
            public void TestMethod1()
            {
                sw.WriteLine("TestMethod1");
                sw.Flush();
            }
            [TestMethod]
            public void TestMethod2()
            {
                sw.WriteLine("TestMethod2");
                sw.Flush();
            }
            [TestMethod]
            public void TestMethod3()
            {
                sw.WriteLine("TestMethod3");
                sw.Flush();
            }
        }
    }

    3个method一起选中执行的输出

    MyClassInitialize

    MyTestInitialize
    TestMethod1
    MyTestCleanup

    MyTestInitialize
    TestMethod2
    MyTestCleanup

    MyTestInitialize
    TestMethod3
    MyTestCleanup

    MyClassCleanup

    选中一个,执行后的输出

    MyClassInitialize
    MyTestInitialize
    TestMethod1
    MyTestCleanup
    MyClassCleanup

  • 相关阅读:
    eclipse- DDMS截图功能使用
    宏-新项目物理按键不能用
    宏-宏的添加跟代码中的使用
    SQlite-数据库的访问实例(转)
    git 工具的使用总结(6)-提交合并处理
    git 工具的使用总结(5)-查看历史记录
    git -处理分支合并
    Linux查询网址
    SQLite常用网址
    Java查询网址
  • 原文地址:https://www.cnblogs.com/shy1766IT/p/6057491.html
Copyright © 2011-2022 走看看