Search code examples
c#arraysargumentsmstest

C# using multi dim array as input for DataRow (MSTest)


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


Solution

  • 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.