Search code examples
c#nunit

Nunit TestCase attribute with Jagged Array ? c#


I'm working on a feature where a jagged array is used as a param, i coult not found any information to create a test case using a jagged array.

  static public int[][] jaggedValues()
  {
      int[][] jagged = new int[3][];

      jagged[0] = [1, 1, 1, 0];
      jagged[1] = [0,5,0,1];
      jagged[2] = [2,1,3,10];
      return jagged;
  }
  [TestCaseSource(nameof(jaggedValues))]

How can i do ? Thanks!.

i tried to search for information however i could not found anything useful


Solution

  • OK, here's an example for you.

    [TestFixture]
    internal class JaggedArrayExample {
    
        [TestCaseSource(typeof(JaggedArrayDataSource), nameof(JaggedArrayDataSource.TestData))]
        public void Demo(int[][] data) {
            Console.WriteLine(data.GetType());
        }
    }
    
    
    internal class JaggedArrayDataSource {
        internal static IEnumerable TestData() {
            yield return Array.Empty<int[]>();
        }
    }
    

    Output:

    System.Int32[][]