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

标签 c# unit-testing nunit testcase testcasesource

大家好,我是 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属性用于提供内联数据,因此 NUnit 试图不向测试提供任何参数,这是失败的。然后它正在处理 TestCaseSource属性,并查找它提供的数据并尝试将其也传递给测试,测试工作正常。

作为旁注,严格来说,文档建议您还应该使用 Test 属性标记您的 TestCaseSource 测试,如下所示,但我从未发现这个必要的:

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

关于c# - Nunit 使用 TestCaseSource 运行 TestCase,第一次迭代没有参数?为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30827144/

相关文章:

nunit - "Inverted"NUnit 测试中的超时功能

c# - 确认对话框以及如何在代码隐藏中控制它

c# - 根据 Microsoft Docs,为什么 SortedDictionary.GetEnumerator() 是 C# 中的 O(log n) 操作?

ios - 用于 PC Mockito 模拟验证的自定义 Hamcrest 匹配器

javascript - JS : How to mock a nested function in a jestJS unit test?

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

C# 连接到西门子 Hipath 3000

c# - 试图修复循环

unit-testing - 使用枚举的 CakePHP 测试的解决方法?

c# - 是否可以加快测试 nservicebus 超时管理器的过程?