1

I'm currently building a Test Project and I need to pass several arguments to the test function. Because I need to call the test function with different parameter sets I decided to use ms-test with the [DataTestMethod]. Now I need to pass jagged arrays as Parameters to the function. But I don't get it to work. The call for TestMethod1 is working. The call for TestMethod2 is not working as it is not successfully compiling.

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

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace UnitTestProject2
{
    [TestClass]
    public class UnitTest1
    {

        [DataTestMethod]
        [DataRow(new int[] { })]
        public void TestMethod1(int[] values)
        {

        }

        [DataTestMethod]
        [DataRow(new int [][] { } )]
        public void TestMethod2(int[][] values)
        {
            
        }
    }
}

Does anyone has any suggestion to get this working? Sadly I need to use some kind of two dimension data type because I need to pass information about two loops inside of the test function. I can't use the params keyword because I need two of this jagged arrays in the test function.

Regards White

4
  • int[][] is not a multi-dimensional array but a jagged array. int[,] is a multi-dimensional array of rank 2 Commented Jan 14, 2021 at 17:05
  • thanks for the hint, I editet my question I really want to use a jagged array because not every "row" have the same amount of "columns" Commented Jan 14, 2021 at 17:23
  • You cannot pass such an array as an Attribute parameter. Commented Jan 14, 2021 at 17:25
  • You can't use a proper multi-dimensional array either Commented Jan 14, 2021 at 18:27

2 Answers 2

1

You cannot use jagged array as a parameter in an attribute, because you cannot declare it as a const. More explanation in here: Const multi-dimensional array initialization

For your purpose I would use DynamicDataAttribute:

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;

namespace UnitTestProject
{
    [TestClass]
    public class TestClass
    {
        static IEnumerable<int[][][]> GetJaggedArray
        {
            get
            {
                return new List<int[][][]>
                {
                    new int[][][]
                    {
                        new int [][]
                        {
                            new int[] { 1 },
                            new int[] { 2, 3, 4 },
                            new int[] { 5, 6 }
                        }
                    }
                };
            }
        }

        [TestMethod]
        [DynamicData(nameof(GetJaggedArray))]
        public void Test1(int[][] jaggedArray)
        {
            Assert.AreEqual(1, jaggedArray[0][0]);
            Assert.AreEqual(2, jaggedArray[1][0]);
            Assert.AreEqual(3, jaggedArray[1][1]);
            Assert.AreEqual(4, jaggedArray[1][2]);
            Assert.AreEqual(5, jaggedArray[2][0]);
            Assert.AreEqual(6, jaggedArray[2][1]);
        }
    }
}

I know that syntax IEnumerable<int[][][]> is not pleasant for eyes, but since DynamicDataAttribute.GetData(MethodInfo) method is returning IEnumerable<object[]>, and your object is int[][], this is what you get.

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

1 Comment

Am I right, that in this case you need GetJaggedArray method for every Test1 call? It's not very compact code...
0

The way to pass a jagged array in the DataRow attribute is to put it last in the data row's input list and use the 'params' for the last argument of your test method.
E.g. I added a new boolean input argument 'anotherTestInput' and passed the jagged array argument 'values' last and used the 'params' keyword:

        [DataTestMethod]
        [DataRow(true, new[] { 3,2 }, new[] { 4,3,5 }, new[] { 4 })]
        [DataRow(false, new[] { 8,2 }, new[] { 1,7,9 })] 
        public void TestMethod2(bool anotherTestInput, params int[][] values)
        {
            ...
        }

That way you can write only 1 test method and use different DataRow inputs to test (which is the purpose of DataRow[]).

Comments

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.