Gallio单元测试启动代码

时间:2012-12-18 01:05:12

标签: unit-testing mbunit

这将是一个业余级别的问题。有没有办法将启动代码添加到使用MBUnit 3.4.0.0的测试项目中?我尝试将[TestFixture]和[FixtureSetUp]属性添加到我想先运行的代码中,但遗憾的是没有帮助。

1 个答案:

答案 0 :(得分:1)

[FixtureSetUp]应该在[TestFixture]中包含的测试的任何测试之前执行一次,但这两个不能互换使用。

这是一个简单的例子。不可否认,该类不必使用[TestFixture]属性进行修饰,但这是一种很好的做法。

[TestFixture]
public class SimpelTest
{
    private string value = "1";

    [FixtureSetUp]
    public void FixtureSetUp()
    {
        // Will run once before any test case is executed.
        value = "2";
    }

    [SetUp]
    public void SetUp()
    {
        // Will run before each test
    }

    [Test]
    public void Test()
    {
        // Test code here
        Assert.AreEqual("2", value);
    }

    [TearDown]
    public void TearDown()
    {
        // Will run after the execution of each test
    }

    [FixtureTearDown]
    public void FixtureTearDown()
    {
        // Will run once after every test has been executed
    }
}