如何在 nunit 测试用例中使用元组? [英] How can I use tuples in nunit TestCases?
问题描述
我正在尝试使用元组指定 nUnit testCases,但在 VisualStudio 中出现编译器错误.
I am trying to specify nUnit testCases using tuples, but I get a compiler error in VisualStudio.
这个简单的例子展示了我想要做什么:
This simple example demonstrates what I am trying to do:
[TestCase((1, 2), (3, 5))]
public void TestRangeOverlaps((int start, int end) firstRange, (int start, int end) secondRange)
{
}
如果这是可能的,我错过了什么?
If this is possible, what am I missing?
推荐答案
您可以使用 TestCaseSource
属性并指定 IEnumerable<(int, int)[]>
作为值源.
You can use TestCaseSource
attribute and specify IEnumerable<(int, int)[]>
for value source.
每个 IEnumerable
项目代表一组传递给测试方法的参数.在您的情况下,它是一个两个元组,因此您应该每次都返回一个数组以传递给 TestRangeOverlaps
Every IEnumerable
item represents a set of parameters passed to test method. In your case it's a two tuples, so you should return an array of them every time to pass to TestRangeOverlaps
[Test]
[TestCaseSource(nameof(Tuples))]
public void TestRangeOverlaps((int start, int end) firstRange, (int start, int end) secondRange)
{
}
public static IEnumerable<(int, int)[]> Tuples
{
get
{
yield return new[] { (1, 2), (3, 5) };
}
}
TestCase
属性仅支持常量值
这篇关于如何在 nunit 测试用例中使用元组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!