4

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

1
  • 1
    So what happened when you run the second code? Commented Jan 19, 2020 at 2:35

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

I've just updated my answer with the expected result image, Please take a look @Andrey
Perfect! Thanks :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.