Using MS Test ClassInitialize() and TestInitialize() in VS2010 as opposed to NUnit

不打扰是莪最后的温柔 提交于 2019-12-30 05:34:12

问题


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.


回答1:


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();
    }
}


来源:https://stackoverflow.com/questions/4565475/using-ms-test-classinitialize-and-testinitialize-in-vs2010-as-opposed-to-nun

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!