在VS2010中使用MS Test ClassInitialize()和TestInitialize()而不是NUnit [英] Using MS Test ClassInitialize() and TestInitialize() in VS2010 as opposed to NUnit

查看:227
本文介绍了在VS2010中使用MS Test ClassInitialize()和TestInitialize()而不是NUnit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将NUnit与VS2008一起使用,现在正在适应VS2010上的MSTest.我曾经能够在TestSetup()中创建一个对象,然后在TestCleanup()中对其进行处理,并且每次在NUnit中运行测试方法时都会创建该对象,从而避免了在每个测试方法中重复编写代码.

使用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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆