3

If expected variable is integer, it simply goes like this

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

But what should one do when there is 2d array int[,] instead of int parameter? When I try to do this

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

error says

An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type

Nguyễn Văn Phong
  • 13,506
  • 17
  • 39
  • 56
Andrey
  • 65
  • 8

1 Answers1

5

You can achieve it by using DynamicData Attribute like below:

[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 } } } };
}

Output

enter image description here

Nguyễn Văn Phong
  • 13,506
  • 17
  • 39
  • 56