NUnit Sequential Attribute with arrays in Values

落花浮王杯 提交于 2019-12-02 00:26:54

问题


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[][][])


回答1:


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.



来源:https://stackoverflow.com/questions/16171321/nunit-sequential-attribute-with-arrays-in-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!