2013-04-23 27 views
3

我如何將string[][]數組傳遞給ValuesAttribute?NUnit序列屬性與數組中的值

我:

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

而且我有Cannot access non-static field "Array1" in static context。比我標誌着Array1static關鍵字,比我有An attribute argument must be a constant expression...比我readonly關鍵字標記它仍然我有An attribute argument must be a constant expression...

是這裏的任何方式來傳遞多個陣列? (除了醜陋的string[][][]和通過param2的相關array[][]的指標在array[][][]

回答

5

這是可能的。但是您需要使用TestCaseSourceAttribute而不是SequentialValues

看到一個例子:這裏

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) 
{ 
    ... 
} 

另一個好處:測試用例是更好的組織,他們可以在多個測試很容易重複使用。

+0

謝謝,這對我有用,但在這裏有任何方式將string [] []數組傳遞給ValuesAttribute? – Vladimirs 2013-04-24 08:29:30

+1

不要認爲這是可能的,因爲屬性只接受常量參數。但是'Array'不能被聲明爲常量。見例如這個問題http://stackoverflow.com/questions/5142349/declare-a-const-array – 2013-04-24 11:34:52