2
using NUnit.Framework;
using System;

namespace NUnitTest
{
    [SetUpFixture]
    public class GlobalSetup
    {
        static int test = 0;

        [SetUp]
        public void ImportConfigurationData ()
        {
            test++;
            Console.WriteLine (test);
        }
    }
}

如果我与这​​个全局设置函数一起重复运行我的测试(使用标准的NUnit GUI runner),打印的数字每次都会增加一。换句话说,这个函数在每个测试会话中运行多次。

是否有另一种方法可以让每个测试会话真正运行一次函数,或者这是运行器中的错误?

4

1 回答 1

1

这是一种廉价的解决方法。

using NUnit.Framework;
using System;

namespace NUnitTest
{
    [SetUpFixture]
    public class GlobalSetup
    {
        // The setup fixture seems to be bugged somewhat.
        // Therefore we manually check if we've run before.
        static bool WeHaveRunAlready = false;

        [SetUp]
        public void ImportConfigurationData ()
        {
            if (WeHaveRunAlready)
                return;

            WeHaveRunAlready = true;

            // Do my setup stuff here ...
        }
    }
}
于 2016-05-02T11:24:45.013 回答