如何将2d数组设置为单元测试的参数

如果期望变量是整数,则它就像这样

[DataRow(2)]
[TestMethod]
public void TestMethod(int expected)
{
      // some code...
}

但是当有2d数组int [,]而不是int参数时,应该怎么办? 当我尝试这样做

[DataRow(new int[,] { {0,0},{0,0} })]
[TestMethod]
public void TestMethod(int[,] expected)
{
      // some code...
}

错误说

属性参数必须是属性参数类型的常量表达式,typeof表达式或数组创建表达式

tangyb521 回答:如何将2d数组设置为单元测试的参数

您可以使用DynamicData Attribute

实现它
[DataTestMethod]
[DynamicData(nameof(TestDataMethod),DynamicDataSourceType.Method)]
public void TestMethod1(int[,] expected)
{
    // some code...
    var b = expected;
}

static IEnumerable<object[]> TestDataMethod()
{
    return new[] { new[] { new int[,] { { 0,0 },{ 1,1 } } } };
}

输出

enter image description here

本文链接:https://www.f2er.com/2745942.html

大家都在问