我正在尝试编写一个单元测试来比较两个数组。我已经定义了单元测试:
[DataTestMethod]
[DataRow(
new[] { "COM3", "COM1", "COM2" },
new[] { "COM1", "COM2", "COM3" }
)]
...
public void TestCOMPortSorting(string[] unorderedPorts, string[] expectedOrderedPorts)
但是,我的 IDE 会引发以下错误:
An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type
我尝试使用外部变量,将数组定义为
new string[]
, 用这些数组创建一个数组,一切都没有运气。如何将这两个数组用作单元测试的参数?
最佳答案
对于如此复杂的数据,请改为使用 DynamicData
属性
This attribute allows to get the values of the parameters from a method or a property. The method or the property must return an
IEnumerable<object[]>
. Each row corresponds to the values of a test.
[DataTestMethod]
[DynamicData(nameof(TestDataMethod), DynamicDataSourceType.Method)]
public void TestCOMPortSorting(string[] unorderedPorts, string[] expectedOrderedPorts) {
//...
}
static IEnumerable<object[]> TestDataMethod() {
return new[] {
new []{ new[] { "COM3", "COM1", "COM2" }, new[] { "COM1", "COM2", "COM3" } } //a data row
};
}
引用 MSTest v2: Data tests
关于c# - 如何提供两个数组作为 DataRow 参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57609039/