使用 Nunit TestCaseSource 运行测试设置的正确方法 [英] Correct Way To Run Test Set Up with Nunit TestCaseSource
问题描述
我正在尝试使用 NUnit 中的 TestCaseSource 运行多个测试.但是我正在努力让 [SetUp] 在我想要的时候运行.
I am trying to run multiple tests using TestCaseSource in NUnit. But I am struggling to get the [SetUp] to run when I want it.
目前它按我想要的方式工作,但感觉并不正确".所以下面是主要的测试用例代码(简化):
At the moment it works how I want it to but it doesnt feel "right". So the following is the main test case code (simplified):
public class ImportTestCases
{
ImportTestCases()
{
TestData.RunTestSetup();
}
public static IEnumerable TestCases
{
get
{
//run the funciton under test...
var results = RunFunctionSubjectToTest(TestData.ImportantVar);
//get multiple results...
var allProperties =new TestCaseData(o).Returns(true)
ExpandNestedProperties(results.AllProperties)
.ToList()
.ConvertAll(o => new TestCaseData(o).Returns(true));
return allProperties;
}
}
}
[TestFixture]
public class ImportTests
{
[TestFixtureSetUp]
public void ImporTestSetup()
{
TestData.RunTestSetup();
}
[Test, TestCaseSource(typeof(ImportTestCases), nameof(ImportTestCases.TestCases))]
public bool PropertyTest(UnitTestHelper.PropInfo info)
{
return info.DoTheyMatch;
}
}
这里的问题是[SetUp]没有在ImportTestCasesTestCases"之前运行运行属性get".ImportTestCases"的构造函数也没有运行.因此,为了确保在引用 ImportVar 之前运行RunTestSetup",我必须执行以下操作:
The trouble here is that [SetUp] does not run before ImportTestCases "TestCases" property "get" is ran. The Constructor of "ImportTestCases" is not ran either. So in order to ensure "RunTestSetup" is ran before ImportVar is referenced I have to do the following:
public static class TestData
{
private static bool HasSetUpRan = false;
private static int _importantVar;
public static int ImportantVar
{
get
{
if(!HasSetUpRan)
{
RunTestSetup();
}
return _importantVar;
}
}
public static void RunTestSetup()
{
if (HasSetUpRan)
{
return;
}
///do set up
//e.g. _importantVar = GenerateId();
//end
HasSetUpRan= true;
}
}
如您所见,这可确保在返回变量之前已运行设置.可悲的是,这是迄今为止我设法让它工作的唯一方法.正如我所说,这感觉错误"且过于复杂.也许我在这里过度使用了 testCases?或者我应该使用某种参数化的测试用例(这可能吗?).
As you can see this ensures that Set up has ran before the variable is returned. Sadly this is the only way I have managed to get it to work so far. Which as I say feels "wrong" and over complicated. Perhaps I am overusing testCases here? or I should use some kind of paramatised testcase (is that possible?).
我试图简化上面的代码,如果我试图测试的内容没有意义,我深表歉意.
I have tried to simplify the code above so apologies if it simply doesnt make sense what I am trying to test.
要点是否有在创建 TestCaseSources 之前运行的 [Setup]?
推荐答案
重点是测试用例将在加载测试时定位.所以带有 [TestFixtureSetUp]
属性的例程将在TestCases"属性被调用后执行.但是,您可以在静态构造函数中执行一些设置例程.但是为了首先调用它,您需要将测试数据放在同一个类中:
The main point is that test cases will be located at the time the tests are loaded. So the routine with [TestFixtureSetUp]
attribute will be executed after the "TestCases" property is called. However you could execute some set-up routine within the static constructor. But in order to call it first you need to put your test data in the same class:
[TestFixture]
public class ImportTests
{
static ImportTests()
{
//Step 1
//run your set-up routine
}
//Step 3
[Test, TestCaseSource(nameof(ImportTests.TestCases))]
public bool PropertyTest(string s) => string.IsNullOrEmpty(s);
//Step 2
public static IEnumerable TestCases => new[] {new TestCaseData("").Returns(true)};
}
这篇关于使用 Nunit TestCaseSource 运行测试设置的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!