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
此随笔或为自己所写、或为转载于网络。仅用于个人收集及备忘。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步