使用MSTest无法做到这一点吗?我使用ClassInitialize和ClassCleanup和TestInitialize和TestCleanup属性找到的示例仅显示了如何写入控制台.没有一个显示这些属性的任何更详细的用法. 解决方案以下是使用TestInitialize和TestCleanup的简单示例.[TestClass]public class UnitTest1{ private NorthwindEntities context; [TestInitialize] public void TestInitialize() { this.context = new NorthwindEntities(); } [TestMethod] public void TestMethod1() { Assert.AreEqual(92, this.context.Customers.Count()); } [TestCleanup] public void TestCleanup() { this.context.Dispose(); }}I've used NUnit with VS2008, and now am adapting to MSTest on VS2010. I used to be able to create an object in TestSetup() and dispose of it in TestCleanup(), and have the object created each time a test method was run in NUnit, preventing me from duplicating the code in each test method.Is this not possible with MSTest? The examples I am finding using the ClassInitialize and ClassCleanup and TestInitialize and TestCleanup attributes only show how to write to the console. None show any more detailed use of these attributes. 解决方案 Here is a simple example using TestInitialize and TestCleanup.[TestClass]public class UnitTest1{ private NorthwindEntities context; [TestInitialize] public void TestInitialize() { this.context = new NorthwindEntities(); } [TestMethod] public void TestMethod1() { Assert.AreEqual(92, this.context.Customers.Count()); } [TestCleanup] public void TestCleanup() { this.context.Dispose(); }} 这篇关于在VS2010中使用MS Test ClassInitialize()和TestInitialize()而不是NUnit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-23 07:22