【问题标题】:NUnit Sequential Attribute with arrays in ValuesNUnit 顺序属性与值中的数组
【发布时间】:2013-04-16 18:49:05
【问题描述】:

如何将 string[][] 数组传递给 ValuesAttribute?

我有:

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。比我用static 关键字标记Array1 比我有An attribute argument must be a constant expression... 比我用readonly 关键字标记它,我仍然有An attribute argument must be a constant expression...

这里有什么方法可以传递多个数组吗? (除了丑陋的string[][][] 和在array[][][] 中传递相关array[][]param2 索引)

【问题讨论】:

    标签: c# arrays nunit sequential


    【解决方案1】:

    这是可能的。但是你需要使用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)
    {
        ...
    }
    

    这里的另一个好处:测试用例组织得更好,并且可以在多个测试中轻松重用。

    【讨论】:

    • 谢谢,这对我有用,但这里有什么方法可以将 string[][] 数组传递给 ValuesAttribute?
    • 不要认为这是可能的,因为属性只接受常量参数。但是Array 不能声明为常量。参见例如这个问题stackoverflow.com/questions/5142349/declare-a-const-array
    猜你喜欢
    • 2015-01-26
    • 2015-07-19
    • 1970-01-01
    • 2017-10-16
    • 2012-06-22
    • 1970-01-01
    • 1970-01-01
    • 2011-04-12
    • 2015-06-17
    相关资源
    最近更新 更多