开发者

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

开发者 https://www.devze.com 2023-02-02 08:30 出处:网络
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 t

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 w开发者_运维知识库ith 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();
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消