.NET Core中替代MSTest的DataSource属性是什么?

4

根据下面的内容,得知在.NET Core项目的MSTest中已不再支持DataSource属性。那么有什么替代方案吗?

链接: https://learn.microsoft.com/en-us/visualstudio/test/how-to-create-a-data-driven-unit-test?view=vs-2019

信息: .NET Core不支持DataSource属性。 如果您尝试以这种方式访问测试数据,则在.NET Core或UWP单元测试项目中,您将看到类似于"'TestContext' does not contain a definition for 'DataRow' and no accessible extension method 'DataRow' accepting a first argument of type 'TestContext' could be found (are you missing a using directive or an assembly reference?)"的错误。

1个回答

2

2020年9月7日 - 没有解决方法

关于此问题有一个未解决的 issue-233,目前没有评论说明何时能够修复。以下是一些解决方法:

DynamicDateAttribute(CSV示例)

private static string[] SplitCsv(string input)
{
    var csvSplit = new Regex("(?:^|,)(\"(?:[^\"]+|\"\")*\"|[^,]*)", RegexOptions.Compiled);
    var list = new List<string>();
    foreach (Match match in csvSplit.Matches(input))
    {
        string value = match.Value;
        if (value.Length == 0)
        {
            list.Add(string.Empty);
        }

        list.Add(value.TrimStart(','));
    }
    return list.ToArray();
}

private static IEnumerable<string[]> GetData()
{
    IEnumerable<string> rows = System.IO.File.ReadAllLines(@"Resources\NameAddressCityStateZip.csv").Skip(1);
    foreach (string row in rows)
    {
        yield return SplitCsv(row);
    }
}

[TestMethod]
[DynamicData(nameof(GetData), DynamicDataSourceType.Method)]
//x [DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", @"Resources\NameAddressCityStateZip.csv", "NameAddressCityStateZip#csv", DataAccessMethod.Sequential)]
public void TestMethod1(string input, string expected)
{
    // Arrange
    //x string input = _testContext.Properties["Data"].ToString(); //x _testContext.DataRow["Data"].ToString();
    //x string expected = _testContext.Properties["Expected"].ToString(); //x _testContext.DataRow["Expected"].ToString();
    var parser = _serviceProvider.GetService<Parser>();

    // Act
    string actual = parser.MultiParser(input, ModeType.NameAddressCityStateZipCountry).ToString();

    // Assert
    Assert.AreEqual(expected, actual);
}

但是它也有缺点。

自己移植代码

如评论 这里 所示。

等待实现


@Kataras - 谢谢!我需要读取xml数据文件,有没有一种方法可以编写一个特殊的类,并将其用作我的测试方法(MS test2)的属性?如果是,你能告诉我吗? - DeSon

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接