gpt4 book ai didi

c# - 值中包含数组的 NUnit 顺序属性

转载 作者:行者123 更新时间:2023-11-30 14:59:50 25 4
gpt4 key购买 nike

如何将 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) { //... }

而且我有 无法在静态上下文中访问非静态字段“Array1”。比我用 static 关键字标记 Array1 比我有 An attribute argument must be a constant expression... 我用readonly 关键字,我仍然有 An attribute argument must be a constant expression...

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

最佳答案

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

这里的另一个好处是:测试用例组织得更好,它们可以很容易地在多个测试中重复使用。

关于c# - 值中包含数组的 NUnit 顺序属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16171321/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com