c# - 如何让设置函数在 NUnit 中的每个测试 session 只运行一次?

标签 c# nunit nunit-2.5

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 ),打印的数字每次都会增加 1。换句话说,这个函数在每个测试 session 中运行多次。

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

最佳答案

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

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 ...
        }
    }
}

关于c# - 如何让设置函数在 NUnit 中的每个测试 session 只运行一次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36980883/

相关文章:

.net - 多线程应用程序中的单元测试事件

c# - 使用 NUnit 测试调用私有(private)方法的公共(public)方法

.net - 在 NUnit 中捕获断言

c# - 转换数据 GridView

c# - Visual Studio 不会更新我的数据源的属性

visual-studio-2010 - VS2012专业代码覆盖率

unit-testing - Nunit 参数化 TestFixtures 并在运行时设置参数?

c# - C#中有什么可以用作数据库触发器的吗

c# - BackgroundWorker 中未处理的异常

testing - 在运行时为 NUnit 测试提供参数