NUnit的顺序属性与值数组 [英] NUnit Sequential Attribute with arrays in Values

查看:226
本文介绍了NUnit的顺序属性与值数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何传递的String [] [] 阵列Values​​Attribute?

How I can pass string[][] arrays to 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) { //... }

和我有不能访问非静态字段数组1,在静态情况下。比我标记数组1 静态关键字,比我有的属性参数必须是恒定的前pression ... 比我与只读关键词,仍然我有的属性参数标记它必须是一个常量前pression ...

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...

是这里的任何方式来传递多个阵列? (除丑的String [] [] [] 并通过参数2 相关数组索引[ ] [] 数组[] [] []

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

推荐答案

这是可能的。但是,你需要使用 TestCaseSourceAttribute 而不是连续

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

见一个例子:

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.

这篇关于NUnit的顺序属性与值数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆