4

How I can pass string[][] arrays to ValuesAttribute?

I have:

public string[][] Array1 = new[] {new[] {"test1", "test2"}};
//...
[Test, Sequential]
public void SomeTest(
    [Values("val1", "val2", "val3")] string param1, 
    [Values(Array1, Array2, Array3)] string[][] param2) { //... }

And I've got Cannot access non-static field "Array1" in static context. Than I mark Array1 with static keyword and than I've got An attribute argument must be a constant expression... than I mark it with readonly keyword and still I have An attribute argument must be a constant expression...

Is here any way to pass multiple arrays? (Except ugly string[][][] and passing param2 indexes of relevant array[][] in array[][][])

Vladimirs
  • 8,232
  • 4
  • 43
  • 79

1 Answers1

5

It is possible. But you need to use TestCaseSourceAttribute instead of Sequential and Values.

See an example:

object[][] testCases = new[] {

    // test case 1
    new object[] {
        "val1",
        new[] { "test11", "test12" }
    },

    // test case 2
    new object[] {
        "val2",
        new[] { "test21", "test22" }
    },

    // test case 3
    new object[] {
        "val3",
        new[] { "test31", "test32", "test33", "test34" }
    }
};

[Test]
[TestCaseSource("testCases")]
public void SomeTest(string param1, string[] param2)
{
    ...
}

Another benefits here: test cases are better organised and they can be easily reused in multiple tests.

Alexander Stepaniuk
  • 6,217
  • 4
  • 31
  • 48
  • Thank you, that works for me, but is here any way to pass string[][] arrays to ValuesAttribute? – Vladimirs Apr 24 '13 at 08:29
  • 1
    Don't think it is possible, as attributes accept constant only parameters. But `Array` cannot be declared as constant. See e.g. this question http://stackoverflow.com/questions/5142349/declare-a-const-array – Alexander Stepaniuk Apr 24 '13 at 11:34