Using the TestCase attribute with a two-dimensional array

I am using NUnit and trying to run tests for the following method: It should take two integers and return a two-dimensional array. So, the title of my test looks like this:

[TestCase(5, 1, new int[,]{{1}, {2}, {3}, {4}, {5}})] public void MyTestMethod(int a, int b, int[][] r) 

At compile time, I have the following error:

Error CS0182: The attribute argument must be a constant expression, a type expression, or an expression to create an array of attribute parameter type (CS0182)


I know that this can be done using TestCaseSource to indicate arrays of objects, for example, answers to the following questions:

  • How to put new list {1} in NITIT TestCase?
  • NUnit Serial attribute with arrays in values

which gives a code like:

 private object[][] combination_tests = new [] { new object[] {5, 1, new [,]{{1}, {2}, {3}, {4}, {5}}}, }; [Test] [TestCaseSource("combination_tests")] public void MyTestMethod(int a, int b, int[,] r) 

but I still have a question: is it possible to do this using only the TestCase attribute?

+7
arrays c # nunit testcaseattribute
source share
2 answers

You can use the testcasedata object to pass the results to

  public IEnumerable<TestCaseData> combination_tests() { yield return new TestCaseData(5,1,new int[,] {{1}, {2}, {3}, {4}, {5}}); } [Test] [TestCaseSource("combination_tests")] public void test(int a, int b, int[,] r) { Console.WriteLine(r[0,0] & r[1,0]); } 

You can also set the test category and test name for each testCaseData element using .SetName ("xxx") or .SetCategory ("xxx"), which can be nice for organizing tests.

+4
source share

Do you absolutely need to have the same signature for your method, i.e.

 public void MyTestMethod(int a, int b, int[][] r) { // elided } 

Depending on your situation, you have two options, both of which use the [TestCase] attribute, as you said you want in the question

Is this possible using only the TestCase attribute?

I prefer the first option, as it looks more concise, but both will suit your needs.


Option 1: If you do not need to keep the same signature

You can slightly modify the signature so that instead of an array (and not compilation time constants) you pass a string ( which is a compilation time constant ) that you can use to get an array, for example

 private static int[][] getArrayForMyTestMethod(string key) { // logic to get from key to int[][] } [TestCase(5, 1, "dataset1")] public void MyTestMethod(int a, int b, string rKey) { int[][] r = getArrayForMyTestMethod(rKey); // elided } 

Option 2: If you want to keep the same signature

If the method needs to keep the same signature, you can have a wrapper method that does the same as parameter 1, i.e.

 private static int[][] getArrayForMyTestMethod(string key) { // logic to get from key to int[][] } [TestCase(5, 1, "dataset1")] public void MyTestMethodWrapper(int a, int b, string rKey) { int[][] r = getArrayForMyTestMethod(rKey); MyTestMethod(a, b, r); } public void MyTestMethod(int a, int b, int[][] r) { // elided } 

Obviously, you can use any type, which may be a compile-time constant instead of string depending on how you create test cases, but I suggested string because you could give your test case a name in the NUnit runner that way.


Otherwise, your alternative is to use [TestCaseSource] , as you mentioned in your question.

+4
source share

All Articles