Nunit 使用 TestCaseSource 运行 TestCase,第一次迭代没有参数?为什么?

Nunit runsTestCase with a TestCaseSource with the first iteration having no parameters? Why?

您好,我是 Nunit 的新手,我正在将一系列对象作为 TestCaseSource 传递给 TestCase。出于某种原因,虽然 Nunit 似乎 运行 测试首先没有传递给它的参数导致忽略输出:

测试:

private readonly object[] _nunitIsWeird =
{
    new object[] {new List<string>{"one", "two", "three"}, 3},
    new object[] {new List<string>{"one", "two"}, 2}

};

[TestCase, TestCaseSource("_nunitIsWeird")]
public void TheCountsAreCorrect(List<string> entries, int expectedCount)
{
    Assert.AreEqual(expectedCount,Calculations.countThese(entries));
}

TheCountsAreCorrect (3 tests), Failed: One or more child tests had errors TheCountsAreCorrect(), Ignored: No arguments were provided TheCountsAreCorrect(System.Collections.Generic.List1[System.String],2), Success TheCountsAreCorrect(System.Collections.Generic.List1[System.String],3), Success

所以第一个测试被忽略了,因为没有参数,但我不想要这个测试 运行,永远,它没有任何意义,它弄乱了我的测试输出。我尝试忽略它并正确设置测试输出但是当我再次 运行 所有测试时它又回来了。

有没有我遗漏的东西,我到处都找过了。

TestCaseTestCaseSource 做两件不同的事情。您只需要删除 TestCase 属性。

[TestCaseSource("_nunitIsWeird")]
public void TheCountsAreCorrect(List<string> entries, int expectedCount)
{
    Assert.AreEqual(expectedCount,Calculations.countThese(entries));
}

TestCase attribute is for supplying inline data, so NUnit is attempting to supply no parameters to the test, which is failing. Then it's processing the TestCaseSource 属性,查找它提供的数据并尝试将其传递给测试,测试工作正常。

附带说明一下,严格来说,文档建议您还应该使用如下所示的 Test 属性标记您的 TestCaseSource 测试,但我从来没有发现这是必要的:

[Test, TestCaseSource("_nunitIsWeird")]
public void TheCountsAreCorrect(List<string> entries, int expectedCount)