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/