[英]Using the 'TestCase' attribute with a two-dimensional array
I'm using NUnit and trying to implement tests for the following method: It should accept two integers and returns two dimensional array. 我正在使用NUnit并尝试为以下方法实现测试:它应该接受两个整数并返回二维数组。 So, header of my test looks like:
所以,我测试的标题看起来像:
[TestCase(5, 1, new int[,]{{1}, {2}, {3}, {4}, {5}})]
public void MyTestMethod(int a, int b, int[][] r)
During compilation I've got the following error: 在编译期间,我遇到以下错误:
Error CS0182: An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type (CS0182)
错误CS0182:属性参数必须是属性参数类型的常量表达式,typeof表达式或数组创建表达式(CS0182)
I know it can be done using TestCaseSource
to refer to object arrays like the answers to the following questions: 我知道可以使用
TestCaseSource
来引用对象数组,例如以下问题的答案:
which gives code like: 它提供了如下代码:
private object[][] combination_tests = new [] {
new object[] {5, 1, new [,]{{1}, {2}, {3}, {4}, {5}}},
};
[Test]
[TestCaseSource("combination_tests")]
public void MyTestMethod(int a, int b, int[,] r)
but I still have the question: is it possible to do that with using only the TestCase
attribute? 但我仍然有一个问题:只使用
TestCase
属性是否可以做到这一点?
Is it absolutely necessary for you to have the same signature for your method, ie 是否绝对有必要为您的方法使用相同的签名,即
public void MyTestMethod(int a, int b, int[][] r)
{
// elided
}
Depending on your situation, you have two options available, both of which use the [TestCase]
attribute as you said you wanted in the question: 根据您的具体情况,您有两个可用选项,两个选项都使用您在问题中所需的
[TestCase]
属性:
is it possible to do that with using only the
TestCase
attribute?只使用
TestCase
属性可以做到这一点吗?
I prefer the first option as it feels more concise, but both will suit your needs. 我更喜欢第一种选择,因为它感觉更简洁,但两者都符合您的需求。
Option 1: If it is NOT necessary to keep the same signature 选项1:如果没有必要保持相同的签名
You could modify the signature slightly so that instead of an array (not compile-time constant), you pass in a string ( which is compile-time constant ) which can be used to obtain the array instead, eg 您可以稍微修改签名,以便代替数组(不是编译时常量),传入一个字符串( 这是编译时常量 ),可以用来获取数组,例如
private static int[][] getArrayForMyTestMethod(string key)
{
// logic to get from key to int[][]
}
[TestCase(5, 1, "dataset1")]
public void MyTestMethod(int a, int b, string rKey)
{
int[][] r = getArrayForMyTestMethod(rKey);
// elided
}
Option 2: If it IS necessary to keep the same signature 选项2:如果有必要保持相同的签名
If it is necessary to keep the same signature for the method, you could have a wrapper method that does the same as option 1, ie 如果需要为方法保留相同的签名,则可以使用与选项1相同的包装方法,即
private static int[][] getArrayForMyTestMethod(string key)
{
// logic to get from key to int[][]
}
[TestCase(5, 1, "dataset1")]
public void MyTestMethodWrapper(int a, int b, string rKey)
{
int[][] r = getArrayForMyTestMethod(rKey);
MyTestMethod(a, b, r);
}
public void MyTestMethod(int a, int b, int[][] r)
{
// elided
}
Obviously you could use any type which can be compile-time constant instead of a string
depending on the way your test cases are constructed, but I suggested a string
because you'd be able to give a name to your test case in the NUnit runner that way. 显然,您可以使用任何类型的编译时常量而不是
string
具体取决于构造测试用例的方式,但我建议使用string
因为您可以在NUnit运行器中为您的测试用例命名那样。
Otherwise your alternative is to use [TestCaseSource]
as you mentioned in your question. 否则,您的替代方法是使用您在问题中提到的
[TestCaseSource]
。
You can use the testcasedata object to pass the results in 您可以使用testcasedata对象传递结果
public IEnumerable<TestCaseData> combination_tests()
{
yield return new TestCaseData(5,1,new int[,] {{1}, {2}, {3}, {4}, {5}});
}
[Test]
[TestCaseSource("combination_tests")]
public void test(int a, int b, int[,] r)
{
Console.WriteLine(r[0,0] & r[1,0]);
}
You can also set the test category and test name for each testCaseData item using .SetName("xxx") or .SetCategory("xxx") which can be nice for organizing the tests. 您还可以使用.SetName(“xxx”)或.SetCategory(“xxx”)为每个testCaseData项设置测试类别和测试名称,这对于组织测试非常有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.