c# - 如何提供两个数组作为 DataRow 参数?

标签 c# unit-testing mstest

我正在尝试编写一个单元测试来比较两个数组。我已经定义了单元测试:

[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/

相关文章:

c# - LuaInterface 内存泄漏问题

teamcity - 在 TeamCity 7 中为 MSTest 运行程序指定测试程序集时了解通配符

C#:如何唤醒已关闭的系统?

c# - 如何使用 Restsharp 反序列化 Xml 列表?

python - 将任意命名的文件导入为 Python 模块,而不生成字节码文件

java - 私有(private)类单元测试

asp.net-mvc - 如何在 MSTest 中对 JsonResult 和集合进行单元测试

visual-studio-2019 - 即使通过 UI 选择该选项,单元测试也不会并行运行

c# - C# 事件是同步的吗?

c# - 如何对 "HttpContext.Current.Request.LogonUserIdentity.Name"进行单元测试?